92
92
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
93
93
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .put ;
94
94
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .request ;
95
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
95
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .cookie ;
96
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
97
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .redirectedUrl ;
98
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
96
99
97
100
/**
98
101
* Tests for {@link CsrfConfigurer}
@@ -114,72 +117,72 @@ public class CsrfConfigurerTests {
114
117
@ Test
115
118
public void postWhenWebSecurityEnabledThenRespondsWithForbidden () throws Exception {
116
119
this .spring
117
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
118
- .autowire ();
120
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
121
+ .autowire ();
119
122
this .mvc .perform (post ("/" )).andExpect (status ().isForbidden ());
120
123
}
121
124
122
125
@ Test
123
126
public void putWhenWebSecurityEnabledThenRespondsWithForbidden () throws Exception {
124
127
this .spring
125
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
126
- .autowire ();
128
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
129
+ .autowire ();
127
130
this .mvc .perform (put ("/" )).andExpect (status ().isForbidden ());
128
131
}
129
132
130
133
@ Test
131
134
public void patchWhenWebSecurityEnabledThenRespondsWithForbidden () throws Exception {
132
135
this .spring
133
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
134
- .autowire ();
136
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
137
+ .autowire ();
135
138
this .mvc .perform (patch ("/" )).andExpect (status ().isForbidden ());
136
139
}
137
140
138
141
@ Test
139
142
public void deleteWhenWebSecurityEnabledThenRespondsWithForbidden () throws Exception {
140
143
this .spring
141
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
142
- .autowire ();
144
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
145
+ .autowire ();
143
146
this .mvc .perform (delete ("/" )).andExpect (status ().isForbidden ());
144
147
}
145
148
146
149
@ Test
147
150
public void invalidWhenWebSecurityEnabledThenRespondsWithForbidden () throws Exception {
148
151
this .spring
149
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
150
- .autowire ();
152
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
153
+ .autowire ();
151
154
this .mvc .perform (request ("INVALID" , URI .create ("/" ))).andExpect (status ().isForbidden ());
152
155
}
153
156
154
157
@ Test
155
158
public void getWhenWebSecurityEnabledThenRespondsWithOk () throws Exception {
156
159
this .spring
157
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
158
- .autowire ();
160
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
161
+ .autowire ();
159
162
this .mvc .perform (get ("/" )).andExpect (status ().isOk ());
160
163
}
161
164
162
165
@ Test
163
166
public void headWhenWebSecurityEnabledThenRespondsWithOk () throws Exception {
164
167
this .spring
165
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
166
- .autowire ();
168
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
169
+ .autowire ();
167
170
this .mvc .perform (head ("/" )).andExpect (status ().isOk ());
168
171
}
169
172
170
173
@ Test
171
174
public void traceWhenWebSecurityEnabledThenRespondsWithOk () throws Exception {
172
175
this .spring
173
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
174
- .autowire ();
176
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
177
+ .autowire ();
175
178
this .mvc .perform (request (HttpMethod .TRACE , "/" )).andExpect (status ().isOk ());
176
179
}
177
180
178
181
@ Test
179
182
public void optionsWhenWebSecurityEnabledThenRespondsWithOk () throws Exception {
180
183
this .spring
181
- .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
182
- .autowire ();
184
+ .register (CsrfAppliedDefaultConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
185
+ .autowire ();
183
186
this .mvc .perform (options ("/" )).andExpect (status ().isOk ());
184
187
}
185
188
@@ -209,11 +212,11 @@ public void loginWhenCsrfDisabledThenRedirectsToPreviousPostRequest() throws Exc
209
212
RequestCache requestCache = new HttpSessionRequestCache ();
210
213
String redirectUrl = requestCache .getRequest (mvcResult .getRequest (), mvcResult .getResponse ()).getRedirectUrl ();
211
214
this .mvc
212
- .perform (post ("/login" ).param ("username" , "user" )
213
- .param ("password" , "password" )
214
- .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
215
- .andExpect (status ().isFound ())
216
- .andExpect (redirectedUrl (redirectUrl ));
215
+ .perform (post ("/login" ).param ("username" , "user" )
216
+ .param ("password" , "password" )
217
+ .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
218
+ .andExpect (status ().isFound ())
219
+ .andExpect (redirectedUrl (redirectUrl ));
217
220
}
218
221
219
222
@ Test
@@ -222,18 +225,18 @@ public void loginWhenCsrfEnabledThenDoesNotRedirectToPreviousPostRequest() throw
222
225
DefaultCsrfToken csrfToken = new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" );
223
226
given (CsrfDisablesPostRequestFromRequestCacheConfig .REPO .loadDeferredToken (any (HttpServletRequest .class ),
224
227
any (HttpServletResponse .class )))
225
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
228
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
226
229
this .spring .register (CsrfDisablesPostRequestFromRequestCacheConfig .class ).autowire ();
227
230
MvcResult mvcResult = this .mvc .perform (post ("/some-url" )).andReturn ();
228
231
this .mvc
229
- .perform (post ("/login" ).param ("username" , "user" )
230
- .param ("password" , "password" )
231
- .with (csrf ())
232
- .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
233
- .andExpect (status ().isFound ())
234
- .andExpect (redirectedUrl ("/" ));
232
+ .perform (post ("/login" ).param ("username" , "user" )
233
+ .param ("password" , "password" )
234
+ .with (csrf ())
235
+ .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
236
+ .andExpect (status ().isFound ())
237
+ .andExpect (redirectedUrl ("/" ));
235
238
verify (CsrfDisablesPostRequestFromRequestCacheConfig .REPO , atLeastOnce ())
236
- .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
239
+ .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
237
240
}
238
241
239
242
@ Test
@@ -242,32 +245,32 @@ public void loginWhenCsrfEnabledThenRedirectsToPreviousGetRequest() throws Excep
242
245
DefaultCsrfToken csrfToken = new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" );
243
246
given (CsrfDisablesPostRequestFromRequestCacheConfig .REPO .loadDeferredToken (any (HttpServletRequest .class ),
244
247
any (HttpServletResponse .class )))
245
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
248
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
246
249
this .spring .register (CsrfDisablesPostRequestFromRequestCacheConfig .class ).autowire ();
247
250
MvcResult mvcResult = this .mvc .perform (get ("/some-url" )).andReturn ();
248
251
RequestCache requestCache = new HttpSessionRequestCache ();
249
252
String redirectUrl = requestCache .getRequest (mvcResult .getRequest (), mvcResult .getResponse ()).getRedirectUrl ();
250
253
this .mvc
251
- .perform (post ("/login" ).param ("username" , "user" )
252
- .param ("password" , "password" )
253
- .with (csrf ())
254
- .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
255
- .andExpect (status ().isFound ())
256
- .andExpect (redirectedUrl (redirectUrl ));
254
+ .perform (post ("/login" ).param ("username" , "user" )
255
+ .param ("password" , "password" )
256
+ .with (csrf ())
257
+ .session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
258
+ .andExpect (status ().isFound ())
259
+ .andExpect (redirectedUrl (redirectUrl ));
257
260
verify (CsrfDisablesPostRequestFromRequestCacheConfig .REPO , atLeastOnce ())
258
- .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
261
+ .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
259
262
}
260
263
261
264
// SEC-2422
262
265
@ Test
263
266
public void postWhenCsrfEnabledAndSessionIsExpiredThenRespondsWithForbidden () throws Exception {
264
267
this .spring .register (InvalidSessionUrlConfig .class ).autowire ();
265
268
MvcResult mvcResult = this .mvc .perform (post ("/" ).param ("_csrf" , "abc" ))
266
- .andExpect (status ().isFound ())
267
- .andExpect (redirectedUrl ("/error/sessionError" ))
268
- .andReturn ();
269
+ .andExpect (status ().isFound ())
270
+ .andExpect (redirectedUrl ("/error/sessionError" ))
271
+ .andReturn ();
269
272
this .mvc .perform (post ("/" ).session ((MockHttpSession ) mvcResult .getRequest ().getSession ()))
270
- .andExpect (status ().isForbidden ());
273
+ .andExpect (status ().isForbidden ());
271
274
}
272
275
273
276
@ Test
@@ -306,7 +309,7 @@ public void postWhenCustomCsrfTokenRepositoryThenRepositoryIsUsed() throws Excep
306
309
CsrfTokenRepositoryConfig .REPO = mock (CsrfTokenRepository .class );
307
310
given (CsrfTokenRepositoryConfig .REPO .loadDeferredToken (any (HttpServletRequest .class ),
308
311
any (HttpServletResponse .class )))
309
- .willReturn (new TestDeferredCsrfToken (new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" )));
312
+ .willReturn (new TestDeferredCsrfToken (new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" )));
310
313
this .spring .register (CsrfTokenRepositoryConfig .class , BasicController .class ).autowire ();
311
314
this .mvc .perform (post ("/" ));
312
315
verify (CsrfTokenRepositoryConfig .REPO ).loadDeferredToken (any (HttpServletRequest .class ),
@@ -329,7 +332,7 @@ public void loginWhenCustomCsrfTokenRepositoryThenCsrfTokenIsCleared() throws Ex
329
332
given (CsrfTokenRepositoryConfig .REPO .loadToken (any ())).willReturn (csrfToken );
330
333
given (CsrfTokenRepositoryConfig .REPO .loadDeferredToken (any (HttpServletRequest .class ),
331
334
any (HttpServletResponse .class )))
332
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
335
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
333
336
this .spring .register (CsrfTokenRepositoryConfig .class , BasicController .class ).autowire ();
334
337
// @formatter:off
335
338
MockHttpServletRequestBuilder loginRequest = post ("/login" )
@@ -348,7 +351,7 @@ public void getWhenCustomCsrfTokenRepositoryInLambdaThenRepositoryIsUsed() throw
348
351
CsrfTokenRepositoryInLambdaConfig .REPO = mock (CsrfTokenRepository .class );
349
352
given (CsrfTokenRepositoryInLambdaConfig .REPO .loadDeferredToken (any (HttpServletRequest .class ),
350
353
any (HttpServletResponse .class )))
351
- .willReturn (new TestDeferredCsrfToken (new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" )));
354
+ .willReturn (new TestDeferredCsrfToken (new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" )));
352
355
this .spring .register (CsrfTokenRepositoryInLambdaConfig .class , BasicController .class ).autowire ();
353
356
this .mvc .perform (post ("/" ));
354
357
verify (CsrfTokenRepositoryInLambdaConfig .REPO ).loadDeferredToken (any (HttpServletRequest .class ),
@@ -418,8 +421,8 @@ public void logoutWhenGetRequestAndGetEnabledForLogoutThenLogsOut() throws Excep
418
421
@ Test
419
422
public void configureWhenRequireCsrfProtectionMatcherNullThenException () {
420
423
assertThatExceptionOfType (BeanCreationException .class )
421
- .isThrownBy (() -> this .spring .register (NullRequireCsrfProtectionMatcherConfig .class ).autowire ())
422
- .withRootCauseInstanceOf (IllegalArgumentException .class );
424
+ .isThrownBy (() -> this .spring .register (NullRequireCsrfProtectionMatcherConfig .class ).autowire ())
425
+ .withRootCauseInstanceOf (IllegalArgumentException .class );
423
426
}
424
427
425
428
@ Test
@@ -432,8 +435,8 @@ public void getWhenDefaultCsrfTokenRepositoryThenDoesNotCreateSession() throws E
432
435
@ Test
433
436
public void getWhenNullAuthenticationStrategyThenException () {
434
437
assertThatExceptionOfType (BeanCreationException .class )
435
- .isThrownBy (() -> this .spring .register (NullAuthenticationStrategy .class ).autowire ())
436
- .withRootCauseInstanceOf (IllegalArgumentException .class );
438
+ .isThrownBy (() -> this .spring .register (NullAuthenticationStrategy .class ).autowire ())
439
+ .withRootCauseInstanceOf (IllegalArgumentException .class );
437
440
}
438
441
439
442
@ Test
@@ -456,13 +459,13 @@ public void getLoginWhenCsrfTokenRequestAttributeHandlerSetThenRespondsWithNorma
456
459
CsrfTokenRepository csrfTokenRepository = mock (CsrfTokenRepository .class );
457
460
CsrfToken csrfToken = new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" );
458
461
given (csrfTokenRepository .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class )))
459
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
462
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
460
463
CsrfTokenRequestHandlerConfig .REPO = csrfTokenRepository ;
461
464
CsrfTokenRequestHandlerConfig .HANDLER = new CsrfTokenRequestAttributeHandler ();
462
465
this .spring .register (CsrfTokenRequestHandlerConfig .class , BasicController .class ).autowire ();
463
466
this .mvc .perform (get ("/login" ))
464
- .andExpect (status ().isOk ())
465
- .andExpect (content ().string (containsString (csrfToken .getToken ())));
467
+ .andExpect (status ().isOk ())
468
+ .andExpect (content ().string (containsString (csrfToken .getToken ())));
466
469
verify (csrfTokenRepository ).loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
467
470
verifyNoMoreInteractions (csrfTokenRepository );
468
471
}
@@ -473,7 +476,7 @@ public void loginWhenCsrfTokenRequestAttributeHandlerSetAndNormalCsrfTokenThenSu
473
476
CsrfTokenRepository csrfTokenRepository = mock (CsrfTokenRepository .class );
474
477
given (csrfTokenRepository .loadToken (any (HttpServletRequest .class ))).willReturn (csrfToken );
475
478
given (csrfTokenRepository .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class )))
476
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
479
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
477
480
CsrfTokenRequestHandlerConfig .REPO = csrfTokenRepository ;
478
481
CsrfTokenRequestHandlerConfig .HANDLER = new CsrfTokenRequestAttributeHandler ();
479
482
this .spring .register (CsrfTokenRequestHandlerConfig .class , BasicController .class ).autowire ();
@@ -497,13 +500,13 @@ public void getLoginWhenXorCsrfTokenRequestAttributeHandlerSetThenRespondsWithMa
497
500
CsrfTokenRepository csrfTokenRepository = mock (CsrfTokenRepository .class );
498
501
CsrfToken csrfToken = new DefaultCsrfToken ("X-CSRF-TOKEN" , "_csrf" , "token" );
499
502
given (csrfTokenRepository .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class )))
500
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
503
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
501
504
CsrfTokenRequestHandlerConfig .REPO = csrfTokenRepository ;
502
505
CsrfTokenRequestHandlerConfig .HANDLER = new XorCsrfTokenRequestAttributeHandler ();
503
506
this .spring .register (CsrfTokenRequestHandlerConfig .class , BasicController .class ).autowire ();
504
507
this .mvc .perform (get ("/login" ))
505
- .andExpect (status ().isOk ())
506
- .andExpect (content ().string (not (containsString (csrfToken .getToken ()))));
508
+ .andExpect (status ().isOk ())
509
+ .andExpect (content ().string (not (containsString (csrfToken .getToken ()))));
507
510
verify (csrfTokenRepository ).loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class ));
508
511
verifyNoMoreInteractions (csrfTokenRepository );
509
512
}
@@ -514,7 +517,7 @@ public void loginWhenXorCsrfTokenRequestAttributeHandlerSetAndMaskedCsrfTokenThe
514
517
CsrfTokenRepository csrfTokenRepository = mock (CsrfTokenRepository .class );
515
518
given (csrfTokenRepository .loadToken (any (HttpServletRequest .class ))).willReturn (csrfToken );
516
519
given (csrfTokenRepository .loadDeferredToken (any (HttpServletRequest .class ), any (HttpServletResponse .class )))
517
- .willReturn (new TestDeferredCsrfToken (csrfToken ));
520
+ .willReturn (new TestDeferredCsrfToken (csrfToken ));
518
521
CsrfTokenRequestHandlerConfig .REPO = csrfTokenRepository ;
519
522
CsrfTokenRequestHandlerConfig .HANDLER = new XorCsrfTokenRequestAttributeHandler ();
520
523
this .spring .register (CsrfTokenRequestHandlerConfig .class , BasicController .class ).autowire ();
@@ -576,8 +579,8 @@ public void postWhenHttpBasicAndCookieCsrfTokenRepositorySetAndExistingTokenThen
576
579
headers .setBasicAuth ("user" , "password" );
577
580
// @formatter:off
578
581
MvcResult mvcResult = this .mvc .perform (post ("/" )
579
- .cookie (existingCookie )
580
- .headers (headers ))
582
+ .cookie (existingCookie )
583
+ .headers (headers ))
581
584
.andExpect (status ().isOk ())
582
585
.andReturn ();
583
586
// @formatter:on
@@ -602,7 +605,7 @@ public void getWhenHttpBasicAndCookieCsrfTokenRepositorySetAndNoExistingCookieTh
602
605
headers .setBasicAuth ("user" , "password" );
603
606
// @formatter:off
604
607
MvcResult mvcResult = this .mvc .perform (get ("/" )
605
- .headers (headers ))
608
+ .headers (headers ))
606
609
.andExpect (status ().isOk ())
607
610
.andReturn ();
608
611
// @formatter:on
@@ -613,35 +616,33 @@ public void getWhenHttpBasicAndCookieCsrfTokenRepositorySetAndNoExistingCookieTh
613
616
614
617
@ Test
615
618
public void spaConfigForbidden () throws Exception {
616
- this .spring
617
- .register (CsrfSpaConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
618
- .autowire ();
619
+ this .spring .register (CsrfSpaConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
620
+ .autowire ();
619
621
this .mvc .perform (post ("/" )).andExpect (status ().isForbidden ());
620
622
}
621
623
622
624
@ Test
623
625
public void spaConfigOk () throws Exception {
624
- this .spring
625
- .register (CsrfSpaConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
626
- .autowire ();
626
+ this .spring .register (CsrfSpaConfig .class , AllowHttpMethodsFirewallConfig .class , BasicController .class )
627
+ .autowire ();
627
628
this .mvc .perform (post ("/" ).with (csrf ())).andExpect (status ().isOk ());
628
629
}
629
630
630
631
@ Test
631
632
public void spaConfigDoubleSubmit () throws Exception {
632
- this .spring
633
- . register ( CsrfSpaConfig . class , AllowHttpMethodsFirewallConfig . class , BasicController . class )
634
- . autowire ();
635
- var token = this . mvc
636
- . perform ( post ( "/ " ))
637
- . andExpect ( status (). isForbidden () )
638
- . andExpect ( cookie (). exists ( "XSRF-TOKEN" ) )
639
- . andReturn (). getResponse () .getCookie ("XSRF-TOKEN" );
633
+ this .spring . register ( CsrfSpaConfig . class , AllowHttpMethodsFirewallConfig . class , BasicController . class )
634
+ . autowire ();
635
+ var token = this . mvc . perform ( post ( "/" ))
636
+ . andExpect ( status (). isForbidden ())
637
+ . andExpect ( cookie (). exists ( "XSRF-TOKEN " ))
638
+ . andReturn ( )
639
+ . getResponse ( )
640
+ .getCookie ("XSRF-TOKEN" );
640
641
641
- this .mvc . perform ( post ( "/" )
642
- .header ("X-XSRF-TOKEN" , token .getValue ())
643
- .cookie (new Cookie ("XSRF-TOKEN" , token .getValue ())))
644
- .andExpect (status ().isOk ());
642
+ this .mvc
643
+ . perform ( post ( "/" ) .header ("X-XSRF-TOKEN" , token .getValue ())
644
+ .cookie (new Cookie ("XSRF-TOKEN" , token .getValue ())))
645
+ .andExpect (status ().isOk ());
645
646
}
646
647
647
648
@ Configuration
@@ -675,7 +676,7 @@ static class DisableCsrfConfig {
675
676
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
676
677
// @formatter:off
677
678
http
678
- .csrf ()
679
+ .csrf ()
679
680
.disable ();
680
681
return http .build ();
681
682
// @formatter:on
@@ -691,7 +692,7 @@ static class DisableCsrfInLambdaConfig {
691
692
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
692
693
// @formatter:off
693
694
http
694
- .csrf (AbstractHttpConfigurer ::disable );
695
+ .csrf (AbstractHttpConfigurer ::disable );
695
696
return http .build ();
696
697
// @formatter:on
697
698
}
@@ -706,12 +707,12 @@ static class DisableCsrfEnablesRequestCacheConfig {
706
707
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
707
708
// @formatter:off
708
709
http
709
- .authorizeRequests ()
710
+ .authorizeRequests ()
710
711
.anyRequest ().authenticated ()
711
712
.and ()
712
- .formLogin ()
713
+ .formLogin ()
713
714
.and ()
714
- .csrf ()
715
+ .csrf ()
715
716
.disable ();
716
717
// @formatter:on
717
718
return http .build ();
@@ -734,12 +735,12 @@ static class CsrfDisablesPostRequestFromRequestCacheConfig {
734
735
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
735
736
// @formatter:off
736
737
http
737
- .authorizeRequests ()
738
+ .authorizeRequests ()
738
739
.anyRequest ().authenticated ()
739
740
.and ()
740
- .formLogin ()
741
+ .formLogin ()
741
742
.and ()
742
- .csrf ()
743
+ .csrf ()
743
744
.csrfTokenRepository (REPO );
744
745
// @formatter:on
745
746
return http .build ();
@@ -760,9 +761,9 @@ static class InvalidSessionUrlConfig {
760
761
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
761
762
// @formatter:off
762
763
http
763
- .csrf ()
764
+ .csrf ()
764
765
.and ()
765
- .sessionManagement ()
766
+ .sessionManagement ()
766
767
.invalidSessionUrl ("/error/sessionError" );
767
768
return http .build ();
768
769
// @formatter:on
@@ -780,7 +781,7 @@ static class RequireCsrfProtectionMatcherConfig {
780
781
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
781
782
// @formatter:off
782
783
http
783
- .csrf ()
784
+ .csrf ()
784
785
.requireCsrfProtectionMatcher (MATCHER );
785
786
return http .build ();
786
787
// @formatter:on
@@ -798,7 +799,7 @@ static class RequireCsrfProtectionMatcherInLambdaConfig {
798
799
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
799
800
// @formatter:off
800
801
http
801
- .csrf ((csrf ) -> csrf .requireCsrfProtectionMatcher (MATCHER ));
802
+ .csrf ((csrf ) -> csrf .requireCsrfProtectionMatcher (MATCHER ));
802
803
return http .build ();
803
804
// @formatter:on
804
805
}
@@ -815,9 +816,9 @@ static class CsrfTokenRepositoryConfig {
815
816
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
816
817
// @formatter:off
817
818
http
818
- .formLogin ()
819
+ .formLogin ()
819
820
.and ()
820
- .csrf ()
821
+ .csrf ()
821
822
.csrfTokenRepository (REPO );
822
823
// @formatter:on
823
824
return http .build ();
@@ -840,8 +841,8 @@ static class CsrfTokenRepositoryInLambdaConfig {
840
841
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
841
842
// @formatter:off
842
843
http
843
- .formLogin (withDefaults ())
844
- .csrf ((csrf ) -> csrf .csrfTokenRepository (REPO ));
844
+ .formLogin (withDefaults ())
845
+ .csrf ((csrf ) -> csrf .csrfTokenRepository (REPO ));
845
846
return http .build ();
846
847
// @formatter:on
847
848
}
@@ -858,7 +859,7 @@ static class AccessDeniedHandlerConfig {
858
859
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
859
860
// @formatter:off
860
861
http
861
- .exceptionHandling ()
862
+ .exceptionHandling ()
862
863
.accessDeniedHandler (DENIED_HANDLER );
863
864
return http .build ();
864
865
// @formatter:on
@@ -878,7 +879,7 @@ static class DefaultAccessDeniedHandlerForConfig {
878
879
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
879
880
// @formatter:off
880
881
http
881
- .exceptionHandling ()
882
+ .exceptionHandling ()
882
883
.defaultAccessDeniedHandlerFor (DENIED_HANDLER , MATCHER );
883
884
return http .build ();
884
885
// @formatter:on
@@ -894,7 +895,7 @@ static class FormLoginConfig {
894
895
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
895
896
// @formatter:off
896
897
http
897
- .formLogin ();
898
+ .formLogin ();
898
899
return http .build ();
899
900
// @formatter:on
900
901
}
@@ -909,9 +910,9 @@ static class LogoutAllowsGetConfig {
909
910
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
910
911
// @formatter:off
911
912
http
912
- .formLogin ()
913
+ .formLogin ()
913
914
.and ()
914
- .logout ()
915
+ .logout ()
915
916
.logoutRequestMatcher (new AntPathRequestMatcher ("/logout" ));
916
917
return http .build ();
917
918
// @formatter:on
@@ -927,7 +928,7 @@ static class NullRequireCsrfProtectionMatcherConfig {
927
928
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
928
929
// @formatter:off
929
930
http
930
- .csrf ()
931
+ .csrf ()
931
932
.requireCsrfProtectionMatcher (null );
932
933
return http .build ();
933
934
// @formatter:on
@@ -943,12 +944,12 @@ static class DefaultDoesNotCreateSession {
943
944
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
944
945
// @formatter:off
945
946
http
946
- .authorizeRequests ()
947
+ .authorizeRequests ()
947
948
.anyRequest ().permitAll ()
948
949
.and ()
949
- .formLogin ()
950
+ .formLogin ()
950
951
.and ()
951
- .httpBasic ();
952
+ .httpBasic ();
952
953
// @formatter:on
953
954
return http .build ();
954
955
}
@@ -1013,14 +1014,14 @@ static class CsrfTokenRequestHandlerConfig {
1013
1014
SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
1014
1015
// @formatter:off
1015
1016
http
1016
- .authorizeHttpRequests ((authorize ) -> authorize
1017
- .anyRequest ().authenticated ()
1018
- )
1019
- .formLogin (Customizer .withDefaults ())
1020
- .csrf ((csrf ) -> csrf
1021
- .csrfTokenRepository (REPO )
1022
- .csrfTokenRequestHandler (HANDLER )
1023
- );
1017
+ .authorizeHttpRequests ((authorize ) -> authorize
1018
+ .anyRequest ().authenticated ()
1019
+ )
1020
+ .formLogin (Customizer .withDefaults ())
1021
+ .csrf ((csrf ) -> csrf
1022
+ .csrfTokenRepository (REPO )
1023
+ .csrfTokenRequestHandler (HANDLER )
1024
+ );
1024
1025
// @formatter:on
1025
1026
1026
1027
return http .build ();
@@ -1043,11 +1044,11 @@ static class CsrfSpaConfig {
1043
1044
1044
1045
@ Bean
1045
1046
SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
1046
- http .csrf (CsrfCustomizer . spaDefaults () );
1047
+ http .csrf (CsrfConfigurer :: spa );
1047
1048
return http .build ();
1048
1049
}
1049
- }
1050
1050
1051
+ }
1051
1052
1052
1053
@ Configuration
1053
1054
@ EnableWebSecurity
@@ -1061,14 +1062,14 @@ static class HttpBasicCsrfTokenRequestHandlerConfig {
1061
1062
SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
1062
1063
// @formatter:off
1063
1064
http
1064
- .authorizeHttpRequests ((authorize ) -> authorize
1065
- .anyRequest ().authenticated ()
1066
- )
1067
- .httpBasic (Customizer .withDefaults ())
1068
- .csrf ((csrf ) -> csrf
1069
- .csrfTokenRepository (REPO )
1070
- .csrfTokenRequestHandler (HANDLER )
1071
- );
1065
+ .authorizeHttpRequests ((authorize ) -> authorize
1066
+ .anyRequest ().authenticated ()
1067
+ )
1068
+ .httpBasic (Customizer .withDefaults ())
1069
+ .csrf ((csrf ) -> csrf
1070
+ .csrfTokenRepository (REPO )
1071
+ .csrfTokenRequestHandler (HANDLER )
1072
+ );
1072
1073
// @formatter:on
1073
1074
1074
1075
return http .build ();
@@ -1078,7 +1079,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1078
1079
void configure (AuthenticationManagerBuilder auth ) throws Exception {
1079
1080
// @formatter:off
1080
1081
auth
1081
- .inMemoryAuthentication ()
1082
+ .inMemoryAuthentication ()
1082
1083
.withUser (PasswordEncodedUser .user ());
1083
1084
// @formatter:on
1084
1085
}
0 commit comments