48 lines
1.7 KiB
Java
48 lines
1.7 KiB
Java
|
|
package com.njcn.auth.service;
|
||
|
|
|
||
|
|
import cn.hutool.core.bean.BeanUtil;
|
||
|
|
import com.njcn.auth.pojo.bo.BusinessUser;
|
||
|
|
import com.njcn.common.pojo.response.HttpResult;
|
||
|
|
import com.njcn.common.utils.LogUtil;
|
||
|
|
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.UserDetailsService;
|
||
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @author hongawen
|
||
|
|
* <p>
|
||
|
|
* 自定义用户认证和授权
|
||
|
|
*/
|
||
|
|
@Slf4j
|
||
|
|
@Service
|
||
|
|
@AllArgsConstructor
|
||
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
||
|
|
|
||
|
|
private final UserFeignClient userFeignClient;
|
||
|
|
|
||
|
|
@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();
|
||
|
|
BeanUtil.copyProperties(userDTO,businessUser,true);
|
||
|
|
businessUser.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.join(",", userDTO.getRoleName())));
|
||
|
|
return businessUser;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|