Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public class CustomAuthenticationProvider implements AuthenticationProvider {
  2.  
  3. @Autowired
  4. AccountService accountService;
  5.  
  6. @Override
  7. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  8.  
  9. String name = authentication.getName();
  10. String password = authentication.getCredentials().toString();
  11.  
  12. Account user;
  13.  
  14. try {
  15. if (name.contains("@")) {
  16. user = accountService.findAccount(new AccountSpecificationByEmail(name));
  17. } else {
  18. user = accountService.findAccount(new AccountSpecificationByUserName(name));
  19. }
  20. } catch (AccountNotFoundException e) {
  21. throw new RuntimeException("Wrong login or password");
  22. }
  23.  
  24. if (user.getPassword().equals(password)){
  25. if (user.getRoles().contains(`***chosenRole***`)){
  26. List<GrantedAuthority> grantedAuths = new ArrayList<>();
  27. grantedAuths.add(new SimpleGrantedAuthority(`***chosenRole***`));
  28. Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
  29. return auth;
  30. }
  31. else{
  32. throw new RuntimeException("Role not supported");
  33. }
  34. }
  35. else {
  36. throw new RuntimeException("Wrong login or password");
  37. }
  38.  
  39.  
  40. }
  41.  
  42. @Override
  43. public boolean supports(Class<?> authentication) {
  44. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement