Removes old spring-security-oauth2.
See gh-1053
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -155,7 +155,7 @@
|
||||
<artifactId>spring-cloud-test-support</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<!--<dependency>
|
||||
<groupId>org.springframework.security.oauth.boot</groupId>
|
||||
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
|
||||
<version>${spring-security-oauth2-autoconfigure.version}</version>
|
||||
@@ -169,7 +169,7 @@
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependency>-->
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<modules>
|
||||
|
||||
@@ -16,14 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.commons.security;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
||||
|
||||
/**
|
||||
* Convenience class for relaying an access token from the {@link SecurityContext} to the
|
||||
* {@link OAuth2ClientContext}. If successful then subsequent calls to an
|
||||
@@ -38,35 +30,27 @@ import org.springframework.security.oauth2.provider.authentication.OAuth2Authent
|
||||
*/
|
||||
public class AccessTokenContextRelay {
|
||||
|
||||
private OAuth2ClientContext context;
|
||||
|
||||
public AccessTokenContextRelay(OAuth2ClientContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to copy an access token from the security context into the oauth2 context.
|
||||
* @return true if the token was copied
|
||||
*/
|
||||
public boolean copyToken() {
|
||||
if (context.getAccessToken() == null) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object details = authentication.getDetails();
|
||||
if (details instanceof OAuth2AuthenticationDetails) {
|
||||
OAuth2AuthenticationDetails holder = (OAuth2AuthenticationDetails) details;
|
||||
String token = holder.getTokenValue();
|
||||
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(token);
|
||||
String tokenType = holder.getTokenType();
|
||||
if (tokenType != null) {
|
||||
accessToken.setTokenType(tokenType);
|
||||
}
|
||||
context.setAccessToken(accessToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
* private OAuth2ClientContext context;
|
||||
*
|
||||
* public AccessTokenContextRelay(OAuth2ClientContext context) { this.context =
|
||||
* context; }
|
||||
*
|
||||
*//**
|
||||
* Attempt to copy an access token from the security context into the oauth2
|
||||
* context.
|
||||
* @return true if the token was copied
|
||||
*//*
|
||||
* public boolean copyToken() { if (context.getAccessToken() == null) {
|
||||
* Authentication authentication =
|
||||
* SecurityContextHolder.getContext().getAuthentication(); if (authentication
|
||||
* != null) { Object details = authentication.getDetails(); if (details
|
||||
* instanceof OAuth2AuthenticationDetails) { OAuth2AuthenticationDetails
|
||||
* holder = (OAuth2AuthenticationDetails) details; String token =
|
||||
* holder.getTokenValue(); DefaultOAuth2AccessToken accessToken = new
|
||||
* DefaultOAuth2AccessToken(token); String tokenType = holder.getTokenType();
|
||||
* if (tokenType != null) { accessToken.setTokenType(tokenType); }
|
||||
* context.setAccessToken(accessToken); return true; } } } return false; }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -26,20 +26,11 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@@ -61,17 +52,17 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
*
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureAfter(OAuth2AutoConfiguration.class)
|
||||
// @AutoConfigureAfter(OAuth2AutoConfiguration.class)
|
||||
@ResourceServerTokenRelayAutoConfiguration.ConditionalOnOAuth2ClientInResourceServer
|
||||
@ConditionalOnClass(ResourceServerConfiguration.class)
|
||||
// @ConditionalOnClass(ResourceServerConfiguration.class)
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnProperty(value = "spring.cloud.mvc.token-relay.enabled", matchIfMissing = true)
|
||||
public class ResourceServerTokenRelayAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public AccessTokenContextRelay accessTokenContextRelay(OAuth2ClientContext context) {
|
||||
return new AccessTokenContextRelay(context);
|
||||
}
|
||||
/*
|
||||
* @Bean public AccessTokenContextRelay accessTokenContextRelay(OAuth2ClientContext
|
||||
* context) { return new AccessTokenContextRelay(context); }
|
||||
*/
|
||||
|
||||
/**
|
||||
* A {@link WebMvcConfigurer} for the access token interceptor.
|
||||
@@ -90,7 +81,7 @@ public class ResourceServerTokenRelayAutoConfiguration {
|
||||
registry.addInterceptor(new HandlerInterceptor() {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
accessTokenContextRelay.copyToken();
|
||||
// accessTokenContextRelay.copyToken();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
@@ -112,15 +103,15 @@ public class ResourceServerTokenRelayAutoConfiguration {
|
||||
super(ConfigurationPhase.REGISTER_BEAN);
|
||||
}
|
||||
|
||||
@ConditionalOnBean(ResourceServerConfiguration.class)
|
||||
static class Server {
|
||||
|
||||
}
|
||||
|
||||
@ConditionalOnBean(OAuth2ClientConfiguration.class)
|
||||
static class Client {
|
||||
|
||||
}
|
||||
/*
|
||||
* @ConditionalOnBean(ResourceServerConfiguration.class) static class Server {
|
||||
*
|
||||
* }
|
||||
*
|
||||
* @ConditionalOnBean(OAuth2ClientConfiguration.class) static class Client {
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,34 +17,21 @@
|
||||
package org.springframework.cloud.commons.security;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Disabled
|
||||
public class ResourceServerTokenRelayAutoConfigurationTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
@@ -67,40 +54,45 @@ public class ResourceServerTokenRelayAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void clientConfigured() throws Exception {
|
||||
this.context = new SpringApplicationBuilder(ClientConfiguration.class).properties("spring.config.name=test",
|
||||
"server.port=0", "security.oauth2.resource.userInfoUri:https://example.com",
|
||||
"security.oauth2.client.clientId=foo").run();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
|
||||
OAuth2ClientContext client = this.context.getBean(OAuth2ClientContext.class);
|
||||
assertThat(client.getAccessToken()).isNull();
|
||||
UserInfoTokenServices services = context.getBean(UserInfoTokenServices.class);
|
||||
OAuth2RestTemplate template = (OAuth2RestTemplate) ReflectionTestUtils.getField(services, "restTemplate");
|
||||
MockRestServiceServer server = MockRestServiceServer.createServer(template);
|
||||
server.expect(requestTo("https://example.com"))
|
||||
.andRespond(withSuccess("{\"id\":\"user\"}", MediaType.APPLICATION_JSON));
|
||||
services.loadAuthentication("FOO");
|
||||
assertThat(client.getAccessToken().getValue()).isEqualTo("FOO");
|
||||
server.verify();
|
||||
/*
|
||||
* this.context = new
|
||||
* SpringApplicationBuilder(ClientConfiguration.class).properties(
|
||||
* "spring.config.name=test", "server.port=0",
|
||||
* "security.oauth2.resource.userInfoUri:https://example.com",
|
||||
* "security.oauth2.client.clientId=foo").run();
|
||||
* RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new
|
||||
* MockHttpServletRequest())); OAuth2ClientContext client =
|
||||
* this.context.getBean(OAuth2ClientContext.class);
|
||||
* assertThat(client.getAccessToken()).isNull(); UserInfoTokenServices services =
|
||||
* context.getBean(UserInfoTokenServices.class); OAuth2RestTemplate template =
|
||||
* (OAuth2RestTemplate) ReflectionTestUtils.getField(services, "restTemplate");
|
||||
* MockRestServiceServer server = MockRestServiceServer.createServer(template);
|
||||
* server.expect(requestTo("https://example.com"))
|
||||
* .andRespond(withSuccess("{\"id\":\"user\"}", MediaType.APPLICATION_JSON));
|
||||
* services.loadAuthentication("FOO");
|
||||
* assertThat(client.getAccessToken().getValue()).isEqualTo("FOO");
|
||||
* server.verify();
|
||||
*/
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableResourceServer
|
||||
// @EnableResourceServer
|
||||
protected static class NoClientConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableResourceServer
|
||||
@EnableOAuth2Sso
|
||||
// @EnableResourceServer
|
||||
// @EnableOAuth2Sso
|
||||
protected static class ClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails resource,
|
||||
OAuth2ClientContext oauth2Context) {
|
||||
return new OAuth2RestTemplate(resource, oauth2Context);
|
||||
}
|
||||
/*
|
||||
* @Bean public OAuth2RestTemplate
|
||||
* oauth2RestTemplate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext
|
||||
* oauth2Context) { return new OAuth2RestTemplate(resource, oauth2Context); }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.commons.security.tokenrelay;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,7 +28,6 @@ import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.cloud.commons.security.AccessTokenContextRelay;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -35,17 +35,10 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.oauth2.client.OAuth2ClientContext;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
@@ -54,6 +47,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||
* @author Peter Szanto (spring@szantocsalad.hu)
|
||||
*
|
||||
*/
|
||||
@Disabled
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
|
||||
properties = { "security.oauth2.resource.jwt.keyValue=secret", "spring.cloud.mvc.token-relay.enabled=true",
|
||||
"spring.autoconfigure.exclude=" })
|
||||
@@ -92,7 +86,7 @@ public class ResourceServerTokenRelayTests {
|
||||
assertThat(exchange.getBody()).isEqualTo(TEST_RESPONSE);
|
||||
|
||||
mockServerToReceiveRelay.verify();
|
||||
verify(accessTokenContextRelay).copyToken();
|
||||
// verify(accessTokenContextRelay).copyToken();
|
||||
}
|
||||
|
||||
private HttpEntity<String> createAuthorizationHeader() {
|
||||
@@ -104,22 +98,21 @@ public class ResourceServerTokenRelayTests {
|
||||
|
||||
@SpringBootApplication
|
||||
@TestConfiguration
|
||||
@EnableResourceServer
|
||||
// @EnableResourceServer
|
||||
@ComponentScan(basePackageClasses = TokenRelayTestController.class)
|
||||
@EnableOAuth2Client
|
||||
// @EnableOAuth2Client
|
||||
protected static class ClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ProtectedResourceDetails resource,
|
||||
OAuth2ClientContext oauth2Context) {
|
||||
return new OAuth2RestTemplate(resource, oauth2Context);
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MockRestServiceServer mockRestServiceServer(OAuth2RestTemplate template) {
|
||||
return MockRestServiceServer.createServer(template);
|
||||
}
|
||||
/*
|
||||
* @Bean public OAuth2RestTemplate
|
||||
* oauth2RestTemplate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext
|
||||
* oauth2Context) { return new OAuth2RestTemplate(resource, oauth2Context);
|
||||
*
|
||||
* }
|
||||
*
|
||||
* @Bean public MockRestServiceServer mockRestServiceServer(OAuth2RestTemplate
|
||||
* template) { return MockRestServiceServer.createServer(template); }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -127,15 +120,16 @@ public class ResourceServerTokenRelayTests {
|
||||
@TestComponent
|
||||
protected static class TokenRelayTestController {
|
||||
|
||||
@Autowired
|
||||
OAuth2RestTemplate oAuth2RestTemplate;
|
||||
|
||||
@GetMapping("/token-relay")
|
||||
public String callAnotherService() {
|
||||
|
||||
return oAuth2RestTemplate.getForEntity("https://example.com/test", String.class).getBody();
|
||||
|
||||
}
|
||||
/*
|
||||
* @Autowired OAuth2RestTemplate oAuth2RestTemplate;
|
||||
*
|
||||
* @GetMapping("/token-relay") public String callAnotherService() {
|
||||
*
|
||||
* return oAuth2RestTemplate.getForEntity("https://example.com/test",
|
||||
* String.class).getBody();
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,6 @@ public class LoadBalancerCacheAutoConfiguration {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(LoadBalancerCaffeineWarnLogger.class);
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
|
||||
@@ -16,45 +16,35 @@
|
||||
|
||||
package org.springframework.cloud.loadbalancer.security;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
||||
import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(OAuth2RestTemplate.class)
|
||||
// @ConditionalOnClass(OAuth2RestTemplate.class)
|
||||
@ConditionalOnProperty("spring.cloud.oauth2.load-balanced.enabled")
|
||||
@AutoConfigureAfter(OAuth2AutoConfiguration.class)
|
||||
// @AutoConfigureAfter(OAuth2AutoConfiguration.class)
|
||||
public class OAuth2LoadBalancerClientAutoConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBean(LoadBalancerInterceptor.class)
|
||||
protected static class UserInfoLoadBalancerConfig {
|
||||
|
||||
@Bean
|
||||
public UserInfoRestTemplateCustomizer loadBalancedUserInfoRestTemplateCustomizer(
|
||||
final LoadBalancerInterceptor loadBalancerInterceptor) {
|
||||
return restTemplate -> {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(restTemplate.getInterceptors());
|
||||
interceptors.add(loadBalancerInterceptor);
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
};
|
||||
}
|
||||
/*
|
||||
* @Bean public UserInfoRestTemplateCustomizer
|
||||
* loadBalancedUserInfoRestTemplateCustomizer( final LoadBalancerInterceptor
|
||||
* loadBalancerInterceptor) { return restTemplate -> {
|
||||
* List<ClientHttpRequestInterceptor> interceptors = new
|
||||
* ArrayList<>(restTemplate.getInterceptors());
|
||||
* interceptors.add(loadBalancerInterceptor);
|
||||
* restTemplate.setInterceptors(interceptors); }; }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -62,18 +52,18 @@ public class OAuth2LoadBalancerClientAutoConfiguration {
|
||||
@ConditionalOnBean(RetryLoadBalancerInterceptor.class)
|
||||
protected static class UserInfoRetryLoadBalancerConfig {
|
||||
|
||||
@Bean
|
||||
public UserInfoRestTemplateCustomizer retryLoadBalancedUserInfoRestTemplateCustomizer(
|
||||
final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
|
||||
return new UserInfoRestTemplateCustomizer() {
|
||||
@Override
|
||||
public void customize(OAuth2RestTemplate restTemplate) {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(restTemplate.getInterceptors());
|
||||
interceptors.add(loadBalancerInterceptor);
|
||||
restTemplate.setInterceptors(interceptors);
|
||||
}
|
||||
};
|
||||
}
|
||||
/*
|
||||
* @Bean public UserInfoRestTemplateCustomizer
|
||||
* retryLoadBalancedUserInfoRestTemplateCustomizer( final
|
||||
* RetryLoadBalancerInterceptor loadBalancerInterceptor) { return new
|
||||
* UserInfoRestTemplateCustomizer() {
|
||||
*
|
||||
* @Override public void customize(OAuth2RestTemplate restTemplate) {
|
||||
* List<ClientHttpRequestInterceptor> interceptors = new
|
||||
* ArrayList<>(restTemplate.getInterceptors());
|
||||
* interceptors.add(loadBalancerInterceptor);
|
||||
* restTemplate.setInterceptors(interceptors); } }; }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -16,27 +16,21 @@
|
||||
|
||||
package org.springframework.cloud.loadbalancer.security;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.test.ClassPathExclusions;
|
||||
import org.springframework.cloud.test.ModifiedClassPathRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -69,6 +63,7 @@ public class OAuth2LoadBalancerClientAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void userInfoNotLoadBalanced() {
|
||||
this.context = new SpringApplicationBuilder(ClientConfiguration.class).properties("spring.config.name=test",
|
||||
"server.port=0", "security.oauth2.resource.userInfoUri:https://example.com").run();
|
||||
@@ -78,6 +73,7 @@ public class OAuth2LoadBalancerClientAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void userInfoLoadBalancedNoRetry() throws Exception {
|
||||
this.context = new SpringApplicationBuilder(ClientConfiguration.class).properties("spring.config.name=test",
|
||||
"server.port=0", "security.oauth2.resource.userInfoUri:https://nosuchservice",
|
||||
@@ -86,16 +82,19 @@ public class OAuth2LoadBalancerClientAutoConfigurationTests {
|
||||
assertThat(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer")).isTrue();
|
||||
assertThat(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer")).isFalse();
|
||||
|
||||
OAuth2RestTemplate template = this.context.getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate();
|
||||
ClientHttpRequest request = template.getRequestFactory().createRequest(new URI("https://nosuchservice"),
|
||||
HttpMethod.GET);
|
||||
expected.expectMessage("No instances available for nosuchservice");
|
||||
request.execute();
|
||||
/*
|
||||
* OAuth2RestTemplate template =
|
||||
* this.context.getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate
|
||||
* (); ClientHttpRequest request = template.getRequestFactory().createRequest(new
|
||||
* URI("https://nosuchservice"), HttpMethod.GET);
|
||||
* expected.expectMessage("No instances available for nosuchservice");
|
||||
* request.execute();
|
||||
*/
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableOAuth2Sso
|
||||
// @EnableOAuth2Sso
|
||||
protected static class ClientConfiguration {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user