Skip to content

Commit 0410bac

Browse files
ilgrossojzheaux
authored andcommitted
Add support for oauth2Login().securityContextRepository(...)
Fixes gh-7222
1 parent bbefc49 commit 0410bac

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,8 @@ public class OAuth2LoginSpec {
972972

973973
private ReactiveAuthenticationManager authenticationManager;
974974

975+
private ServerSecurityContextRepository securityContextRepository = new WebSessionServerSecurityContextRepository();
976+
975977
private ServerAuthenticationConverter authenticationConverter;
976978

977979
private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver;
@@ -993,6 +995,19 @@ public OAuth2LoginSpec authenticationManager(ReactiveAuthenticationManager authe
993995
return this;
994996
}
995997

998+
/**
999+
* The {@link ServerSecurityContextRepository} used to save the {@code Authentication}. Defaults to
1000+
* {@link WebSessionServerSecurityContextRepository}.
1001+
*
1002+
* @since 5.2
1003+
* @param securityContextRepository the repository to use
1004+
* @return the {@link OAuth2LoginSpec} to continue configuring
1005+
*/
1006+
public OAuth2LoginSpec securityContextRepository(ServerSecurityContextRepository securityContextRepository) {
1007+
this.securityContextRepository = securityContextRepository;
1008+
return this;
1009+
}
1010+
9961011
/**
9971012
* The {@link ServerAuthenticationSuccessHandler} used after authentication success. Defaults to
9981013
* {@link RedirectServerAuthenticationSuccessHandler} redirecting to "/".
@@ -1138,7 +1153,7 @@ protected void configure(ServerHttpSecurity http) {
11381153

11391154
authenticationFilter.setAuthenticationSuccessHandler(this.authenticationSuccessHandler);
11401155
authenticationFilter.setAuthenticationFailureHandler(this.authenticationFailureHandler);
1141-
authenticationFilter.setSecurityContextRepository(new WebSessionServerSecurityContextRepository());
1156+
authenticationFilter.setSecurityContextRepository(this.securityContextRepository);
11421157

11431158
MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher(
11441159
MediaType.TEXT_HTML);

config/src/test/java/org/springframework/security/config/web/server/OAuth2LoginTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ public void oauth2LoginWhenCustomBeansThenUsed() {
426426
ServerAuthenticationConverter converter = config.authenticationConverter;
427427
when(converter.convert(any())).thenReturn(Mono.just(token));
428428

429+
ServerSecurityContextRepository securityContextRepository = config.securityContextRepository;
430+
when(securityContextRepository.save(any(), any())).thenReturn(Mono.empty());
431+
429432
Map<String, Object> additionalParameters = new HashMap<>();
430433
additionalParameters.put(OidcParameterNames.ID_TOKEN, "id-token");
431434
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue())
@@ -447,6 +450,7 @@ public void oauth2LoginWhenCustomBeansThenUsed() {
447450

448451
verify(config.jwtDecoderFactory).createDecoder(any());
449452
verify(tokenResponseClient).getTokenResponse(any());
453+
verify(securityContextRepository).save(any(), any());
450454
}
451455

452456
@Configuration
@@ -461,6 +465,8 @@ static class OAuth2LoginWithCustomBeansConfig {
461465

462466
ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory = spy(new JwtDecoderFactory());
463467

468+
ServerSecurityContextRepository securityContextRepository = mock(ServerSecurityContextRepository.class);
469+
464470
@Bean
465471
public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
466472
// @formatter:off
@@ -470,7 +476,8 @@ public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
470476
.and()
471477
.oauth2Login()
472478
.authenticationConverter(authenticationConverter)
473-
.authenticationManager(authenticationManager());
479+
.authenticationManager(authenticationManager())
480+
.securityContextRepository(securityContextRepository);
474481
return http.build();
475482
// @formatter:on
476483
}

0 commit comments

Comments
 (0)