Advertisement
Guest User

spring security config

a guest
Feb 2nd, 2022
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. @Configuration
  2. @EnableWebSecurity
  3. @RequiredArgsConstructor
  4. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  5.  
  6.     private final UserDetailsService userDetailsService;
  7.     private final BCryptPasswordEncoder bCryptPasswordEncoder;
  8.  
  9.     @Override
  10.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  11.         auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
  12.     }
  13.  
  14.     @Override
  15.     protected void configure(HttpSecurity http) throws Exception {
  16.         http.csrf().disable();
  17.         http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  18.         http.authorizeRequests().antMatchers("/login/**", "/api/token/refresh/**").permitAll();
  19.         http.authorizeRequests().antMatchers(GET, "/api/user/**").hasAnyAuthority("ROLE_SUPERADMIN");
  20.         http.authorizeRequests().antMatchers(POST, "/api/user/save/**").hasAnyAuthority("ROLE_ADMIN");
  21.         http.authorizeRequests().anyRequest().authenticated();
  22.         http.addFilter(new CustomAuthFilter(authenticationManagerBean()));
  23.         http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
  24.     }
  25.  
  26.     @Bean
  27.     @Override
  28.     public AuthenticationManager authenticationManagerBean() throws Exception {
  29.         return super.authenticationManagerBean();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement