Skip to content

Allow authenticationManagerResolver to take precedence over jwt/opaqueToken #17676

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -256,8 +256,11 @@ public void init(H http) {

@Override
public void configure(H http) {
// Use the authenticationManagerResolver if configured, as it takes precedence
// over any jwt() or opaqueToken() configuration
AuthenticationManagerResolver resolver = this.authenticationManagerResolver;
if (resolver == null) {
// Fall back to jwt() or opaqueToken() configuration
AuthenticationManager authenticationManager = getAuthenticationManager(http);
resolver = (request) -> authenticationManager;
}
Expand All @@ -282,11 +285,9 @@ private void validateConfiguration() {
Assert.state(this.jwtConfigurer == null || this.opaqueTokenConfigurer == null,
"Spring Security only supports JWTs or Opaque Tokens, not both at the " + "same time.");
}
else {
Assert.state(this.jwtConfigurer == null && this.opaqueTokenConfigurer == null,
"If an authenticationManagerResolver() is configured, then it takes "
+ "precedence over any jwt() or opaqueToken() configuration.");
}
// When authenticationManagerResolver is configured, it takes precedence over
// jwt() or opaqueToken()
// configuration, so no validation is needed for that case
}

private void registerDefaultAccessDeniedHandler(H http) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,18 @@ public void configureWhenUsingBothJwtAndOpaqueThenWiringException() {
}

@Test
public void configureWhenUsingBothAuthenticationManagerResolverAndOpaqueThenWiringException() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring.register(AuthenticationManagerResolverPlusOtherConfig.class).autowire())
.withMessageContaining("authenticationManagerResolver");
public void configureWhenUsingBothAuthenticationManagerResolverAndOpaqueThenAuthenticationManagerResolverTakesPrecedence() {
// authenticationManagerResolver should take precedence over opaqueToken
// configuration
this.spring.register(AuthenticationManagerResolverPlusOtherConfig.class).autowire();
// No exception should be thrown
}

@Test
public void configureWhenUsingBothAuthenticationManagerResolverAndJwtThenAuthenticationManagerResolverTakesPrecedence() {
// authenticationManagerResolver should take precedence over jwt configuration
this.spring.register(AuthenticationManagerResolverPlusJwtConfig.class).autowire();
// No exception should be thrown
}

@Test
Expand Down Expand Up @@ -2601,15 +2609,20 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
@EnableWebSecurity
static class AuthenticationManagerResolverPlusOtherConfig {

@Bean
OpaqueTokenIntrospector opaqueTokenIntrospector() {
return mock(OpaqueTokenIntrospector.class);
}

@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeHttpRequests((requests) -> requests
.anyRequest().authenticated())
.oauth2ResourceServer((server) -> server
.authenticationManagerResolver(mock(AuthenticationManagerResolver.class))
.opaqueToken(Customizer.withDefaults()));
.opaqueToken(Customizer.withDefaults())
.authenticationManagerResolver(mock(AuthenticationManagerResolver.class)));
return http.build();
// @formatter:on
}
Expand Down Expand Up @@ -2788,4 +2801,28 @@ public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request)

}

@Configuration
@EnableWebSecurity
static class AuthenticationManagerResolverPlusJwtConfig {

@Bean
JwtDecoder jwtDecoder() {
return mock(JwtDecoder.class);
}

@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeHttpRequests((requests) -> requests
.anyRequest().authenticated())
.oauth2ResourceServer((server) -> server
.jwt(Customizer.withDefaults())
.authenticationManagerResolver(mock(AuthenticationManagerResolver.class)));
return http.build();
// @formatter:on
}

}

}