1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Component public class SpringAuthenticationProvider implements AuthenticationProvider { @Autowired private UserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; @Override public Authentication authenticate(Authentication authentication) { String username = authentication.getName(); String password = authentication.getCredentials().toString(); UserDetails user = userDetailsService.loadUserByUsername(username); if (passwordEncoder.matches(password, user.getPassword())) { return new UsernamePasswordAuthenticationToken(username, password, u.getAuthorities()); } else { throw new BadCredentialsException("The username or password is wrong!"); } } @Override public boolean supports(Class<?> authenticationType) { return authenticationType.equals(UsernamePasswordAuthenticationToken.class); } }
|