|
|
package cn.mb.cloud.auth.security.social;
|
|
import cn.mb.cloud.auth.security.component.MbCloudPreAuthenticationChecks;
|
import cn.mb.cloud.auth.security.service.MbCloudUserAuthDetailsService;
|
import lombok.Getter;
|
import lombok.Setter;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.context.support.MessageSourceAccessor;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.SpringSecurityMessageSource;
|
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetailsChecker;
|
|
/**
|
* @author jason
|
* 手机登录校验逻辑
|
* 验证码登录、社交登录
|
*/
|
@Slf4j
|
public class SocialAuthenticationProvider implements AuthenticationProvider {
|
private MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
|
private UserDetailsChecker detailsChecker = new MbCloudPreAuthenticationChecks();
|
|
@Getter
|
@Setter
|
private MbCloudUserAuthDetailsService userDetailsService;
|
|
@Override
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
SocialAuthenticationToken mobileAuthenticationToken = (SocialAuthenticationToken) authentication;
|
|
String principal = mobileAuthenticationToken.getPrincipal().toString();
|
UserDetails userDetails = userDetailsService.loadUserBySocial(principal);
|
if (userDetails == null) {
|
log.debug("Authentication failed: no credentials provided");
|
|
throw new BadCredentialsException(messages.getMessage(
|
"AbstractUserDetailsAuthenticationProvider.noopBindAccount",
|
"Noop Bind Account"));
|
}
|
|
// 检查账号状态
|
detailsChecker.check(userDetails);
|
|
SocialAuthenticationToken authenticationToken = new SocialAuthenticationToken(userDetails, userDetails.getAuthorities());
|
authenticationToken.setDetails(mobileAuthenticationToken.getDetails());
|
return authenticationToken;
|
}
|
|
@Override
|
public boolean supports(Class<?> authentication) {
|
return SocialAuthenticationToken.class.isAssignableFrom(authentication);
|
}
|
}
|