Commit 3f4c32fc authored by Phillip Webb's avatar Phillip Webb

Polish

parent b084e182
...@@ -16,12 +16,6 @@ ...@@ -16,12 +16,6 @@
package org.springframework.boot.autoconfigure.security.oauth2.client; 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 javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
...@@ -76,7 +70,7 @@ import org.springframework.util.StringUtils; ...@@ -76,7 +70,7 @@ import org.springframework.util.StringUtils;
public class OAuth2RestOperationsConfiguration { public class OAuth2RestOperationsConfiguration {
@Configuration @Configuration
@ConditionalOnClientCredentials @Conditional(ClientCredentialsCondition.class)
protected static class SingletonScopedConfiguration { protected static class SingletonScopedConfiguration {
@Bean @Bean
...@@ -96,7 +90,7 @@ public class OAuth2RestOperationsConfiguration { ...@@ -96,7 +90,7 @@ public class OAuth2RestOperationsConfiguration {
@Configuration @Configuration
@ConditionalOnBean(OAuth2ClientConfiguration.class) @ConditionalOnBean(OAuth2ClientConfiguration.class)
@ConditionalOnNotClientCredentials @Conditional(NoClientCredentialsCondition.class)
@Import(OAuth2ProtectedResourceDetailsConfiguration.class) @Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class SessionScopedConfiguration { protected static class SessionScopedConfiguration {
...@@ -126,15 +120,13 @@ public class OAuth2RestOperationsConfiguration { ...@@ -126,15 +120,13 @@ public class OAuth2RestOperationsConfiguration {
} }
/* // When the authentication is per cookie but the stored token is an oauth2 one, we can
* 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
* 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
* OAuth2ClientContextFilter until we need to refresh the access token. To handle // refresh tokens you need to @EnableOAuth2Client
* refresh tokens you need to {@code @EnableOAuth2Client}
*/
@Configuration @Configuration
@ConditionalOnMissingBean(OAuth2ClientConfiguration.class) @ConditionalOnMissingBean(OAuth2ClientConfiguration.class)
@ConditionalOnNotClientCredentials @Conditional(NoClientCredentialsCondition.class)
@Import(OAuth2ProtectedResourceDetailsConfiguration.class) @Import(OAuth2ProtectedResourceDetailsConfiguration.class)
protected static class RequestScopedConfiguration { protected static class RequestScopedConfiguration {
...@@ -182,22 +174,24 @@ public class OAuth2RestOperationsConfiguration { ...@@ -182,22 +174,24 @@ public class OAuth2RestOperationsConfiguration {
} }
@Conditional(ClientCredentialsCondition.class) /**
@Target({ ElementType.TYPE, ElementType.METHOD }) * Condition to check for no client credentials.
@Retention(RetentionPolicy.RUNTIME) */
@Documented static class NoClientCredentialsCondition extends NoneNestedConditions {
public static @interface ConditionalOnClientCredentials {
} NoClientCredentialsCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@Conditional(NotClientCredentialsCondition.class) @Conditional(ClientCredentialsCondition.class)
@Target({ ElementType.TYPE, ElementType.METHOD }) static class ClientCredentialsActivated {
@Retention(RetentionPolicy.RUNTIME) }
@Documented
public static @interface ConditionalOnNotClientCredentials {
} }
/**
* Condition to check for client credentials.
*/
static class ClientCredentialsCondition extends AnyNestedCondition { static class ClientCredentialsCondition extends AnyNestedCondition {
ClientCredentialsCondition() { ClientCredentialsCondition() {
...@@ -211,17 +205,6 @@ public class OAuth2RestOperationsConfiguration { ...@@ -211,17 +205,6 @@ public class OAuth2RestOperationsConfiguration {
@ConditionalOnNotWebApplication @ConditionalOnNotWebApplication
static class NoWebApplication { 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; ...@@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
...@@ -196,8 +195,8 @@ public class OAuth2AutoConfigurationTests { ...@@ -196,8 +195,8 @@ public class OAuth2AutoConfigurationTests {
"security.oauth2.client.clientId=client", "security.oauth2.client.clientId=client",
"security.oauth2.client.grantType=client_credentials"); "security.oauth2.client.grantType=client_credentials");
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(OAuth2ClientContext.class).getAccessTokenRequest()) OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
.isNotNull(); assertThat(bean.getAccessTokenRequest()).isNotNull();
assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1); assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(1); assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(1);
} }
...@@ -211,17 +210,15 @@ public class OAuth2AutoConfigurationTests { ...@@ -211,17 +210,15 @@ public class OAuth2AutoConfigurationTests {
"security.oauth2.client.clientId=client", "security.oauth2.client.clientId=client",
"security.oauth2.client.grantType=client_credentials"); "security.oauth2.client.grantType=client_credentials");
this.context.refresh(); this.context.refresh();
// Thr primary context is fine (not session scoped): // The primary context is fine (not session scoped):
assertThat(this.context.getBean(OAuth2ClientContext.class).getAccessTokenRequest()) OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
.isNotNull(); assertThat(bean.getAccessTokenRequest()).isNotNull();
assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1); assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
/* // Kind of a bug (should ideally be 1), but the cause is in Spring OAuth2 (there
* 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
* 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,
* 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
* it will still just about work (because of the @Primary annotation on the // Boot-created instance of OAuth2ClientContext).
* Boot-created instance of OAuth2ClientContext).
*/
assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(2); assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(2);
} }
......
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