Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. package com.ateam.egrocery.api.config;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.security.core.AuthenticationException;
  9. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  10. import org.springframework.security.web.AuthenticationEntryPoint;
  11. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  12. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  13. import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
  14.  
  15. import javax.servlet.ServletException;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * Common authentication handlers
  22.  */
  23. @Configuration
  24. public class AuthenticationHandlers {
  25.  
  26.     static final Logger logger = LoggerFactory.getLogger(AuthenticationHandlers.class);
  27.  
  28.     @Bean
  29.     public AuthenticationEntryPoint authenticationEntryPoint() {
  30.         return new AuthenticationEntryPoint() {
  31.             /**
  32.              * Always returns a 401 error code to the client.
  33.              */
  34.             @Override
  35.             public void commence(HttpServletRequest request,
  36.                                  HttpServletResponse response,
  37.                                  AuthenticationException authenticationException) throws IOException,
  38.                     ServletException {
  39.                 logger.debug("Pre-authenticated entry point called. Rejecting access");
  40.                 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied");
  41.             }
  42.         };
  43.     }
  44.  
  45.     @Bean
  46.     public AuthenticationSuccessHandler successHandler() {
  47.         return (request, response, authentication) -> {
  48.             logger.debug("Authentication success");
  49.             response.setStatus(HttpStatus.OK.value());
  50.         };
  51.     }
  52.  
  53.     @Bean
  54.     public AuthenticationFailureHandler failureHandler() {
  55.         return (request, response, exception) -> {
  56.             logger.debug("Authentication failure");
  57.             response.setStatus(HttpStatus.UNAUTHORIZED.value());
  58.         };
  59.     }
  60.  
  61.     @Bean
  62.     public LogoutSuccessHandler logoutSuccessHandler() {
  63.         return (request, response, authentication) ->
  64.                 response.setStatus(HttpStatus.OK.value());
  65.     }
  66.  
  67.     @Bean
  68.     public BCryptPasswordEncoder passwordEncoder() {
  69.         return new BCryptPasswordEncoder();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement