@@ -78,7 +78,7 @@ void oneTimeTokenWhenCorrectTokenThenCanAuthenticate() throws Exception {
78
78
this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
79
79
.andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
80
80
81
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
81
+ String token = getLastToken () .getTokenValue ();
82
82
83
83
this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
84
84
.andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -90,7 +90,7 @@ void oneTimeTokenWhenDifferentAuthenticationUrlsThenCanAuthenticate() throws Exc
90
90
this .mvc .perform (post ("/generateurl" ).param ("username" , "user" ).with (csrf ()))
91
91
.andExpectAll (status ().isFound (), redirectedUrl ("/redirected" ));
92
92
93
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
93
+ String token = getLastToken () .getTokenValue ();
94
94
95
95
this .mvc .perform (post ("/loginprocessingurl" ).param ("token" , token ).with (csrf ()))
96
96
.andExpectAll (status ().isFound (), redirectedUrl ("/authenticated" ), authenticated ());
@@ -102,7 +102,7 @@ void oneTimeTokenWhenCorrectTokenUsedTwiceThenSecondTimeFails() throws Exception
102
102
this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
103
103
.andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
104
104
105
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
105
+ String token = getLastToken () .getTokenValue ();
106
106
107
107
this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
108
108
.andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -206,7 +206,7 @@ void oneTimeTokenWhenCustomTokenExpirationTimeSetThenAuthenticate() throws Excep
206
206
this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
207
207
.andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
208
208
209
- OneTimeToken token = TestOneTimeTokenGenerationSuccessHandler . lastToken ;
209
+ OneTimeToken token = getLastToken () ;
210
210
211
211
this .mvc .perform (post ("/login/ott" ).param ("token" , token .getTokenValue ()).with (csrf ()))
212
212
.andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -219,25 +219,37 @@ private int getCurrentMinutes(Instant expiresAt) {
219
219
return expiresMinutes - currentMinutes ;
220
220
}
221
221
222
+ private OneTimeToken getLastToken () {
223
+ OneTimeToken lastToken = this .spring .getContext ()
224
+ .getBean (TestOneTimeTokenGenerationSuccessHandler .class ).lastToken ;
225
+ return lastToken ;
226
+ }
227
+
222
228
@ Configuration (proxyBeanMethods = false )
223
229
@ EnableWebSecurity
224
230
@ Import (UserDetailsServiceConfig .class )
225
231
static class OneTimeTokenConfigWithCustomTokenExpirationTime {
226
232
227
233
@ Bean
228
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
234
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
235
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
229
236
// @formatter:off
230
237
http
231
238
.authorizeHttpRequests ((authz ) -> authz
232
239
.anyRequest ().authenticated ()
233
240
)
234
241
.oneTimeTokenLogin ((ott ) -> ott
235
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
242
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
236
243
);
237
244
// @formatter:on
238
245
return http .build ();
239
246
}
240
247
248
+ @ Bean
249
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
250
+ return new TestOneTimeTokenGenerationSuccessHandler ();
251
+ }
252
+
241
253
@ Bean
242
254
GenerateOneTimeTokenRequestResolver generateOneTimeTokenRequestResolver () {
243
255
DefaultGenerateOneTimeTokenRequestResolver delegate = new DefaultGenerateOneTimeTokenRequestResolver ();
@@ -255,19 +267,25 @@ GenerateOneTimeTokenRequestResolver generateOneTimeTokenRequestResolver() {
255
267
static class OneTimeTokenDefaultConfig {
256
268
257
269
@ Bean
258
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
270
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
271
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
259
272
// @formatter:off
260
273
http
261
274
.authorizeHttpRequests ((authz ) -> authz
262
275
.anyRequest ().authenticated ()
263
276
)
264
277
.oneTimeTokenLogin ((ott ) -> ott
265
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
278
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
266
279
);
267
280
// @formatter:on
268
281
return http .build ();
269
282
}
270
283
284
+ @ Bean
285
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
286
+ return new TestOneTimeTokenGenerationSuccessHandler ();
287
+ }
288
+
271
289
}
272
290
273
291
@ Configuration (proxyBeanMethods = false )
@@ -276,22 +294,28 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
276
294
static class OneTimeTokenDifferentUrlsConfig {
277
295
278
296
@ Bean
279
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
297
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
298
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
280
299
// @formatter:off
281
300
http
282
301
.authorizeHttpRequests ((authz ) -> authz
283
302
.anyRequest ().authenticated ()
284
303
)
285
304
.oneTimeTokenLogin ((ott ) -> ott
286
305
.tokenGeneratingUrl ("/generateurl" )
287
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler ( "/redirected" ) )
306
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
288
307
.loginProcessingUrl ("/loginprocessingurl" )
289
308
.authenticationSuccessHandler (new SimpleUrlAuthenticationSuccessHandler ("/authenticated" ))
290
309
);
291
310
// @formatter:on
292
311
return http .build ();
293
312
}
294
313
314
+ @ Bean
315
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
316
+ return new TestOneTimeTokenGenerationSuccessHandler ("/redirected" );
317
+ }
318
+
295
319
}
296
320
297
321
@ Configuration (proxyBeanMethods = false )
@@ -300,20 +324,26 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
300
324
static class OneTimeTokenFormLoginConfig {
301
325
302
326
@ Bean
303
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
327
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
328
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
304
329
// @formatter:off
305
330
http
306
331
.authorizeHttpRequests ((authz ) -> authz
307
332
.anyRequest ().authenticated ()
308
333
)
309
334
.formLogin (Customizer .withDefaults ())
310
335
.oneTimeTokenLogin ((ott ) -> ott
311
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
336
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
312
337
);
313
338
// @formatter:on
314
339
return http .build ();
315
340
}
316
341
342
+ @ Bean
343
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
344
+ return new TestOneTimeTokenGenerationSuccessHandler ();
345
+ }
346
+
317
347
}
318
348
319
349
@ Configuration (proxyBeanMethods = false )
@@ -337,7 +367,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
337
367
338
368
static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
339
369
340
- private static OneTimeToken lastToken ;
370
+ private OneTimeToken lastToken ;
341
371
342
372
private final OneTimeTokenGenerationSuccessHandler delegate ;
343
373
@@ -352,7 +382,7 @@ static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGen
352
382
@ Override
353
383
public void handle (HttpServletRequest request , HttpServletResponse response , OneTimeToken oneTimeToken )
354
384
throws IOException , ServletException {
355
- lastToken = oneTimeToken ;
385
+ this . lastToken = oneTimeToken ;
356
386
this .delegate .handle (request , response , oneTimeToken );
357
387
}
358
388
0 commit comments