Skip to content

Commit 9e9f006

Browse files
committed
Polish "Enable customization of RestTemplate that retrieves JwtAccessTokenConverter's key"
See gh-8268 See gh-5859
1 parent dc9ff73 commit 9e9f006

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/JwtAccessTokenConverterRestTemplateCustomizer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616

1717
package org.springframework.boot.autoconfigure.security.oauth2.resource;
1818

19+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
1920
import org.springframework.web.client.RestTemplate;
2021

2122
/**
22-
* Callback for customizing the rest template used to fetch the token key.
23+
* Callback for customizing the {@link RestTemplate} that is used to fetch the keys used
24+
* by {@link JwtAccessTokenConverter}.
2325
*
2426
* @author Eddú Meléndez
2527
* @since 1.5.2
28+
* @see JwtAccessTokenConverter#setSigningKey(String)
29+
* @see JwtAccessTokenConverter#setVerifierKey(String)
2630
*/
2731
public interface JwtAccessTokenConverterRestTemplateCustomizer {
2832

2933
/**
30-
* Customize the rest template before it is initialized.
34+
* Customize the {@code template} before it is initialized.
3135
* @param template the rest template
3236
*/
3337
void customize(RestTemplate template);

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ public JwtAccessTokenConverter jwtTokenEnhancer() {
303303

304304
private String getKeyFromServer() {
305305
RestTemplate keyUriRestTemplate = new RestTemplate();
306-
for (JwtAccessTokenConverterRestTemplateCustomizer customizer : this.customizers) {
307-
customizer.customize(keyUriRestTemplate);
306+
if (!CollectionUtils.isEmpty(this.customizers)) {
307+
for (JwtAccessTokenConverterRestTemplateCustomizer customizer : this.customizers) {
308+
customizer.customize(keyUriRestTemplate);
309+
}
308310
}
309311
HttpHeaders headers = new HttpHeaders();
310312
String username = this.resource.getClientId();

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfigurationTests.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@
5454
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
5555
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
5656
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
57+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
5758
import org.springframework.social.connect.ConnectionFactoryLocator;
5859
import org.springframework.stereotype.Component;
5960
import org.springframework.web.client.RestTemplate;
6061

6162
import static org.assertj.core.api.Assertions.assertThat;
63+
import static org.mockito.Matchers.any;
6264
import static org.mockito.Mockito.mock;
65+
import static org.mockito.Mockito.verify;
6366

6467
/**
6568
* Tests for {@link ResourceServerTokenServicesConfiguration}.
@@ -243,20 +246,24 @@ public void customUserInfoRestTemplateFactory() {
243246
}
244247

245248
@Test
246-
public void customRestTemplate() {
249+
public void jwtAccessTokenConverterIsConfiguredWhenKeyUriIsProvided() {
247250
EnvironmentTestUtils.addEnvironment(this.environment,
248-
"security.oauth2.resource.userInfoUri:http://example.com",
249-
"security.oauth2.resource.tokenInfoUri:http://example.com",
250-
"security.oauth2.resource.preferTokenInfo:false");
251+
"security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana");
252+
this.context = new SpringApplicationBuilder(ResourceConfiguration.class)
253+
.environment(this.environment).web(false).run();
254+
assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
255+
}
256+
257+
@Test
258+
public void jwtAccessTokenConverterRestTemplateCanBeCustomized() {
259+
EnvironmentTestUtils.addEnvironment(this.environment,
260+
"security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana");
251261
this.context = new SpringApplicationBuilder(ResourceConfiguration.class,
252-
RestTemplateCustomizer.class).environment(this.environment).web(false)
253-
.run();
254-
String[] restTemplateCustomizers = this.context
255-
.getBeanNamesForType(JwtAccessTokenConverterRestTemplateCustomizer.class);
256-
UserInfoTokenServices services = this.context
257-
.getBean(UserInfoTokenServices.class);
258-
assertThat(restTemplateCustomizers).hasSize(1);
259-
assertThat(services).isNotNull();
262+
JwtAccessTokenConverterRestTemplateCustomizerConfiguration.class)
263+
.environment(this.environment).web(false).run();
264+
JwtAccessTokenConverterRestTemplateCustomizer customizer = this.context
265+
.getBean(JwtAccessTokenConverterRestTemplateCustomizer.class);
266+
verify(customizer).customize(any(RestTemplate.class));
260267
}
261268

262269
@Configuration
@@ -373,22 +380,14 @@ public OAuth2RestTemplate getUserInfoRestTemplate() {
373380

374381
}
375382

376-
@Component
377-
protected static class RestTemplateCustomizer
378-
implements JwtAccessTokenConverterRestTemplateCustomizer {
379-
380-
@Override
381-
public void customize(RestTemplate template) {
382-
template.getInterceptors().add(new ClientHttpRequestInterceptor() {
383-
384-
@Override
385-
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
386-
ClientHttpRequestExecution execution) throws IOException {
387-
return execution.execute(request, body);
388-
}
383+
@Configuration
384+
static class JwtAccessTokenConverterRestTemplateCustomizerConfiguration {
389385

390-
});
386+
@Bean
387+
public JwtAccessTokenConverterRestTemplateCustomizer restTemplateCustomizer() {
388+
return mock(JwtAccessTokenConverterRestTemplateCustomizer.class);
391389
}
390+
392391
}
393392

394393
}

0 commit comments

Comments
 (0)