package com.kidgrow.oauth2.mobile; import com.kidgrow.authclient.token.MobileAuthenticationToken; import com.kidgrow.oauth2.service.KidgrowUserDetailsService; import lombok.Setter; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.InternalAuthenticationServiceException; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.password.PasswordEncoder; /** * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
* * @Description:
* @Project:
* @CreateDate: Created in 2020/2/20 09:46
* @Author: liuke */ @Setter public class MobileAuthenticationProvider implements AuthenticationProvider { private KidgrowUserDetailsService userDetailsService; private PasswordEncoder passwordEncoder; @Override public Authentication authenticate(Authentication authentication) { MobileAuthenticationToken authenticationToken = (MobileAuthenticationToken) authentication; String mobile = (String) authenticationToken.getPrincipal(); String password = (String) authenticationToken.getCredentials(); UserDetails user = userDetailsService.loadUserByMobile(mobile); if (user == null) { throw new InternalAuthenticationServiceException("手机号或密码错误"); } if (!passwordEncoder.matches(password, user.getPassword())) { throw new BadCredentialsException("手机号或密码错误"); } MobileAuthenticationToken authenticationResult = new MobileAuthenticationToken(user, password, user.getAuthorities()); authenticationResult.setDetails(authenticationToken.getDetails()); return authenticationResult; } @Override public boolean supports(Class authentication) { return MobileAuthenticationToken.class.isAssignableFrom(authentication); } }