Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. @Component
  2. public class CustomAuthenticationProviderImpl implements UnalAuthenticationProvider {
  3.  
  4. @Autowired
  5. private CustomUserDetailService customUserDetailService;
  6.  
  7. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  8. String username = authentication.getName();
  9. String password = (String) authentication.getCredentials();
  10.  
  11. UserDetails user = null;
  12.  
  13. try{
  14. user = (UserDetails) customUserDetailService.loadUserByUsername(username);
  15. }
  16. catch(UsernameNotFoundException ex){
  17. System.out.println("User name not found");
  18. throw ex;
  19. }
  20.  
  21. if (user == null) {
  22. throw new BadCredentialsException("Username not found.");
  23. }
  24.  
  25. if (!password.equals(user.getPassword())) {
  26. throw new BadCredentialsException("Wrong password.");
  27. }
  28.  
  29. Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
  30.  
  31. return new UsernamePasswordAuthenticationToken(user, password, authorities);
  32. }
  33.  
  34.  
  35. public boolean supports(Class<?> arg0) {
  36. return true;
  37. }
  38.  
  39. }
  40.  
  41. <global-method-security pre-post-annotations="enabled" />
  42.  
  43. <http auto-config="true">
  44.  
  45. <intercept-url pattern="/resources/**" access="permitAll" />
  46. <intercept-url pattern="/login" access="permitAll" />
  47. <intercept-url pattern="/access-denied" access="permitAll" />
  48. <intercept-url pattern="/admin**" access="hasRole('ADMIN')" />
  49. <intercept-url pattern="/**" access="hasRole('USER')" />
  50.  
  51. <form-login login-page="/login" default-target-url="/main" always-use-default-target="true"/>
  52.  
  53. <logout logout-url="/logout" logout-success-url="/"/>
  54.  
  55. <headers>
  56. <frame-options policy="SAMEORIGIN"/>
  57. </headers>
  58.  
  59. <session-management>
  60. <concurrency-control expired-url="/login" />
  61. </session-management>
  62. </http>
  63.  
  64. <authentication-manager alias="authenticationManager">
  65. <authentication-provider ref="customAuthenticationProviderImpl" />
  66. </authentication-manager>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement