Skip to content

Commit 3426f5c

Browse files
author
Michael Kreis
committed
use the applications rest template for the autoconfigured jwks receiver
1 parent ed2196f commit 3426f5c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerJwtConfiguration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.security.interfaces.RSAPublicKey;
2020
import java.security.spec.X509EncodedKeySpec;
2121
import java.util.Base64;
22+
import java.util.Optional;
2223

2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -37,6 +38,7 @@
3738
import org.springframework.security.oauth2.jwt.JwtDecoders;
3839
import org.springframework.security.oauth2.jwt.JwtValidators;
3940
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
41+
import org.springframework.web.client.RestTemplate;
4042

4143
/**
4244
* Configures a {@link JwtDecoder} when a JWK Set URI, OpenID Connect Issuer URI or Public
@@ -63,9 +65,14 @@ static class JwtDecoderConfiguration {
6365

6466
@Bean
6567
@ConditionalOnProperty(name = "spring.security.oauth2.resourceserver.jwt.jwk-set-uri")
66-
JwtDecoder jwtDecoderByJwkKeySetUri() {
67-
NimbusJwtDecoder nimbusJwtDecoder = NimbusJwtDecoder.withJwkSetUri(this.properties.getJwkSetUri())
68-
.jwsAlgorithm(SignatureAlgorithm.from(this.properties.getJwsAlgorithm())).build();
68+
JwtDecoder jwtDecoderByJwkKeySetUri(Optional<RestTemplate> configuredRestTemplate) {
69+
NimbusJwtDecoder.JwkSetUriJwtDecoderBuilder jwtDecoderBuilder = NimbusJwtDecoder
70+
.withJwkSetUri(this.properties.getJwkSetUri())
71+
.jwsAlgorithm(SignatureAlgorithm.from(this.properties.getJwsAlgorithm()));
72+
73+
configuredRestTemplate.ifPresent(jwtDecoderBuilder::restOperations);
74+
NimbusJwtDecoder nimbusJwtDecoder = jwtDecoderBuilder.build();
75+
6976
String issuerUri = this.properties.getIssuerUri();
7077
if (issuerUri != null) {
7178
nimbusJwtDecoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(issuerUri));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerAutoConfigurationTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.security.web.FilterChainProxy;
5555
import org.springframework.security.web.SecurityFilterChain;
5656
import org.springframework.test.util.ReflectionTestUtils;
57+
import org.springframework.web.client.RestTemplate;
5758

5859
import static org.assertj.core.api.Assertions.assertThat;
5960
import static org.mockito.Mockito.mock;
@@ -90,6 +91,23 @@ void autoConfigurationShouldConfigureResourceServer() {
9091
});
9192
}
9293

94+
@Test
95+
void autoConfigurationShouldUseApplicationsRestTemplate() {
96+
this.contextRunner
97+
.withPropertyValues("spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://jwk-set-uri.com")
98+
.withUserConfiguration(RestTemplateConfig.class).run((context) -> {
99+
assertThat(context).hasSingleBean(JwtDecoder.class);
100+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
101+
Object processor = ReflectionTestUtils.getField(jwtDecoder, "jwtProcessor");
102+
Object keySelector = ReflectionTestUtils.getField(processor, "jwsKeySelector");
103+
Object jwkSource = ReflectionTestUtils.getField(keySelector, "jwkSource");
104+
Object jwkSetRetriever = ReflectionTestUtils.getField(jwkSource, "jwkSetRetriever");
105+
Object restOperations = ReflectionTestUtils.getField(jwkSetRetriever, "restOperations");
106+
assertThat(restOperations).isNotNull();
107+
assertThat(restOperations).isEqualTo(RestTemplateConfig.configuredRestTemplate);
108+
});
109+
}
110+
93111
@Test
94112
void autoConfigurationShouldMatchDefaultJwsAlgorithm() {
95113
this.contextRunner
@@ -424,6 +442,19 @@ JwtDecoder decoder() {
424442

425443
}
426444

445+
@Configuration(proxyBeanMethods = false)
446+
@EnableWebSecurity
447+
static class RestTemplateConfig {
448+
449+
private static RestTemplate configuredRestTemplate = new RestTemplate();
450+
451+
@Bean
452+
RestTemplate restTemplate() {
453+
return configuredRestTemplate;
454+
}
455+
456+
}
457+
427458
@Configuration(proxyBeanMethods = false)
428459
@EnableWebSecurity
429460
static class OpaqueTokenIntrospectorConfig {

0 commit comments

Comments
 (0)