11package com .bfwg .config ;
22
3+ import static org .springframework .security .web .util .matcher .AntPathRequestMatcher .antMatcher ;
4+
35import org .springframework .beans .factory .annotation .Autowired ;
46import org .springframework .context .annotation .Bean ;
57import org .springframework .context .annotation .Configuration ;
68import org .springframework .http .HttpMethod ;
79import org .springframework .security .authentication .AuthenticationManager ;
810import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
911import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
10- import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
12+ import org .springframework .security .config .annotation .method .configuration .EnableMethodSecurity ;
1113import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1214import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1315import org .springframework .security .config .annotation .web .configuration .WebSecurityCustomizer ;
2729
2830@ Configuration
2931@ EnableWebSecurity
30- @ EnableGlobalMethodSecurity (prePostEnabled = true )
32+ @ EnableMethodSecurity (prePostEnabled = true )
3133public class WebSecurityConfig {
3234
3335 @ Autowired
@@ -41,8 +43,7 @@ public class WebSecurityConfig {
4143
4244 @ Autowired
4345 public void configureGlobal (AuthenticationManagerBuilder auth ) throws Exception {
44- auth .userDetailsService (jwtUserDetailsService )
45- .passwordEncoder (passwordEncoder );
46+ auth .userDetailsService (jwtUserDetailsService ).passwordEncoder (passwordEncoder );
4647 }
4748
4849 @ Autowired
@@ -56,46 +57,32 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration a
5657
5758 @ Bean
5859 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
59- http
60- .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS ).and ()
61- .exceptionHandling ().authenticationEntryPoint (restAuthenticationEntryPoint ).and ()
62- .authorizeRequests ()
63- .antMatchers (
64- HttpMethod .GET ,
65- "/" ,
66- "/auth/**" ,
67- "/webjars/**" ,
68- "/*.html" ,
69- "/favicon.ico" ,
70- "/**/*.html" ,
71- "/**/*.css" ,
72- "/**/*.js" )
73- .permitAll ()
74- .antMatchers ("/auth/**" ).permitAll ()
75- .anyRequest ().authenticated ().and ()
76- .addFilterBefore (new TokenAuthenticationFilter (tokenHelper , jwtUserDetailsService ),
77- BasicAuthenticationFilter .class );
7860
79- http .csrf ().disable ();
61+ http .addFilterBefore (new TokenAuthenticationFilter (tokenHelper , jwtUserDetailsService ),
62+ BasicAuthenticationFilter .class )
63+ .authorizeHttpRequests (authorize -> authorize
64+ .requestMatchers (antMatcher (HttpMethod .GET , "/" ), antMatcher (HttpMethod .GET , "/auth/**" ),
65+ antMatcher (HttpMethod .GET , "/webjars/**" ), antMatcher (HttpMethod .GET , "/*.html" ),
66+ antMatcher (HttpMethod .GET , "/favicon.ico" ), antMatcher (HttpMethod .GET , "/**/*.html" ),
67+ antMatcher (HttpMethod .GET , "/**/*.css" ), antMatcher (HttpMethod .GET , "/**/*.js" ))
68+ .permitAll ().requestMatchers ("/auth/**" ).permitAll ().anyRequest ().authenticated ())
69+ .sessionManagement (sec -> sec .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
70+ .exceptionHandling (
71+ exceptionHandler -> exceptionHandler .authenticationEntryPoint (restAuthenticationEntryPoint ))
72+ .csrf (csrf -> csrf .disable ());
73+
8074 return http .build ();
8175 }
8276
8377 @ Bean
8478 public WebSecurityCustomizer webSecurityCustomizer () {
8579 // TokenAuthenticationFilter will ignore the below paths
8680 return (web ) -> {
87- web .ignoring ().antMatchers (
88- HttpMethod .POST ,
89- "/auth/login" );
90- web .ignoring ().antMatchers (
91- HttpMethod .GET ,
92- "/" ,
93- "/webjars/**" ,
94- "/*.html" ,
95- "/favicon.ico" ,
96- "/**/*.html" ,
97- "/**/*.css" ,
98- "/**/*.js" );
81+ web .ignoring ().requestMatchers (HttpMethod .POST , "/auth/login" ).requestMatchers (
82+ antMatcher (HttpMethod .GET , "/" ), antMatcher (HttpMethod .GET , "/webjars/**" ),
83+ antMatcher (HttpMethod .GET , "/*.html" ), antMatcher (HttpMethod .GET , "/favicon.ico" ),
84+ antMatcher (HttpMethod .GET , "/**/*.html" ), antMatcher (HttpMethod .GET , "/**path/*.css" ),
85+ antMatcher (HttpMethod .GET , "/**path/*.js" ));
9986 };
10087 }
10188}
0 commit comments