23
23
24
24
import org .junit .Rule ;
25
25
import org .junit .Test ;
26
+ import org .mockito .stubbing .Answer ;
26
27
import org .openqa .selenium .WebDriver ;
28
+ import org .springframework .security .web .server .WebFilterExchange ;
29
+ import org .springframework .security .web .server .authentication .RedirectServerAuthenticationSuccessHandler ;
30
+ import org .springframework .security .web .server .authentication .ServerAuthenticationSuccessHandler ;
27
31
import reactor .core .publisher .Mono ;
28
32
29
33
import org .springframework .beans .factory .annotation .Autowired ;
@@ -184,6 +188,8 @@ public void oauth2LoginWhenCustomObjectsThenUsed() {
184
188
this .spring .register (OAuth2LoginWithSingleClientRegistrations .class ,
185
189
OAuth2LoginMockAuthenticationManagerConfig .class ).autowire ();
186
190
191
+ String redirectLocation = "/custom-redirect-location" ;
192
+
187
193
WebTestClient webTestClient = WebTestClientBuilder
188
194
.bindToWebFilters (this .springSecurity )
189
195
.build ();
@@ -194,6 +200,7 @@ public void oauth2LoginWhenCustomObjectsThenUsed() {
194
200
ReactiveAuthenticationManager manager = config .manager ;
195
201
ServerWebExchangeMatcher matcher = config .matcher ;
196
202
ServerOAuth2AuthorizationRequestResolver resolver = config .resolver ;
203
+ ServerAuthenticationSuccessHandler successHandler = config .successHandler ;
197
204
198
205
OAuth2AuthorizationExchange exchange = TestOAuth2AuthorizationExchanges .success ();
199
206
OAuth2User user = TestOAuth2Users .create ();
@@ -205,16 +212,25 @@ public void oauth2LoginWhenCustomObjectsThenUsed() {
205
212
when (manager .authenticate (any ())).thenReturn (Mono .just (result ));
206
213
when (matcher .matches (any ())).thenReturn (ServerWebExchangeMatcher .MatchResult .match ());
207
214
when (resolver .resolve (any ())).thenReturn (Mono .empty ());
215
+ when (successHandler .onAuthenticationSuccess (any (), any ())).thenAnswer ((Answer <Mono <Void >>) invocation -> {
216
+ WebFilterExchange webFilterExchange = invocation .getArgument (0 );
217
+ Authentication authentication = invocation .getArgument (1 );
218
+
219
+ return new RedirectServerAuthenticationSuccessHandler (redirectLocation )
220
+ .onAuthenticationSuccess (webFilterExchange , authentication );
221
+ });
208
222
209
223
webTestClient .get ()
210
224
.uri ("/login/oauth2/code/github" )
211
225
.exchange ()
212
- .expectStatus ().is3xxRedirection ();
226
+ .expectStatus ().is3xxRedirection ()
227
+ .expectHeader ().valueEquals ("Location" , redirectLocation );
213
228
214
229
verify (converter ).convert (any ());
215
230
verify (manager ).authenticate (any ());
216
231
verify (matcher ).matches (any ());
217
232
verify (resolver ).resolve (any ());
233
+ verify (successHandler ).onAuthenticationSuccess (any (), any ());
218
234
}
219
235
220
236
@ Configuration
@@ -227,6 +243,8 @@ static class OAuth2LoginMockAuthenticationManagerConfig {
227
243
228
244
ServerOAuth2AuthorizationRequestResolver resolver = mock (ServerOAuth2AuthorizationRequestResolver .class );
229
245
246
+ ServerAuthenticationSuccessHandler successHandler = mock (ServerAuthenticationSuccessHandler .class );
247
+
230
248
@ Bean
231
249
public SecurityWebFilterChain springSecurityFilter (ServerHttpSecurity http ) {
232
250
http
@@ -237,7 +255,8 @@ public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
237
255
.authenticationConverter (authenticationConverter )
238
256
.authenticationManager (manager )
239
257
.authenticationMatcher (matcher )
240
- .authorizationRequestResolver (resolver );
258
+ .authorizationRequestResolver (resolver )
259
+ .authenticationSuccessHandler (successHandler );
241
260
return http .build ();
242
261
}
243
262
}
@@ -425,4 +444,5 @@ Mono<SecurityContext> authentication(Authentication authentication) {
425
444
<T > T getBean (Class <T > beanClass ) {
426
445
return this .spring .getContext ().getBean (beanClass );
427
446
}
447
+
428
448
}
0 commit comments