Skip to content

Commit 566634e

Browse files
committed
Support overriding WebClient in ReactiveOidcIdTokenDecoderFactory
Closes gh-14178
1 parent 69808bf commit 566634e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/ReactiveOidcIdTokenDecoderFactory.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoderFactory;
5050
import org.springframework.util.Assert;
5151
import org.springframework.util.StringUtils;
52+
import org.springframework.web.reactive.function.client.WebClient;
5253

5354
/**
5455
* A {@link ReactiveJwtDecoderFactory factory} that provides a {@link ReactiveJwtDecoder}
@@ -89,6 +90,8 @@ public final class ReactiveOidcIdTokenDecoderFactory implements ReactiveJwtDecod
8990
private Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory = (
9091
clientRegistration) -> DEFAULT_CLAIM_TYPE_CONVERTER;
9192

93+
private Function<ClientRegistration, WebClient> webClientFactory = (clientRegistration) -> WebClient.create();
94+
9295
/**
9396
* Returns the default {@link Converter}'s used for type conversion of claim values
9497
* for an {@link OidcIdToken}.
@@ -165,6 +168,7 @@ private NimbusReactiveJwtDecoder buildDecoder(ClientRegistration clientRegistrat
165168
}
166169
return NimbusReactiveJwtDecoder.withJwkSetUri(jwkSetUri)
167170
.jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm)
171+
.webClient(webClientFactory.apply(clientRegistration))
168172
.build();
169173
}
170174
if (jwsAlgorithm != null && MacAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) {
@@ -241,4 +245,19 @@ public void setClaimTypeConverterFactory(
241245
this.claimTypeConverterFactory = claimTypeConverterFactory;
242246
}
243247

248+
/**
249+
* Sets the factory that provides a {@link WebClient} used by
250+
* {@link NimbusReactiveJwtDecoder} to coordinate with the authorization servers
251+
* indicated in the <a href="https://tools.ietf.org/html/rfc7517#section-5">JWK
252+
* Set</a> uri.
253+
* @param webClientFactory the factory that provides a {@link WebClient} used by
254+
* {@link NimbusReactiveJwtDecoder}
255+
*
256+
* @since 6.3
257+
*/
258+
public void setWebClientFactory(Function<ClientRegistration, WebClient> webClientFactory) {
259+
Assert.notNull(webClientFactory, "webClientFactory cannot be null");
260+
this.webClientFactory = webClientFactory;
261+
}
262+
244263
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/authentication/ReactiveOidcIdTokenDecoderFactoryTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
3535
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
3636
import org.springframework.security.oauth2.jwt.Jwt;
37+
import org.springframework.web.reactive.function.client.WebClient;
3738

3839
import static org.assertj.core.api.Assertions.assertThat;
3940
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -94,6 +95,12 @@ public void setClaimTypeConverterFactoryWhenNullThenThrowIllegalArgumentExceptio
9495
.isThrownBy(() -> this.idTokenDecoderFactory.setClaimTypeConverterFactory(null));
9596
}
9697

98+
@Test
99+
public void setWebClientFactoryWhenNullThenThrowIllegalArgumentException() {
100+
assertThatIllegalArgumentException()
101+
.isThrownBy(() -> this.idTokenDecoderFactory.setWebClientFactory(null));
102+
}
103+
97104
@Test
98105
public void createDecoderWhenClientRegistrationNullThenThrowIllegalArgumentException() {
99106
assertThatIllegalArgumentException().isThrownBy(() -> this.idTokenDecoderFactory.createDecoder(null));
@@ -176,4 +183,15 @@ public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
176183
verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
177184
}
178185

186+
@Test
187+
public void createDecoderWhenCustomWebClientFactorySetThenApplied() {
188+
Function<ClientRegistration, WebClient> customWebClientFactory = mock(
189+
Function.class);
190+
this.idTokenDecoderFactory.setWebClientFactory(customWebClientFactory);
191+
ClientRegistration clientRegistration = this.registration.build();
192+
given(customWebClientFactory.apply(same(clientRegistration)))
193+
.willReturn(WebClient.create());
194+
this.idTokenDecoderFactory.createDecoder(clientRegistration);
195+
verify(customWebClientFactory).apply(same(clientRegistration));
196+
}
179197
}

0 commit comments

Comments
 (0)