Guest User

Untitled

a guest
Oct 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package com.example.security;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6.  
  7. import org.springframework.security.authentication.BadCredentialsException;
  8. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  9. import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
  10. import org.springframework.security.core.AuthenticationException;
  11. import org.springframework.security.core.GrantedAuthority;
  12. import org.springframework.security.core.authority.GrantedAuthorityImpl;
  13. import org.springframework.security.core.userdetails.User;
  14. import org.springframework.security.core.userdetails.UserDetails;
  15.  
  16. public class HardCodedUserDetailsAuthentcationProvider extends
  17. AbstractUserDetailsAuthenticationProvider {
  18.  
  19. private Logger logger = Logger.getLogger(getClass().getName());
  20. private final String username, password;
  21.  
  22. public HardCodedUserDetailsAuthentcationProvider(String username,
  23. String password) {
  24. super();
  25. this.username = username;
  26. this.password = password;
  27. }
  28.  
  29. @Override
  30. protected void additionalAuthenticationChecks(UserDetails userDetails,
  31. UsernamePasswordAuthenticationToken authentication)
  32. throws AuthenticationException {
  33. logger.info(String
  34. .format("additionalAuthenticationChecks requested on %s details with %s authentication",
  35. userDetails, authentication));
  36. }
  37.  
  38. @Override
  39. protected UserDetails retrieveUser(String username,
  40. UsernamePasswordAuthenticationToken authentication)
  41. throws AuthenticationException {
  42.  
  43. Object creds = authentication.getCredentials();
  44. if (creds != null && String.class.isAssignableFrom(creds.getClass())) {
  45. String pw = (String) creds;
  46. if (this.username.equalsIgnoreCase(username)
  47. && this.password.equals(pw)) {
  48. boolean enabled = true, accountNonExpired = true, credentialsNonExpired = true, accountNonLocked = true;
  49. List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
  50. authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
  51. User user = new User(username, pw, enabled, accountNonExpired,
  52. credentialsNonExpired, accountNonLocked, authorities);
  53. return user;
  54. }
  55. throw new BadCredentialsException("Invalid credentials!!");
  56. }
  57.  
  58. // creds should never be null, so we shouldn't ever end up here
  59. throw new IllegalStateException("Unreachable code");
  60. }
  61.  
  62. }
Add Comment
Please, Sign In to add comment