Merge branch '1.5.x'

pull/10101/head
Stephane Nicoll 7 years ago
commit f52003f9ed

@ -29,7 +29,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2RestOperationsConfiguration.OAuth2ClientIdCondition;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -64,7 +63,6 @@ import org.springframework.util.StringUtils;
*/ */
@Configuration @Configuration
@ConditionalOnClass(EnableOAuth2Client.class) @ConditionalOnClass(EnableOAuth2Client.class)
@Conditional(OAuth2ClientIdCondition.class)
public class OAuth2RestOperationsConfiguration { public class OAuth2RestOperationsConfiguration {
@Configuration @Configuration
@ -88,7 +86,7 @@ public class OAuth2RestOperationsConfiguration {
@Configuration @Configuration
@ConditionalOnBean(OAuth2ClientConfiguration.class) @ConditionalOnBean(OAuth2ClientConfiguration.class)
@Conditional(NoClientCredentialsCondition.class) @Conditional({OAuth2ClientIdCondition.class, NoClientCredentialsCondition.class})
@Import(OAuth2ProtectedResourceDetailsConfiguration.class) @Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class SessionScopedConfiguration { protected static class SessionScopedConfiguration {
@ -127,7 +125,7 @@ public class OAuth2RestOperationsConfiguration {
// refresh tokens you need to @EnableOAuth2Client // refresh tokens you need to @EnableOAuth2Client
@Configuration @Configuration
@ConditionalOnMissingBean(OAuth2ClientConfiguration.class) @ConditionalOnMissingBean(OAuth2ClientConfiguration.class)
@Conditional(NoClientCredentialsCondition.class) @Conditional({ OAuth2ClientIdCondition.class, NoClientCredentialsCondition.class })
@Import(OAuth2ProtectedResourceDetailsConfiguration.class) @Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class RequestScopedConfiguration { protected static class RequestScopedConfiguration {

@ -19,14 +19,22 @@ package org.springframework.boot.autoconfigure.security.oauth2.client;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.autoconfigure.web.servlet.MockServletWebServerFactory;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -45,23 +53,88 @@ public class OAuth2RestOperationsConfigurationTests {
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Test @Test
public void clientIdConditionMatches() throws Exception { public void clientCredentialsWithClientId() throws Exception {
TestPropertyValues.of("security.oauth2.client.client-id=acme") TestPropertyValues.of("security.oauth2.client.client-id=acme")
.applyTo(this.environment); .applyTo(this.environment);
this.context = new SpringApplicationBuilder( initializeContext(OAuth2RestOperationsConfiguration.class, true);
OAuth2RestOperationsConfiguration.class).environment(this.environment)
.web(WebApplicationType.NONE).run();
assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class)) assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class))
.isNotNull(); .isNotNull();
assertThat(this.context.getBean(ClientCredentialsResourceDetails.class))
.isNotNull();
}
@Test
public void clientCredentialsWithNoClientId() throws Exception {
initializeContext(OAuth2RestOperationsConfiguration.class, true);
assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class))
.isNotNull();
assertThat(this.context.getBean(ClientCredentialsResourceDetails.class))
.isNotNull();
}
@Test
public void requestScopedWithClientId() throws Exception {
TestPropertyValues.of("security.oauth2.client.client-id=acme")
.applyTo(this.environment);
initializeContext(ConfigForRequestScopedConfiguration.class, false);
assertThat(this.context.containsBean("oauth2ClientContext"))
.isTrue();
}
@Test
public void requestScopedWithNoClientId() throws Exception {
initializeContext(ConfigForRequestScopedConfiguration.class, false);
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(DefaultOAuth2ClientContext.class);
}
@Test
public void sessionScopedWithClientId() throws Exception {
TestPropertyValues.of("security.oauth2.client.client-id=acme")
.applyTo(this.environment);
initializeContext(ConfigForSessionScopedConfiguration.class, false);
assertThat(this.context.containsBean("oauth2ClientContext"))
.isTrue();
} }
@Test @Test
public void clientIdConditionDoesNotMatch() throws Exception { public void sessionScopedWithNoClientId() throws Exception {
this.context = new SpringApplicationBuilder( initializeContext(ConfigForSessionScopedConfiguration.class, false);
OAuth2RestOperationsConfiguration.class).environment(this.environment)
.web(WebApplicationType.NONE).run();
this.thrown.expect(NoSuchBeanDefinitionException.class); this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(OAuth2RestOperationsConfiguration.class); this.context.getBean(DefaultOAuth2ClientContext.class);
}
private void initializeContext(Class<?> configuration, boolean isClientCredentials) {
this.context = new SpringApplicationBuilder(configuration)
.environment(this.environment)
.web(!isClientCredentials).run();
}
@Configuration
@Import({ OAuth2RestOperationsConfiguration.class })
protected static class WebApplicationConfiguration {
@Bean
public MockServletWebServerFactory webServerFactory() {
return new MockServletWebServerFactory();
}
}
@Configuration
@Import({ OAuth2ClientConfiguration.class, OAuth2RestOperationsConfiguration.class })
protected static class ConfigForSessionScopedConfiguration extends WebApplicationConfiguration {
@Bean
public SecurityProperties securityProperties() {
return Mockito.mock(SecurityProperties.class);
}
}
@Configuration
protected static class ConfigForRequestScopedConfiguration extends WebApplicationConfiguration {
} }
} }

Loading…
Cancel
Save