Skip to content

Add OAuth2LoginSpec#securityContextRepository #7244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ public class OAuth2LoginSpec {

private ReactiveAuthenticationManager authenticationManager;

private ServerSecurityContextRepository securityContextRepository = new WebSessionServerSecurityContextRepository();

private ServerAuthenticationConverter authenticationConverter;

private ServerOAuth2AuthorizationRequestResolver authorizationRequestResolver;
Expand All @@ -993,6 +995,19 @@ public OAuth2LoginSpec authenticationManager(ReactiveAuthenticationManager authe
return this;
}

/**
* The {@link ServerSecurityContextRepository} used to save the {@code Authentication}. Defaults to
* {@link WebSessionServerSecurityContextRepository}.
*
* @since 5.2
* @param securityContextRepository the repository to use
* @return the {@link OAuth2LoginSpec} to continue configuring
*/
public OAuth2LoginSpec securityContextRepository(ServerSecurityContextRepository securityContextRepository) {
this.securityContextRepository = securityContextRepository;
return this;
}

/**
* The {@link ServerAuthenticationSuccessHandler} used after authentication success. Defaults to
* {@link RedirectServerAuthenticationSuccessHandler} redirecting to "/".
Expand Down Expand Up @@ -1138,7 +1153,7 @@ protected void configure(ServerHttpSecurity http) {

authenticationFilter.setAuthenticationSuccessHandler(this.authenticationSuccessHandler);
authenticationFilter.setAuthenticationFailureHandler(this.authenticationFailureHandler);
authenticationFilter.setSecurityContextRepository(new WebSessionServerSecurityContextRepository());
authenticationFilter.setSecurityContextRepository(this.securityContextRepository);

MediaTypeServerWebExchangeMatcher htmlMatcher = new MediaTypeServerWebExchangeMatcher(
MediaType.TEXT_HTML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ public void oauth2LoginWhenCustomBeansThenUsed() {
ServerAuthenticationConverter converter = config.authenticationConverter;
when(converter.convert(any())).thenReturn(Mono.just(token));

ServerSecurityContextRepository securityContextRepository = config.securityContextRepository;
when(securityContextRepository.save(any(), any())).thenReturn(Mono.empty());

Map<String, Object> additionalParameters = new HashMap<>();
additionalParameters.put(OidcParameterNames.ID_TOKEN, "id-token");
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken(accessToken.getTokenValue())
Expand All @@ -447,6 +450,7 @@ public void oauth2LoginWhenCustomBeansThenUsed() {

verify(config.jwtDecoderFactory).createDecoder(any());
verify(tokenResponseClient).getTokenResponse(any());
verify(securityContextRepository).save(any(), any());
}

@Configuration
Expand All @@ -461,6 +465,8 @@ static class OAuth2LoginWithCustomBeansConfig {

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

ServerSecurityContextRepository securityContextRepository = mock(ServerSecurityContextRepository.class);

@Bean
public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
// @formatter:off
Expand All @@ -470,7 +476,8 @@ public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
.and()
.oauth2Login()
.authenticationConverter(authenticationConverter)
.authenticationManager(authenticationManager());
.authenticationManager(authenticationManager())
.securityContextRepository(securityContextRepository);
return http.build();
// @formatter:on
}
Expand Down