Moves classes from spring-cloud-security to spring-cloud-commons

This commit is contained in:
Marcin Grzejszczak
2020-12-18 17:21:10 +01:00
parent 18b7e0cc9d
commit 1f0f750707
14 changed files with 754 additions and 3 deletions

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2015-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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)
@ConditionalOnProperty("spring.cloud.oauth2.load-balanced.enabled=true")
@AutoConfigureAfter(OAuth2AutoConfiguration.class)
public class OAuth2LoadBalancerClientAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(LoadBalancerInterceptor.class)
protected static class UserInfoLoadBalancerConfig {
@Bean
public UserInfoRestTemplateCustomizer loadBalancedUserInfoRestTemplateCustomizer(
final LoadBalancerInterceptor loadBalancerInterceptor) {
return new UserInfoRestTemplateCustomizer() {
@Override
public void customize(OAuth2RestTemplate restTemplate) {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(restTemplate.getInterceptors());
interceptors.add(loadBalancerInterceptor);
restTemplate.setInterceptors(interceptors);
}
};
}
}
@Configuration(proxyBeanMethods = false)
@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);
}
};
}
}
}

View File

@@ -2,4 +2,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration,\
org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration,\
org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration
org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration,\
org.springframework.cloud.loadbalancer.security.OAuth2LoadBalancerClientAutoConfiguration

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2015-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.loadbalancer.security;
import java.net.URI;
import org.junit.After;
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;
/**
* @author Dave Syer
*
*/
@RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("spring-retry-*.jar")
public class OAuth2LoadBalancerClientAutoConfigurationTests {
private ConfigurableApplicationContext context;
@Rule
public ExpectedException expected = ExpectedException.none();
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
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();
assertThat(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer")).isFalse();
assertThat(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer")).isFalse();
}
@Test
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",
"spring.cloud.oauth2.load-balanced.enabled=true").run();
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();
}
@EnableAutoConfiguration
@Configuration(proxyBeanMethods = false)
@EnableOAuth2Sso
protected static class ClientConfiguration {
}
}

View File

@@ -25,3 +25,7 @@ spring:
uri: http://hhost
- service-id: myservice
uri: http://ihost
autoconfigure.exclude:
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
- org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration
- org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration