@@ -72,7 +72,7 @@ void oneTimeTokenWhenCorrectTokenThenCanAuthenticate() throws Exception {
72
72
this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
73
73
.andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
74
74
75
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
75
+ String token = getLastToken () .getTokenValue ();
76
76
77
77
this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
78
78
.andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -84,7 +84,7 @@ void oneTimeTokenWhenDifferentAuthenticationUrlsThenCanAuthenticate() throws Exc
84
84
this .mvc .perform (post ("/generateurl" ).param ("username" , "user" ).with (csrf ()))
85
85
.andExpectAll (status ().isFound (), redirectedUrl ("/redirected" ));
86
86
87
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
87
+ String token = getLastToken () .getTokenValue ();
88
88
89
89
this .mvc .perform (post ("/loginprocessingurl" ).param ("token" , token ).with (csrf ()))
90
90
.andExpectAll (status ().isFound (), redirectedUrl ("/authenticated" ), authenticated ());
@@ -96,7 +96,7 @@ void oneTimeTokenWhenCorrectTokenUsedTwiceThenSecondTimeFails() throws Exception
96
96
this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
97
97
.andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
98
98
99
- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
99
+ String token = getLastToken () .getTokenValue ();
100
100
101
101
this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
102
102
.andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -194,25 +194,37 @@ Please provide it as a bean or pass it to the oneTimeTokenLogin() DSL.
194
194
""" );
195
195
}
196
196
197
+ private OneTimeToken getLastToken () {
198
+ OneTimeToken lastToken = this .spring .getContext ()
199
+ .getBean (TestOneTimeTokenGenerationSuccessHandler .class ).lastToken ;
200
+ return lastToken ;
201
+ }
202
+
197
203
@ Configuration (proxyBeanMethods = false )
198
204
@ EnableWebSecurity
199
205
@ Import (UserDetailsServiceConfig .class )
200
206
static class OneTimeTokenDefaultConfig {
201
207
202
208
@ Bean
203
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
209
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
210
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
204
211
// @formatter:off
205
212
http
206
213
.authorizeHttpRequests ((authz ) -> authz
207
214
.anyRequest ().authenticated ()
208
215
)
209
216
.oneTimeTokenLogin ((ott ) -> ott
210
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
217
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
211
218
);
212
219
// @formatter:on
213
220
return http .build ();
214
221
}
215
222
223
+ @ Bean
224
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
225
+ return new TestOneTimeTokenGenerationSuccessHandler ();
226
+ }
227
+
216
228
}
217
229
218
230
@ Configuration (proxyBeanMethods = false )
@@ -221,22 +233,28 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
221
233
static class OneTimeTokenDifferentUrlsConfig {
222
234
223
235
@ Bean
224
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
236
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
237
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
225
238
// @formatter:off
226
239
http
227
240
.authorizeHttpRequests ((authz ) -> authz
228
241
.anyRequest ().authenticated ()
229
242
)
230
243
.oneTimeTokenLogin ((ott ) -> ott
231
244
.tokenGeneratingUrl ("/generateurl" )
232
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler ( "/redirected" ) )
245
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
233
246
.loginProcessingUrl ("/loginprocessingurl" )
234
247
.authenticationSuccessHandler (new SimpleUrlAuthenticationSuccessHandler ("/authenticated" ))
235
248
);
236
249
// @formatter:on
237
250
return http .build ();
238
251
}
239
252
253
+ @ Bean
254
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
255
+ return new TestOneTimeTokenGenerationSuccessHandler ("/redirected" );
256
+ }
257
+
240
258
}
241
259
242
260
@ Configuration (proxyBeanMethods = false )
@@ -245,20 +263,26 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
245
263
static class OneTimeTokenFormLoginConfig {
246
264
247
265
@ Bean
248
- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
266
+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
267
+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
249
268
// @formatter:off
250
269
http
251
270
.authorizeHttpRequests ((authz ) -> authz
252
271
.anyRequest ().authenticated ()
253
272
)
254
273
.formLogin (Customizer .withDefaults ())
255
274
.oneTimeTokenLogin ((ott ) -> ott
256
- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
275
+ .tokenGenerationSuccessHandler (ottSuccessHandler )
257
276
);
258
277
// @formatter:on
259
278
return http .build ();
260
279
}
261
280
281
+ @ Bean
282
+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
283
+ return new TestOneTimeTokenGenerationSuccessHandler ();
284
+ }
285
+
262
286
}
263
287
264
288
@ Configuration (proxyBeanMethods = false )
@@ -282,7 +306,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
282
306
283
307
static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
284
308
285
- private static OneTimeToken lastToken ;
309
+ private OneTimeToken lastToken ;
286
310
287
311
private final OneTimeTokenGenerationSuccessHandler delegate ;
288
312
@@ -297,7 +321,7 @@ static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGen
297
321
@ Override
298
322
public void handle (HttpServletRequest request , HttpServletResponse response , OneTimeToken oneTimeToken )
299
323
throws IOException , ServletException {
300
- lastToken = oneTimeToken ;
324
+ this . lastToken = oneTimeToken ;
301
325
this .delegate .handle (request , response , oneTimeToken );
302
326
}
303
327
0 commit comments