71 lines
2.5 KiB
Java
71 lines
2.5 KiB
Java
package com.senmesh.security.ssb;
|
||
|
||
import java.util.HashSet;
|
||
import java.util.Set;
|
||
|
||
import org.springframework.security.authentication.AuthenticationProvider;
|
||
import org.springframework.security.core.Authentication;
|
||
import org.springframework.security.core.AuthenticationException;
|
||
import org.springframework.security.core.GrantedAuthority;
|
||
import org.springframework.security.core.userdetails.UserDetails;
|
||
|
||
import com.senmesh.common.exception.util.Assert;
|
||
import com.senmesh.common.util.JsonUtils;
|
||
import com.senmesh.common.validation.util.ValidateUtils;
|
||
import com.senmesh.security.dto.SecurityUserDTO;
|
||
import com.senmesh.security.expection.BadLoginException;
|
||
import com.senmesh.security.mapstruct.UserMapMapper;
|
||
import com.senmesh.usercenter.user.login.dto.UserDetailDTO;
|
||
import com.senmesh.usercenter.user.login.service.UserLoginService;
|
||
|
||
import lombok.extern.slf4j.Slf4j;
|
||
|
||
/**
|
||
* 账号密码认证provider,比对信息是否匹配
|
||
*
|
||
* @author sr on 2021-06-21
|
||
*/
|
||
@Slf4j
|
||
public class SsbAuthenticationProvider implements AuthenticationProvider {
|
||
|
||
private UserLoginService userLoginService;
|
||
|
||
public SsbAuthenticationProvider(UserLoginService userLoginService) {
|
||
this.userLoginService = userLoginService;
|
||
}
|
||
|
||
@Override
|
||
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||
SsbLoginDTO ssbLoginDTO =
|
||
(SsbLoginDTO) authentication.getPrincipal();
|
||
log.info("ssb login req:{}", JsonUtils.toJsonString(ssbLoginDTO));
|
||
String errMsg = ValidateUtils.validate(ssbLoginDTO);
|
||
Assert.isTrue(errMsg == null, new BadLoginException(errMsg));
|
||
SecurityUserDTO loadedUser = null;
|
||
try {
|
||
UserDetailDTO userDetailDTO = this.userLoginService.loadUser(ssbLoginDTO);
|
||
loadedUser = UserMapMapper.INSTANCE.dtoToDto(userDetailDTO);
|
||
} catch (BadLoginException be) {
|
||
throw be;
|
||
} catch (Exception e) {
|
||
throw new BadLoginException(e.getMessage(), e);
|
||
}
|
||
|
||
SsbAuthenticationToken result =
|
||
new SsbAuthenticationToken(loadedUser, listUserGrantedAuthorities(loadedUser));
|
||
result.setDetails(authentication.getDetails());
|
||
return result;
|
||
}
|
||
|
||
@Override
|
||
public boolean supports(Class<?> authentication) {
|
||
return (SsbAuthenticationToken.class.isAssignableFrom(authentication));
|
||
}
|
||
|
||
private Set<GrantedAuthority> listUserGrantedAuthorities(UserDetails userDetails) {
|
||
// 什么都不用做,访问资源鉴权的时会拿userDetail去缓存获取对应权限
|
||
return new HashSet<GrantedAuthority>();
|
||
}
|
||
|
||
}
|