Skip to content

Commit ab85b21

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

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-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: 32 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,24 @@ 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)
99+
.run((context) -> {
100+
assertThat(context).hasSingleBean(JwtDecoder.class);
101+
JwtDecoder jwtDecoder = context.getBean(JwtDecoder.class);
102+
Object processor = ReflectionTestUtils.getField(jwtDecoder, "jwtProcessor");
103+
Object keySelector = ReflectionTestUtils.getField(processor, "jwsKeySelector");
104+
Object jwkSource = ReflectionTestUtils.getField(keySelector, "jwkSource");
105+
Object jwkSetRetriever = ReflectionTestUtils.getField(jwkSource, "jwkSetRetriever");
106+
Object restOperations = ReflectionTestUtils.getField(jwkSetRetriever, "restOperations");
107+
assertThat(restOperations).isNotNull();
108+
assertThat(restOperations).isEqualTo(RestTemplateConfig.configuredRestTemplate);
109+
});
110+
}
111+
93112
@Test
94113
void autoConfigurationShouldMatchDefaultJwsAlgorithm() {
95114
this.contextRunner
@@ -424,6 +443,19 @@ JwtDecoder decoder() {
424443

425444
}
426445

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

0 commit comments

Comments
 (0)