Commit 8708a07a authored by Dave Syer's avatar Dave Syer

Ensure RestTemplate interceptors remain mutable

Fixes gh-4553
parent 3dda029c
...@@ -114,8 +114,7 @@ public class ResourceServerTokenServicesConfiguration { ...@@ -114,8 +114,7 @@ public class ResourceServerTokenServicesConfiguration {
this.details = DEFAULT_RESOURCE_DETAILS; this.details = DEFAULT_RESOURCE_DETAILS;
} }
OAuth2RestTemplate template = getTemplate(); OAuth2RestTemplate template = getTemplate();
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList( template.getInterceptors().add(new AcceptJsonRequestInterceptor());
new AcceptJsonRequestInterceptor()));
AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider(); AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
accessTokenProvider.setTokenRequestEnhancer(new AcceptJsonRequestEnhancer()); accessTokenProvider.setTokenRequestEnhancer(new AcceptJsonRequestEnhancer());
template.setAccessTokenProvider(accessTokenProvider); template.setAccessTokenProvider(accessTokenProvider);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.security.oauth2.resource; package org.springframework.boot.autoconfigure.security.oauth2.resource;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -41,11 +42,17 @@ import org.springframework.context.annotation.Configuration; ...@@ -41,11 +42,17 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
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.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.stereotype.Component;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -147,6 +154,19 @@ public class ResourceServerTokenServicesConfigurationTests { ...@@ -147,6 +154,19 @@ public class ResourceServerTokenServicesConfigurationTests {
assertNotNull(services); assertNotNull(services);
} }
@Test
public void userInfoWithCustomizer() {
EnvironmentTestUtils.addEnvironment(this.environment,
"security.oauth2.resource.userInfoUri:http://example.com",
"security.oauth2.resource.tokenInfoUri:http://example.com",
"security.oauth2.resource.preferTokenInfo:false");
this.context = new SpringApplicationBuilder(ResourceConfiguration.class,
Customizer.class).environment(this.environment).web(false).run();
UserInfoTokenServices services = this.context
.getBean(UserInfoTokenServices.class);
assertNotNull(services);
}
@Test @Test
public void switchToJwt() { public void switchToJwt() {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
...@@ -245,4 +265,21 @@ public class ResourceServerTokenServicesConfigurationTests { ...@@ -245,4 +265,21 @@ public class ResourceServerTokenServicesConfigurationTests {
} }
@Component
protected static class Customizer implements UserInfoRestTemplateCustomizer {
@Override
public void customize(OAuth2RestTemplate template) {
template.getInterceptors().add(new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
return execution.execute(request, body);
}
});
}
}
} }
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