| package com.mzl.flower.config.security.provider; | 
|   | 
| import com.mzl.flower.config.security.token.UserIdAuthenticationToken; | 
| import com.mzl.flower.service.impl.BaseUserDetailsService; | 
| import com.mzl.flower.service.impl.WechatUserDetailsService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.security.authentication.BadCredentialsException; | 
| import org.springframework.security.authentication.InternalAuthenticationServiceException; | 
| import org.springframework.security.core.Authentication; | 
| import org.springframework.security.core.AuthenticationException; | 
| import org.springframework.security.core.userdetails.UserDetails; | 
| import org.springframework.security.core.userdetails.UserDetailsService; | 
| import org.springframework.security.core.userdetails.UsernameNotFoundException; | 
| import org.springframework.util.Assert; | 
|   | 
| @Slf4j | 
| public class UserIdAuthenticationProvider extends SelfAuthenticationProvider { | 
|   | 
|     private UserDetailsService userDetailsService; | 
|   | 
|     @Override | 
|     protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException { | 
|         if (authentication.getCredentials() == null) { | 
|             log.debug("Authentication failed: no credentials provided"); | 
|   | 
|             throw new BadCredentialsException(messages.getMessage( | 
|                     "AbstractUserDetailsAuthenticationProvider.badCredentials", | 
|                     "Bad credentials")); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) { | 
|         UserIdAuthenticationToken result = new UserIdAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities()); | 
|         result.setDetails(authentication.getDetails()); | 
|         return result; | 
|     } | 
|   | 
|     @Override | 
|     protected UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException { | 
|         try { | 
|             WechatUserDetailsService wechatUserDetailsService = (WechatUserDetailsService) this.getUserDetailsService(); | 
|             UserDetails loadedUser = wechatUserDetailsService.loadUserByUsername(username); | 
|             if (loadedUser == null) { | 
|                 throw new InternalAuthenticationServiceException( | 
|                         "UserDetailsService returned null, which is an interface contract violation"); | 
|             } | 
|             return loadedUser; | 
|         } catch (UsernameNotFoundException ex) { | 
|             throw ex; | 
|         } catch (InternalAuthenticationServiceException ex) { | 
|             throw ex; | 
|         } catch (Exception ex) { | 
|             throw new InternalAuthenticationServiceException(ex.getMessage(), ex); | 
|         } | 
|     } | 
|   | 
|     protected void doAfterPropertiesSet() throws Exception { | 
|         Assert.notNull(this.userDetailsService, "A UserDetailsService must be set"); | 
|     } | 
|   | 
|     public void setUserDetailsService(UserDetailsService userDetailsService) { | 
|         this.userDetailsService = userDetailsService; | 
|     } | 
|   | 
|     protected UserDetailsService getUserDetailsService() { | 
|         return userDetailsService; | 
|     } | 
|   | 
|     @Override | 
|     public boolean supports(Class<?> authentication) { | 
|         return UserIdAuthenticationToken.class.isAssignableFrom(authentication); | 
|     } | 
| } |