diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java index 2131074bee..e603a9cae5 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java @@ -256,8 +256,11 @@ public final class OAuth2ResourceServerConfigurer authenticationManager; } @@ -282,11 +285,9 @@ public final class OAuth2ResourceServerConfigurer 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 @@ -2601,6 +2609,11 @@ public class OAuth2ResourceServerConfigurerTests { @EnableWebSecurity static class AuthenticationManagerResolverPlusOtherConfig { + @Bean + OpaqueTokenIntrospector opaqueTokenIntrospector() { + return mock(OpaqueTokenIntrospector.class); + } + @Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // @formatter:off @@ -2608,8 +2621,8 @@ public class OAuth2ResourceServerConfigurerTests { .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 } @@ -2788,4 +2801,28 @@ public class OAuth2ResourceServerConfigurerTests { } + @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 + } + + } + }