Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. package team.hub.project.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.annotation.Order;
  7. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  8. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  9. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  10. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  13. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  14. import org.springframework.security.crypto.password.PasswordEncoder;
  15. import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
  16. import org.springframework.security.web.AuthenticationEntryPoint;
  17. import org.springframework.security.web.access.AccessDeniedHandler;
  18. import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
  19. import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
  20. import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
  21. import team.hub.project.security.CustomAdminDetailsService;
  22. import team.hub.project.security.CustomUserDetailsService;
  23. import team.hub.project.security.oauth2.CustomOauth2UserService;
  24.  
  25. @Configuration
  26. @EnableWebSecurity
  27. @EnableGlobalMethodSecurity(
  28.         prePostEnabled = true,
  29.         securedEnabled = true,
  30.         jsr250Enabled = true)
  31. public class SecurityConfig {
  32.     @Bean
  33.     public static PasswordEncoder passwordEncoder() {
  34.         return new BCryptPasswordEncoder();
  35.     }
  36.  
  37.     @Bean
  38.     public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
  39.         return new SecurityEvaluationContextExtension();
  40.     }
  41.  
  42.     @Configuration
  43.     @Order(1)
  44.     public static class AdminConfigurationAdapter extends WebSecurityConfigurerAdapter {
  45.         @Autowired
  46.         private AccessDeniedHandler customAccessDenied;
  47.  
  48.         @Autowired
  49.         private AuthenticationEntryPoint customAuthEntryPoint;
  50.  
  51.         @Autowired
  52.         private SimpleUrlAuthenticationSuccessHandler customAuthSuccess;
  53.  
  54.         @Autowired
  55.         private SimpleUrlAuthenticationFailureHandler failureHandler;
  56.  
  57.         @Autowired
  58.         private CustomAdminDetailsService adminDetailsService;
  59.  
  60.         @Autowired
  61.         private SimpleUrlLogoutSuccessHandler customLogoutSuccess;
  62.  
  63.         @Override
  64.         protected void configure(HttpSecurity http) throws Exception {
  65.             http
  66.                     .csrf()
  67.                         .disable()
  68.                     .antMatcher("/api/admin/**")
  69.                     .authorizeRequests()
  70.                     .antMatchers("/api/admin/**").hasRole("ADMIN")
  71.                     .and()
  72.                     .exceptionHandling()
  73.                         .authenticationEntryPoint(customAuthEntryPoint)
  74.                         .accessDeniedHandler(customAccessDenied)
  75.                         .and()
  76.                     .formLogin()
  77.                         .loginProcessingUrl("/api/admin/login")
  78.                         .successHandler(customAuthSuccess)
  79.                         .failureHandler(failureHandler)
  80.                         .usernameParameter("email")
  81.                         .and()
  82.                     .logout()
  83.                         .deleteCookies("JSESSIONID")
  84.                         .logoutUrl("/api/admin/logout")
  85.                         .logoutSuccessHandler(customLogoutSuccess)
  86.                         .and()
  87.                     .rememberMe()
  88.                     .userDetailsService(adminDetailsService)
  89.                     .and();
  90.         }
  91.  
  92.  
  93.         @Override
  94.         protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  95.             auth.authenticationProvider(authenticationProvider());
  96.         }
  97.  
  98.  
  99.         protected DaoAuthenticationProvider authenticationProvider() {
  100.             DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  101.             authProvider.setUserDetailsService(adminDetailsService);
  102.             authProvider.setPasswordEncoder(passwordEncoder());
  103.             return authProvider;
  104.         }
  105.  
  106.     }
  107.  
  108.     @Configuration
  109.     @Order(2)
  110.     public static class UserConfigurationAdapter extends WebSecurityConfigurerAdapter {
  111.         @Autowired
  112.         private AccessDeniedHandler customAccessDenied;
  113.  
  114.         @Autowired
  115.         private AuthenticationEntryPoint customAuthEntryPoint;
  116.  
  117.         @Autowired
  118.         private SimpleUrlAuthenticationSuccessHandler customAuthSuccess;
  119.  
  120.         @Autowired
  121.         private SimpleUrlAuthenticationFailureHandler failureHandler;
  122.  
  123.         @Autowired
  124.         private CustomUserDetailsService userService;
  125.  
  126.         @Autowired
  127.         private SimpleUrlLogoutSuccessHandler customLogoutSuccess;
  128.  
  129.         @Autowired
  130.         private CustomOauth2UserService customOauth2UserService;
  131.  
  132.  
  133.  
  134.         @Override
  135.         protected void configure(HttpSecurity http) throws Exception {
  136.             http
  137.                     .csrf()
  138.                         .disable()
  139.                     .antMatcher("/api/**")
  140.                     .authorizeRequests()
  141.                         .antMatchers("/",
  142.                                 "/api/user/oauth2/**",
  143.                                 "/api/user/registration",
  144.                                 "/api/user/emailConfirm",
  145.                                 "/api/user/resetPassword",
  146.                                 "/api/user/profile/password/update")
  147.                         .permitAll()
  148.                         .antMatchers("/api", "/api/**")
  149.                         .authenticated()
  150.                         .and()
  151.                     .exceptionHandling()
  152.                         .accessDeniedHandler(customAccessDenied)
  153.                         .authenticationEntryPoint(customAuthEntryPoint)
  154.                         .and()
  155.                     .formLogin()
  156.                         .loginProcessingUrl("/api/user/login").permitAll()
  157.                         .successHandler(customAuthSuccess)
  158.                         .failureHandler(failureHandler)
  159.                         .usernameParameter("email")
  160.                         .and()
  161.                     .logout()
  162.                         .deleteCookies("JSESSIONID")
  163.                         .logoutUrl("/api/user/logout")
  164.                         .logoutSuccessHandler(customLogoutSuccess)
  165.                         .and()
  166.                     .rememberMe()
  167.                     .userDetailsService(userService)
  168.                     .and()
  169.                     .oauth2Login()
  170.                         .authorizationEndpoint()
  171.                             .baseUri("/api/user/oauth2/authorize")
  172.                             .and()
  173.                         .redirectionEndpoint()
  174.                             .baseUri("/api/user/oauth2/callback/*")
  175.                             .and()
  176.                         .userInfoEndpoint()
  177.                         .userService(customOauth2UserService)
  178.                     .and();
  179.         }
  180.  
  181.  
  182.         @Override
  183.         protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  184.             auth.authenticationProvider(authenticationProvider());
  185.         }
  186.  
  187.  
  188.         protected DaoAuthenticationProvider authenticationProvider() {
  189.             DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  190.             authProvider.setUserDetailsService(userService);
  191.             authProvider.setPasswordEncoder(passwordEncoder());
  192.             return authProvider;
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement