@@ -256,6 +256,76 @@ public void loadConfigWhenBothAdapterAndFilterChainConfiguredThenException() {
256
256
257
257
}
258
258
259
+ @ Test
260
+ public void loadConfigWhenOnlyWebSecurityCustomizerThenDefaultFilterChainCreated () {
261
+ this .spring .register (WebSecurityCustomizerConfig .class ).autowire ();
262
+ FilterChainProxy filterChainProxy = this .spring .getContext ().getBean (FilterChainProxy .class );
263
+ List <SecurityFilterChain > filterChains = filterChainProxy .getFilterChains ();
264
+ assertThat (filterChains ).hasSize (3 );
265
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "" );
266
+ request .setServletPath ("/ignore1" );
267
+ assertThat (filterChains .get (0 ).matches (request )).isTrue ();
268
+ assertThat (filterChains .get (0 ).getFilters ()).isEmpty ();
269
+ request .setServletPath ("/ignore2" );
270
+ assertThat (filterChains .get (1 ).matches (request )).isTrue ();
271
+ assertThat (filterChains .get (1 ).getFilters ()).isEmpty ();
272
+ request .setServletPath ("/test/**" );
273
+ assertThat (filterChains .get (2 ).matches (request )).isTrue ();
274
+ }
275
+
276
+ @ Test
277
+ public void loadConfigWhenWebSecurityCustomizerAndFilterChainThenFilterChainsOrdered () {
278
+ this .spring .register (CustomizerAndFilterChainConfig .class ).autowire ();
279
+ FilterChainProxy filterChainProxy = this .spring .getContext ().getBean (FilterChainProxy .class );
280
+ List <SecurityFilterChain > filterChains = filterChainProxy .getFilterChains ();
281
+ assertThat (filterChains ).hasSize (3 );
282
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "" );
283
+ request .setServletPath ("/ignore1" );
284
+ assertThat (filterChains .get (0 ).matches (request )).isTrue ();
285
+ assertThat (filterChains .get (0 ).getFilters ()).isEmpty ();
286
+ request .setServletPath ("/ignore2" );
287
+ assertThat (filterChains .get (1 ).matches (request )).isTrue ();
288
+ assertThat (filterChains .get (1 ).getFilters ()).isEmpty ();
289
+ request .setServletPath ("/role1/**" );
290
+ assertThat (filterChains .get (2 ).matches (request )).isTrue ();
291
+ request .setServletPath ("/test/**" );
292
+ assertThat (filterChains .get (2 ).matches (request )).isFalse ();
293
+ }
294
+
295
+ @ Test
296
+ public void loadConfigWhenWebSecurityCustomizerAndWebSecurityConfigurerAdapterThenFilterChainsOrdered () {
297
+ this .spring .register (CustomizerAndAdapterConfig .class ).autowire ();
298
+ FilterChainProxy filterChainProxy = this .spring .getContext ().getBean (FilterChainProxy .class );
299
+ List <SecurityFilterChain > filterChains = filterChainProxy .getFilterChains ();
300
+ assertThat (filterChains ).hasSize (3 );
301
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "" );
302
+ request .setServletPath ("/ignore1" );
303
+ assertThat (filterChains .get (0 ).matches (request )).isTrue ();
304
+ assertThat (filterChains .get (0 ).getFilters ()).isEmpty ();
305
+ request .setServletPath ("/ignore2" );
306
+ assertThat (filterChains .get (1 ).matches (request )).isTrue ();
307
+ assertThat (filterChains .get (1 ).getFilters ()).isEmpty ();
308
+ request .setServletPath ("/role1/**" );
309
+ assertThat (filterChains .get (2 ).matches (request )).isTrue ();
310
+ request .setServletPath ("/test/**" );
311
+ assertThat (filterChains .get (2 ).matches (request )).isFalse ();
312
+ }
313
+
314
+ @ Test
315
+ public void loadConfigWhenCustomizerAndAdapterConfigureWebSecurityThenBothConfigurationsApplied () {
316
+ this .spring .register (CustomizerAndAdapterIgnoringConfig .class ).autowire ();
317
+ FilterChainProxy filterChainProxy = this .spring .getContext ().getBean (FilterChainProxy .class );
318
+ List <SecurityFilterChain > filterChains = filterChainProxy .getFilterChains ();
319
+ assertThat (filterChains ).hasSize (3 );
320
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "" );
321
+ request .setServletPath ("/ignore1" );
322
+ assertThat (filterChains .get (0 ).matches (request )).isTrue ();
323
+ assertThat (filterChains .get (0 ).getFilters ()).isEmpty ();
324
+ request .setServletPath ("/ignore2" );
325
+ assertThat (filterChains .get (1 ).matches (request )).isTrue ();
326
+ assertThat (filterChains .get (1 ).getFilters ()).isEmpty ();
327
+ }
328
+
259
329
@ EnableWebSecurity
260
330
@ Import (AuthenticationTestConfiguration .class )
261
331
static class SortedWebSecurityConfigurerAdaptersConfig {
@@ -682,4 +752,86 @@ protected void configure(HttpSecurity http) throws Exception {
682
752
683
753
}
684
754
755
+ @ EnableWebSecurity
756
+ @ Import (AuthenticationTestConfiguration .class )
757
+ static class WebSecurityCustomizerConfig {
758
+
759
+ @ Bean
760
+ public WebSecurityCustomizer webSecurityCustomizer () {
761
+ return (web ) -> web .ignoring ().antMatchers ("/ignore1" , "/ignore2" );
762
+ }
763
+
764
+ }
765
+
766
+ @ EnableWebSecurity
767
+ @ Import (AuthenticationTestConfiguration .class )
768
+ static class CustomizerAndFilterChainConfig {
769
+
770
+ @ Bean
771
+ public WebSecurityCustomizer webSecurityCustomizer () {
772
+ return (web ) -> web .ignoring ().antMatchers ("/ignore1" , "/ignore2" );
773
+ }
774
+
775
+ @ Bean
776
+ SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
777
+ // @formatter:off
778
+ return http
779
+ .antMatcher ("/role1/**" )
780
+ .authorizeRequests ((authorize ) -> authorize
781
+ .anyRequest ().hasRole ("1" )
782
+ )
783
+ .build ();
784
+ // @formatter:on
785
+ }
786
+
787
+ }
788
+
789
+ @ EnableWebSecurity
790
+ @ Import (AuthenticationTestConfiguration .class )
791
+ static class CustomizerAndAdapterConfig {
792
+
793
+ @ Bean
794
+ public WebSecurityCustomizer webSecurityCustomizer () {
795
+ return (web ) -> web .ignoring ().antMatchers ("/ignore1" , "/ignore2" );
796
+ }
797
+
798
+ @ Configuration
799
+ static class SecurityConfig extends WebSecurityConfigurerAdapter {
800
+
801
+ @ Override
802
+ protected void configure (HttpSecurity http ) throws Exception {
803
+ // @formatter:off
804
+ http
805
+ .antMatcher ("/role1/**" )
806
+ .authorizeRequests ((authorize ) -> authorize
807
+ .anyRequest ().hasRole ("1" )
808
+ );
809
+ // @formatter:on
810
+ }
811
+
812
+ }
813
+
814
+ }
815
+
816
+ @ EnableWebSecurity
817
+ @ Import (AuthenticationTestConfiguration .class )
818
+ static class CustomizerAndAdapterIgnoringConfig {
819
+
820
+ @ Bean
821
+ public WebSecurityCustomizer webSecurityCustomizer () {
822
+ return (web ) -> web .ignoring ().antMatchers ("/ignore1" );
823
+ }
824
+
825
+ @ Configuration
826
+ static class SecurityConfig extends WebSecurityConfigurerAdapter {
827
+
828
+ @ Override
829
+ public void configure (WebSecurity web ) throws Exception {
830
+ web .ignoring ().antMatchers ("/ignore2" );
831
+ }
832
+
833
+ }
834
+
835
+ }
836
+
685
837
}
0 commit comments