pull/7718/merge
Phillip Webb 8 years ago
parent b084e1824c
commit 3f4c32fcdd

@ -16,12 +16,6 @@
package org.springframework.boot.autoconfigure.security.oauth2.client;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
@ -76,7 +70,7 @@ import org.springframework.util.StringUtils;
public class OAuth2RestOperationsConfiguration {
@Configuration
@ConditionalOnClientCredentials
@Conditional(ClientCredentialsCondition.class)
protected static class SingletonScopedConfiguration {
@Bean
@ -96,7 +90,7 @@ public class OAuth2RestOperationsConfiguration {
@Configuration
@ConditionalOnBean(OAuth2ClientConfiguration.class)
@ConditionalOnNotClientCredentials
@Conditional(NoClientCredentialsCondition.class)
@Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class SessionScopedConfiguration {
@ -126,15 +120,13 @@ public class OAuth2RestOperationsConfiguration {
}
/*
* When the authentication is per cookie but the stored token is an oauth2 one, we can
* pass that on to a client that wants to call downstream. We don't even need an
* OAuth2ClientContextFilter until we need to refresh the access token. To handle
* refresh tokens you need to {@code @EnableOAuth2Client}
*/
// When the authentication is per cookie but the stored token is an oauth2 one, we can
// pass that on to a client that wants to call downstream. We don't even need an
// OAuth2ClientContextFilter until we need to refresh the access token. To handle
// refresh tokens you need to @EnableOAuth2Client
@Configuration
@ConditionalOnMissingBean(OAuth2ClientConfiguration.class)
@ConditionalOnNotClientCredentials
@Conditional(NoClientCredentialsCondition.class)
@Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class RequestScopedConfiguration {
@ -182,22 +174,24 @@ public class OAuth2RestOperationsConfiguration {
}
@Conditional(ClientCredentialsCondition.class)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public static @interface ConditionalOnClientCredentials {
/**
* Condition to check for no client credentials.
*/
static class NoClientCredentialsCondition extends NoneNestedConditions {
}
NoClientCredentialsCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@Conditional(NotClientCredentialsCondition.class)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public static @interface ConditionalOnNotClientCredentials {
@Conditional(ClientCredentialsCondition.class)
static class ClientCredentialsActivated {
}
}
/**
* Condition to check for client credentials.
*/
static class ClientCredentialsCondition extends AnyNestedCondition {
ClientCredentialsCondition() {
@ -211,17 +205,6 @@ public class OAuth2RestOperationsConfiguration {
@ConditionalOnNotWebApplication
static class NoWebApplication {
}
}
static class NotClientCredentialsCondition extends NoneNestedConditions {
NotClientCredentialsCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnClientCredentials
static class ClientCredentialsActivated {
}
}

@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
@ -196,8 +195,8 @@ public class OAuth2AutoConfigurationTests {
"security.oauth2.client.clientId=client",
"security.oauth2.client.grantType=client_credentials");
this.context.refresh();
assertThat(this.context.getBean(OAuth2ClientContext.class).getAccessTokenRequest())
.isNotNull();
OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
assertThat(bean.getAccessTokenRequest()).isNotNull();
assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(1);
}
@ -211,17 +210,15 @@ public class OAuth2AutoConfigurationTests {
"security.oauth2.client.clientId=client",
"security.oauth2.client.grantType=client_credentials");
this.context.refresh();
// Thr primary context is fine (not session scoped):
assertThat(this.context.getBean(OAuth2ClientContext.class).getAccessTokenRequest())
.isNotNull();
// The primary context is fine (not session scoped):
OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
assertThat(bean.getAccessTokenRequest()).isNotNull();
assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
/*
* Kind of a bug (should ideally be 1), but the cause is in Spring OAuth2 (there
* is no need for the extra session-scoped bean). What this test proves is that
* even if the user screws up and does @EnableOAuth2Client for client credentials,
* it will still just about work (because of the @Primary annotation on the
* Boot-created instance of OAuth2ClientContext).
*/
// Kind of a bug (should ideally be 1), but the cause is in Spring OAuth2 (there
// is no need for the extra session-scoped bean). What this test proves is that
// even if the user screws up and does @EnableOAuth2Client for client credentials,
// it will still just about work (because of the @Primary annotation on the
// Boot-created instance of OAuth2ClientContext).
assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(2);
}

Loading…
Cancel
Save