72 lines
2.6 KiB
Java
72 lines
2.6 KiB
Java
|
|
package com.newyetai.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.newyetai.common.exception.util.Assert;
|
|||
|
|
import com.newyetai.common.util.JsonUtils;
|
|||
|
|
import com.newyetai.common.validation.util.ValidateUtils;
|
|||
|
|
import com.newyetai.security.dto.SecurityUserDTO;
|
|||
|
|
import com.newyetai.security.expection.BadLoginException;
|
|||
|
|
import com.newyetai.security.mapstruct.UserMapMapper;
|
|||
|
|
import com.newyetai.usercenter.user.login.dto.LoginUsernamePasswordDTO;
|
|||
|
|
import com.newyetai.usercenter.user.login.dto.UserDetailDTO;
|
|||
|
|
import com.newyetai.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>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|