@@ -72,7 +72,7 @@ void oneTimeTokenWhenCorrectTokenThenCanAuthenticate() throws Exception {
7272 this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
7373 .andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
7474
75- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
75+ String token = getLastToken () .getTokenValue ();
7676
7777 this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
7878 .andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -84,7 +84,7 @@ void oneTimeTokenWhenDifferentAuthenticationUrlsThenCanAuthenticate() throws Exc
8484 this .mvc .perform (post ("/generateurl" ).param ("username" , "user" ).with (csrf ()))
8585 .andExpectAll (status ().isFound (), redirectedUrl ("/redirected" ));
8686
87- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
87+ String token = getLastToken () .getTokenValue ();
8888
8989 this .mvc .perform (post ("/loginprocessingurl" ).param ("token" , token ).with (csrf ()))
9090 .andExpectAll (status ().isFound (), redirectedUrl ("/authenticated" ), authenticated ());
@@ -96,7 +96,7 @@ void oneTimeTokenWhenCorrectTokenUsedTwiceThenSecondTimeFails() throws Exception
9696 this .mvc .perform (post ("/ott/generate" ).param ("username" , "user" ).with (csrf ()))
9797 .andExpectAll (status ().isFound (), redirectedUrl ("/login/ott" ));
9898
99- String token = TestOneTimeTokenGenerationSuccessHandler . lastToken .getTokenValue ();
99+ String token = getLastToken () .getTokenValue ();
100100
101101 this .mvc .perform (post ("/login/ott" ).param ("token" , token ).with (csrf ()))
102102 .andExpectAll (status ().isFound (), redirectedUrl ("/" ), authenticated ());
@@ -194,25 +194,37 @@ Please provide it as a bean or pass it to the oneTimeTokenLogin() DSL.
194194 """ );
195195 }
196196
197+ private OneTimeToken getLastToken () {
198+ OneTimeToken lastToken = this .spring .getContext ()
199+ .getBean (TestOneTimeTokenGenerationSuccessHandler .class ).lastToken ;
200+ return lastToken ;
201+ }
202+
197203 @ Configuration (proxyBeanMethods = false )
198204 @ EnableWebSecurity
199205 @ Import (UserDetailsServiceConfig .class )
200206 static class OneTimeTokenDefaultConfig {
201207
202208 @ Bean
203- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
209+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
210+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
204211 // @formatter:off
205212 http
206213 .authorizeHttpRequests ((authz ) -> authz
207214 .anyRequest ().authenticated ()
208215 )
209216 .oneTimeTokenLogin ((ott ) -> ott
210- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
217+ .tokenGenerationSuccessHandler (ottSuccessHandler )
211218 );
212219 // @formatter:on
213220 return http .build ();
214221 }
215222
223+ @ Bean
224+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
225+ return new TestOneTimeTokenGenerationSuccessHandler ();
226+ }
227+
216228 }
217229
218230 @ Configuration (proxyBeanMethods = false )
@@ -221,22 +233,28 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
221233 static class OneTimeTokenDifferentUrlsConfig {
222234
223235 @ Bean
224- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
236+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
237+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
225238 // @formatter:off
226239 http
227240 .authorizeHttpRequests ((authz ) -> authz
228241 .anyRequest ().authenticated ()
229242 )
230243 .oneTimeTokenLogin ((ott ) -> ott
231244 .tokenGeneratingUrl ("/generateurl" )
232- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler ( "/redirected" ) )
245+ .tokenGenerationSuccessHandler (ottSuccessHandler )
233246 .loginProcessingUrl ("/loginprocessingurl" )
234247 .authenticationSuccessHandler (new SimpleUrlAuthenticationSuccessHandler ("/authenticated" ))
235248 );
236249 // @formatter:on
237250 return http .build ();
238251 }
239252
253+ @ Bean
254+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
255+ return new TestOneTimeTokenGenerationSuccessHandler ("/redirected" );
256+ }
257+
240258 }
241259
242260 @ Configuration (proxyBeanMethods = false )
@@ -245,20 +263,26 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
245263 static class OneTimeTokenFormLoginConfig {
246264
247265 @ Bean
248- SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
266+ SecurityFilterChain securityFilterChain (HttpSecurity http ,
267+ OneTimeTokenGenerationSuccessHandler ottSuccessHandler ) throws Exception {
249268 // @formatter:off
250269 http
251270 .authorizeHttpRequests ((authz ) -> authz
252271 .anyRequest ().authenticated ()
253272 )
254273 .formLogin (Customizer .withDefaults ())
255274 .oneTimeTokenLogin ((ott ) -> ott
256- .tokenGenerationSuccessHandler (new TestOneTimeTokenGenerationSuccessHandler () )
275+ .tokenGenerationSuccessHandler (ottSuccessHandler )
257276 );
258277 // @formatter:on
259278 return http .build ();
260279 }
261280
281+ @ Bean
282+ TestOneTimeTokenGenerationSuccessHandler ottSuccessHandler () {
283+ return new TestOneTimeTokenGenerationSuccessHandler ();
284+ }
285+
262286 }
263287
264288 @ Configuration (proxyBeanMethods = false )
@@ -282,7 +306,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
282306
283307 static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
284308
285- private static OneTimeToken lastToken ;
309+ private OneTimeToken lastToken ;
286310
287311 private final OneTimeTokenGenerationSuccessHandler delegate ;
288312
@@ -297,7 +321,7 @@ static class TestOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGen
297321 @ Override
298322 public void handle (HttpServletRequest request , HttpServletResponse response , OneTimeToken oneTimeToken )
299323 throws IOException , ServletException {
300- lastToken = oneTimeToken ;
324+ this . lastToken = oneTimeToken ;
301325 this .delegate .handle (request , response , oneTimeToken );
302326 }
303327
0 commit comments