Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. WebConfig:
  2.  
  3.  
  4. @Configuration
  5. @EnableWebMvcSecurity
  6. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  7.  
  8. @Autowired
  9. private CustomAuthenticationProvider customAuthenticationProvider;
  10.  
  11. @Autowired
  12. private AMCiUserDetailsService userDetailsService;
  13.  
  14. @Autowired
  15. private CustomImpersonateFailureHandler impersonateFailureHandler;
  16.  
  17. @Autowired
  18. private LoginFailureHandler loginFailureHandler;
  19.  
  20. @Override
  21. protected void configure(HttpSecurity http) throws Exception {
  22. http
  23. .csrf().disable()
  24. .authorizeRequests()
  25. .antMatchers("/jsp/*.css","/jsp/*.js","/images/**").permitAll()
  26. .antMatchers("/login/impersonate*").access("hasRole('ADMIN') or hasRole('ROLE_PREVIOUS_ADMINISTRATOR')")
  27. .anyRequest().authenticated()
  28. .and()
  29. .formLogin()
  30. .loginPage("/login.jsp")
  31. .defaultSuccessUrl("/jsp/Home.jsp",true)
  32. .loginProcessingUrl("/login.jsp")
  33. .failureHandler(loginFailureHandler)
  34. .permitAll()
  35. .and()
  36. .logout()
  37. .logoutSuccessUrl("/login.jsp?msg=1")
  38. .permitAll()
  39. .and()
  40. .addFilter(switchUserFilter())
  41. .authenticationProvider(customAuthenticationProvider);
  42.  
  43. http.exceptionHandling().accessDeniedPage("/jsp/SecurityViolation.jsp"); //if user not authorized to a page, automatically forward them to this page.
  44. http.headers().addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsMode.SAMEORIGIN));
  45. }
  46.  
  47.  
  48. @Override
  49. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  50. auth.authenticationProvider(customAuthenticationProvider);
  51. }
  52.  
  53. @Bean
  54. public PasswordEncoder passwordEncoder() {
  55. return new BCryptPasswordEncoder();
  56. }
  57.  
  58. //Used for the impersonate functionality
  59. @Bean CustomSwitchUserFilter switchUserFilter() {
  60. CustomSwitchUserFilter filter = new CustomSwitchUserFilter();
  61. filter.setUserDetailsService(userDetailsService);
  62. filter.setTargetUrl("/jsp/Impersonate.jsp?msg=0");
  63. filter.setSwitchUserUrl("/login/impersonate");
  64. filter.setExitUserUrl("/logout/impersonate");
  65. filter.setFailureHandler(impersonateFailureHandler);
  66. return filter;
  67. }
  68. }
  69.  
  70. @Component
  71. public class CustomAuthenticationProvider implements AuthenticationProvider {
  72.  
  73. @Autowired(required = true)
  74. private HttpServletRequest request;
  75.  
  76. @Autowired
  77. private AMCiUserDetailsService userService;
  78.  
  79. @Autowired
  80. private PasswordEncoder encoder;
  81.  
  82. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  83.  
  84. String username = authentication.getName().trim();
  85. String password = ((String) authentication.getCredentials()).trim();
  86. if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
  87. throw new BadCredentialsException("Login failed! Please try again.");
  88. }
  89.  
  90.  
  91. UserDetails user;
  92. try {
  93. user = userService.loadUserByUsername(username);
  94. //log successful attempt
  95. auditLoginBean.setComment("Login Successful");
  96. auditLoginBean.insert();
  97. } catch (Exception e) {
  98. try {
  99. //log unsuccessful attempt
  100. auditLoginBean.setComment("Login Unsuccessful");
  101. auditLoginBean.insert();
  102. } catch (Exception e1) {
  103. // TODO Auto-generated catch block
  104. }
  105. throw new BadCredentialsException("Please enter a valid username and password.");
  106. }
  107.  
  108. if (!encoder.matches(password, user.getPassword().trim())) {
  109. throw new BadCredentialsException("Please enter a valid username and password.");
  110. }
  111.  
  112. if (!user.isEnabled()) {
  113. throw new DisabledException("Please enter a valid username and password.");
  114. }
  115.  
  116. if (!user.isAccountNonLocked()) {
  117. throw new LockedException("Account locked. ");
  118. }
  119.  
  120. Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
  121. List<GrantedAuthority> permlist = new ArrayList<GrantedAuthority>(authorities);
  122.  
  123. return new UsernamePasswordAuthenticationToken(user, password, permlist);
  124. }
  125.  
  126.  
  127. public boolean supports(Class<? extends Object> authentication) {
  128. return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
  129. }
Add Comment
Please, Sign In to add comment