Skip to content

Commit ad9dc49

Browse files
Aanuoluwapo Otitoolajgrandja
Aanuoluwapo Otitoola
authored andcommitted
OAuth2LoginSpec discovers ReactiveOAuth2AccessTokenResponseClient @bean
Fixes: gh-6477
1 parent 6bf45e5 commit ad9dc49

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.UUID;
3232
import java.util.function.Function;
3333

34+
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
35+
import org.springframework.security.oauth2.client.endpoint.ReactiveOAuth2AccessTokenResponseClient;
3436
import reactor.core.publisher.Mono;
3537
import reactor.util.context.Context;
3638

@@ -621,7 +623,7 @@ private ReactiveAuthenticationManager getAuthenticationManager() {
621623
}
622624

623625
private ReactiveAuthenticationManager createDefault() {
624-
WebClientReactiveAuthorizationCodeTokenResponseClient client = new WebClientReactiveAuthorizationCodeTokenResponseClient();
626+
ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> client = getAccessTokenResponseClient();
625627
ReactiveAuthenticationManager result = new OAuth2LoginReactiveAuthenticationManager(client, getOauth2UserService());
626628

627629
boolean oidcAuthenticationProviderEnabled = ClassUtils.isPresent(
@@ -788,6 +790,15 @@ private Map<String, String> getLinks() {
788790
return result;
789791
}
790792

793+
private ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> getAccessTokenResponseClient() {
794+
ResolvableType type = ResolvableType.forClassWithGenerics(ReactiveOAuth2AccessTokenResponseClient.class, OAuth2AuthorizationCodeGrantRequest.class);
795+
ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> bean = getBeanOrNull(type);
796+
if (bean == null) {
797+
return new WebClientReactiveAuthorizationCodeTokenResponseClient();
798+
}
799+
return bean;
800+
}
801+
791802
private ReactiveClientRegistrationRepository getClientRegistrationRepository() {
792803
if (this.clientRegistrationRepository == null) {
793804
this.clientRegistrationRepository = getBeanOrNull(ReactiveClientRegistrationRepository.class);

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,16 @@ public SecurityWebFilterChain springSecurityFilter(ServerHttpSecurity http) {
218218
}
219219

220220
@Test
221-
public void oauth2LoginWhenCustomJwtDecoderFactoryThenUsed() {
221+
public void oauth2LoginWhenCustomBeansThenUsed() {
222222
this.spring.register(OAuth2LoginWithMultipleClientRegistrations.class,
223-
OAuth2LoginWithJwtDecoderFactoryBeanConfig.class).autowire();
223+
OAuth2LoginWithCustomBeansConfig.class).autowire();
224224

225225
WebTestClient webTestClient = WebTestClientBuilder
226226
.bindToWebFilters(this.springSecurity)
227227
.build();
228228

229-
OAuth2LoginWithJwtDecoderFactoryBeanConfig config = this.spring.getContext()
230-
.getBean(OAuth2LoginWithJwtDecoderFactoryBeanConfig.class);
229+
OAuth2LoginWithCustomBeansConfig config = this.spring.getContext()
230+
.getBean(OAuth2LoginWithCustomBeansConfig.class);
231231

232232
OAuth2AuthorizationRequest request = TestOAuth2AuthorizationRequests.request().scope("openid").build();
233233
OAuth2AuthorizationResponse response = TestOAuth2AuthorizationResponses.success().build();
@@ -258,10 +258,11 @@ public void oauth2LoginWhenCustomJwtDecoderFactoryThenUsed() {
258258
.expectStatus().is3xxRedirection();
259259

260260
verify(config.jwtDecoderFactory).createDecoder(any());
261+
verify(tokenResponseClient).getTokenResponse(any());
261262
}
262263

263264
@Configuration
264-
static class OAuth2LoginWithJwtDecoderFactoryBeanConfig {
265+
static class OAuth2LoginWithCustomBeansConfig {
265266

266267
ServerAuthenticationConverter authenticationConverter = mock(ServerAuthenticationConverter.class);
267268

@@ -298,6 +299,11 @@ public ReactiveJwtDecoderFactory<ClientRegistration> jwtDecoderFactory() {
298299
return jwtDecoderFactory;
299300
}
300301

302+
@Bean
303+
public ReactiveOAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient() {
304+
return tokenResponseClient;
305+
}
306+
301307
private static class JwtDecoderFactory implements ReactiveJwtDecoderFactory<ClientRegistration> {
302308

303309
@Override

0 commit comments

Comments
 (0)