Commit 9e9f0067 authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Enable customization of RestTemplate that retrieves JwtAccessTokenConverter's key"

See gh-8268
See gh-5859
parent dc9ff738
...@@ -16,18 +16,22 @@ ...@@ -16,18 +16,22 @@
package org.springframework.boot.autoconfigure.security.oauth2.resource; package org.springframework.boot.autoconfigure.security.oauth2.resource;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
/** /**
* Callback for customizing the rest template used to fetch the token key. * Callback for customizing the {@link RestTemplate} that is used to fetch the keys used
* by {@link JwtAccessTokenConverter}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 1.5.2 * @since 1.5.2
* @see JwtAccessTokenConverter#setSigningKey(String)
* @see JwtAccessTokenConverter#setVerifierKey(String)
*/ */
public interface JwtAccessTokenConverterRestTemplateCustomizer { public interface JwtAccessTokenConverterRestTemplateCustomizer {
/** /**
* Customize the rest template before it is initialized. * Customize the {@code template} before it is initialized.
* @param template the rest template * @param template the rest template
*/ */
void customize(RestTemplate template); void customize(RestTemplate template);
......
...@@ -303,8 +303,10 @@ public class ResourceServerTokenServicesConfiguration { ...@@ -303,8 +303,10 @@ public class ResourceServerTokenServicesConfiguration {
private String getKeyFromServer() { private String getKeyFromServer() {
RestTemplate keyUriRestTemplate = new RestTemplate(); RestTemplate keyUriRestTemplate = new RestTemplate();
for (JwtAccessTokenConverterRestTemplateCustomizer customizer : this.customizers) { if (!CollectionUtils.isEmpty(this.customizers)) {
customizer.customize(keyUriRestTemplate); for (JwtAccessTokenConverterRestTemplateCustomizer customizer : this.customizers) {
customizer.customize(keyUriRestTemplate);
}
} }
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
String username = this.resource.getClientId(); String username = this.resource.getClientId();
......
...@@ -54,12 +54,15 @@ import org.springframework.security.oauth2.client.OAuth2RestTemplate; ...@@ -54,12 +54,15 @@ import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices; import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link ResourceServerTokenServicesConfiguration}. * Tests for {@link ResourceServerTokenServicesConfiguration}.
...@@ -243,20 +246,24 @@ public class ResourceServerTokenServicesConfigurationTests { ...@@ -243,20 +246,24 @@ public class ResourceServerTokenServicesConfigurationTests {
} }
@Test @Test
public void customRestTemplate() { public void jwtAccessTokenConverterIsConfiguredWhenKeyUriIsProvided() {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
"security.oauth2.resource.userInfoUri:http://example.com", "security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana");
"security.oauth2.resource.tokenInfoUri:http://example.com", this.context = new SpringApplicationBuilder(ResourceConfiguration.class)
"security.oauth2.resource.preferTokenInfo:false"); .environment(this.environment).web(false).run();
assertThat(this.context.getBeansOfType(JwtAccessTokenConverter.class)).hasSize(1);
}
@Test
public void jwtAccessTokenConverterRestTemplateCanBeCustomized() {
EnvironmentTestUtils.addEnvironment(this.environment,
"security.oauth2.resource.jwt.key-uri=http://localhost:12345/banana");
this.context = new SpringApplicationBuilder(ResourceConfiguration.class, this.context = new SpringApplicationBuilder(ResourceConfiguration.class,
RestTemplateCustomizer.class).environment(this.environment).web(false) JwtAccessTokenConverterRestTemplateCustomizerConfiguration.class)
.run(); .environment(this.environment).web(false).run();
String[] restTemplateCustomizers = this.context JwtAccessTokenConverterRestTemplateCustomizer customizer = this.context
.getBeanNamesForType(JwtAccessTokenConverterRestTemplateCustomizer.class); .getBean(JwtAccessTokenConverterRestTemplateCustomizer.class);
UserInfoTokenServices services = this.context verify(customizer).customize(any(RestTemplate.class));
.getBean(UserInfoTokenServices.class);
assertThat(restTemplateCustomizers).hasSize(1);
assertThat(services).isNotNull();
} }
@Configuration @Configuration
...@@ -373,22 +380,14 @@ public class ResourceServerTokenServicesConfigurationTests { ...@@ -373,22 +380,14 @@ public class ResourceServerTokenServicesConfigurationTests {
} }
@Component @Configuration
protected static class RestTemplateCustomizer static class JwtAccessTokenConverterRestTemplateCustomizerConfiguration {
implements JwtAccessTokenConverterRestTemplateCustomizer {
@Override
public void customize(RestTemplate template) {
template.getInterceptors().add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
return execution.execute(request, body);
}
}); @Bean
public JwtAccessTokenConverterRestTemplateCustomizer restTemplateCustomizer() {
return mock(JwtAccessTokenConverterRestTemplateCustomizer.class);
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment