2022-06-21 20:47:46 +08:00
|
|
|
|
package com.njcn.auth.service;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
2023-08-10 19:17:30 +08:00
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2022-06-21 20:47:46 +08:00
|
|
|
|
import com.njcn.auth.pojo.bo.BusinessUser;
|
|
|
|
|
|
import com.njcn.common.pojo.response.HttpResult;
|
|
|
|
|
|
import com.njcn.common.utils.LogUtil;
|
2023-08-10 19:17:30 +08:00
|
|
|
|
import com.njcn.oss.utils.FileStorageUtil;
|
2022-06-21 20:47:46 +08:00
|
|
|
|
import com.njcn.user.api.UserFeignClient;
|
|
|
|
|
|
import com.njcn.user.pojo.dto.UserDTO;
|
|
|
|
|
|
import com.njcn.web.utils.RequestUtil;
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author hongawen
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
* 自定义用户认证和授权
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@Service
|
|
|
|
|
|
@AllArgsConstructor
|
2023-06-15 16:01:50 +08:00
|
|
|
|
public class UserDetailsServiceImpl implements CustomUserDetailsService {
|
2022-06-21 20:47:46 +08:00
|
|
|
|
|
|
|
|
|
|
private final UserFeignClient userFeignClient;
|
|
|
|
|
|
|
2023-08-10 19:17:30 +08:00
|
|
|
|
private final FileStorageUtil fileStorageUtil;
|
|
|
|
|
|
|
2022-06-21 20:47:46 +08:00
|
|
|
|
@SneakyThrows
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
|
|
|
|
|
|
String clientId = RequestUtil.getOAuth2ClientId();
|
|
|
|
|
|
BusinessUser businessUser = new BusinessUser(loginName, null, null);
|
|
|
|
|
|
businessUser.setClientId(clientId);
|
|
|
|
|
|
HttpResult<UserDTO> result = userFeignClient.getUserByName(loginName);
|
|
|
|
|
|
LogUtil.njcnDebug(log, "用户认证时,用户名:{}获取用户信息:{}", loginName, result.toString());
|
|
|
|
|
|
//成功获取用户信息
|
|
|
|
|
|
UserDTO userDTO = result.getData();
|
2023-08-10 19:17:30 +08:00
|
|
|
|
BeanUtil.copyProperties(userDTO, businessUser, true);
|
|
|
|
|
|
//处理头像
|
2023-08-10 19:19:30 +08:00
|
|
|
|
// dealHead(businessUser);
|
2022-06-21 20:47:46 +08:00
|
|
|
|
businessUser.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.join(",", userDTO.getRoleName())));
|
|
|
|
|
|
return businessUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-10 19:17:30 +08:00
|
|
|
|
|
2023-06-15 16:01:50 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
public UserDetails loadUserByPhone(String phone) throws UsernameNotFoundException {
|
|
|
|
|
|
String clientId = RequestUtil.getOAuth2ClientId();
|
|
|
|
|
|
BusinessUser businessUser = new BusinessUser(phone, null, null);
|
|
|
|
|
|
businessUser.setClientId(clientId);
|
|
|
|
|
|
HttpResult<UserDTO> result = userFeignClient.getUserByPhone(phone);
|
|
|
|
|
|
LogUtil.njcnDebug(log, "用户验证码认证时,用户名:{}获取用户信息:{}", phone, result.toString());
|
|
|
|
|
|
//成功获取用户信息
|
|
|
|
|
|
UserDTO userDTO = result.getData();
|
2023-08-10 19:17:30 +08:00
|
|
|
|
BeanUtil.copyProperties(userDTO, businessUser, true);
|
2023-08-10 19:19:30 +08:00
|
|
|
|
// dealHead(businessUser);
|
2023-06-15 16:01:50 +08:00
|
|
|
|
businessUser.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.join(",", userDTO.getRoleName())));
|
|
|
|
|
|
return businessUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-10 19:17:30 +08:00
|
|
|
|
/***
|
|
|
|
|
|
*处理用户头像,如果存在头像地址,则去minioss获取url
|
|
|
|
|
|
*/
|
|
|
|
|
|
private void dealHead(BusinessUser businessUser) {
|
|
|
|
|
|
if(StrUtil.isNotBlank(businessUser.getHeadSculpture())){
|
|
|
|
|
|
businessUser.setHeadSculpture(fileStorageUtil.getFileUrl(businessUser.getHeadSculpture()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-21 20:47:46 +08:00
|
|
|
|
}
|