Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public class AuthProviderImpl implements AuthenticationProvider {
  2.  
  3. @Autowired
  4. UserRepository userRepository;
  5.  
  6. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  7.  
  8. @Override
  9. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  10. String login = authentication.getName();
  11.  
  12. Users user = userRepository.findByLogin(login);
  13. if (user == null) {
  14. System.out.println("user not found");
  15. throw new UsernameNotFoundException("user not found");
  16.  
  17. }
  18. String password = authentication.getCredentials().toString();
  19. System.out.println(password);
  20. if (!encoder.matches(password, user.getPassword()) && !password.equals(user.getPassword())) {
  21. System.out.println("invalid password");
  22. throw new BadCredentialsException("invalid password");
  23. }
  24.  
  25. List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  26. authorities.add(new SimpleGrantedAuthority(user.getRole()));
  27.  
  28. return new UsernamePasswordAuthenticationToken(user, null, authorities);
  29. }
  30.  
  31. @Override
  32. public boolean supports(Class<?> aClass) {
  33. return aClass.equals(UsernamePasswordAuthenticationToken.class);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement