From 3f4c32fcddec094787e356238d48adf9e9a41a7e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 20 Dec 2016 11:27:57 -0800 Subject: [PATCH] Polish --- .../OAuth2RestOperationsConfiguration.java | 61 +++++++------------ .../oauth2/OAuth2AutoConfigurationTests.java | 23 +++---- 2 files changed, 32 insertions(+), 52 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration.java index 55e30f672e0..86866d662f4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration.java @@ -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 { - - } - - @Conditional(NotClientCredentialsCondition.class) - @Target({ ElementType.TYPE, ElementType.METHOD }) - @Retention(RetentionPolicy.RUNTIME) - @Documented - public static @interface ConditionalOnNotClientCredentials { + /** + * Condition to check for no client credentials. + */ + static class NoClientCredentialsCondition extends NoneNestedConditions { + + NoClientCredentialsCondition() { + super(ConfigurationPhase.PARSE_CONFIGURATION); + } + + @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 { - } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfigurationTests.java index 750f009b5f9..22bad3fbccf 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/OAuth2AutoConfigurationTests.java @@ -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); }