From 00fbb5c3d92088ac434a109f62e51eebfa57dc4d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 16 May 2016 12:00:03 -0700 Subject: [PATCH] Polish --- .../oauth2/client/SsoSecurityConfigurer.java | 4 ++-- ...ourceServerTokenServicesConfiguration.java | 21 +++++++++++----- ....java => UserInfoRestTemplateFactory.java} | 24 +++++++++---------- .../boot/json/GsonJsonParser.java | 12 ++++++---- .../boot/json/JacksonJsonParser.java | 12 ++++++---- ...Tests.java => JacksonJsonParserTests.java} | 6 ++--- 6 files changed, 48 insertions(+), 31 deletions(-) rename spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/{UserInfoRestTemplateConfiguration.java => UserInfoRestTemplateFactory.java} (85%) rename spring-boot/src/test/java/org/springframework/boot/json/{JacksonParserTests.java => JacksonJsonParserTests.java} (81%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/SsoSecurityConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/SsoSecurityConfigurer.java index b9ce977916..457463d8e9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/SsoSecurityConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/SsoSecurityConfigurer.java @@ -18,7 +18,7 @@ package org.springframework.boot.autoconfigure.security.oauth2.client; import java.util.Collections; -import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateConfiguration; +import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory; import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -84,7 +84,7 @@ class SsoSecurityConfigurer { private OAuth2ClientAuthenticationProcessingFilter oauth2SsoFilter( OAuth2SsoProperties sso) { OAuth2RestOperations restTemplate = this.applicationContext - .getBean(UserInfoRestTemplateConfiguration.class).userInfoRestTemplate(); + .getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate(); ResourceServerTokenServices tokenServices = this.applicationContext .getBean(ResourceServerTokenServices.class); OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter( diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java index 2b154b27ce..a724d6d625 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerTokenServicesConfiguration.java @@ -36,7 +36,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -49,6 +48,7 @@ import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.security.crypto.codec.Base64; +import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.client.OAuth2RestOperations; import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; import org.springframework.security.oauth2.client.token.AccessTokenRequest; @@ -76,12 +76,21 @@ import org.springframework.web.client.RestTemplate; */ @Configuration @ConditionalOnMissingBean(AuthorizationServerEndpointsConfiguration.class) -@Import(UserInfoRestTemplateConfiguration.class) public class ResourceServerTokenServicesConfiguration { private static final Log logger = LogFactory .getLog(ResourceServerTokenServicesConfiguration.class); + @Bean + @ConditionalOnMissingBean + public UserInfoRestTemplateFactory userInfoRestTemplateFactory( + ObjectProvider> customizersProvider, + ObjectProvider detailsProvider, + ObjectProvider oauth2ClientContextProvider) { + return new UserInfoRestTemplateFactory(customizersProvider, detailsProvider, + oauth2ClientContextProvider); + } + @Configuration @Conditional(NotJwtTokenCondition.class) protected static class RemoteTokenServicesConfiguration { @@ -122,11 +131,11 @@ public class ResourceServerTokenServicesConfiguration { public SocialTokenServicesConfiguration(ResourceServerProperties sso, ObjectProvider> connectionFactoryProvider, - UserInfoRestTemplateConfiguration restTemplateProvider, + UserInfoRestTemplateFactory restTemplateFactory, ObjectProvider authoritiesExtractorProvider) { this.sso = sso; this.connectionFactory = connectionFactoryProvider.getIfAvailable(); - this.restTemplate = restTemplateProvider.userInfoRestTemplate(); + this.restTemplate = restTemplateFactory.getUserInfoRestTemplate(); this.authoritiesExtractor = authoritiesExtractorProvider.getIfAvailable(); } @@ -166,10 +175,10 @@ public class ResourceServerTokenServicesConfiguration { private final AuthoritiesExtractor authoritiesExtractor; public UserInfoTokenServicesConfiguration(ResourceServerProperties sso, - UserInfoRestTemplateConfiguration restTemplateProvider, + UserInfoRestTemplateFactory restTemplateFactory, ObjectProvider authoritiesExtractorProvider) { this.sso = sso; - this.restTemplate = restTemplateProvider.userInfoRestTemplate(); + this.restTemplate = restTemplateFactory.getUserInfoRestTemplate(); this.authoritiesExtractor = authoritiesExtractorProvider.getIfAvailable(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateFactory.java similarity index 85% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateConfiguration.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateFactory.java index cc7922fd4d..cfe4355410 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/UserInfoRestTemplateFactory.java @@ -31,22 +31,23 @@ import org.springframework.security.oauth2.client.token.grant.code.Authorization import org.springframework.util.CollectionUtils; /** - * Configuration acting as a factory for the rest template used for extracting user info - * during authentication. + * Factory used to create the rest template used for extracting user info during + * authentication. * * @author Dave Syer + * @since 1.4.0 */ @Configuration -public class UserInfoRestTemplateConfiguration { +public class UserInfoRestTemplateFactory { - private static final AuthorizationCodeResourceDetails DEFAULT_RESOURCE_DETAILS = new AuthorizationCodeResourceDetails(); + private static final AuthorizationCodeResourceDetails DEFAULT_RESOURCE_DETAILS; static { - DEFAULT_RESOURCE_DETAILS.setClientId(""); - DEFAULT_RESOURCE_DETAILS - .setUserAuthorizationUri("Not a URI " + "because there is no client"); - DEFAULT_RESOURCE_DETAILS - .setAccessTokenUri("Not a URI " + "because there is no client"); + AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); + details.setClientId(""); + details.setUserAuthorizationUri("Not a URI because there is no client"); + details.setAccessTokenUri("Not a URI because there is no client"); + DEFAULT_RESOURCE_DETAILS = details; } private final List customizers; @@ -57,7 +58,7 @@ public class UserInfoRestTemplateConfiguration { private OAuth2RestTemplate template; - public UserInfoRestTemplateConfiguration( + public UserInfoRestTemplateFactory( ObjectProvider> customizersProvider, ObjectProvider detailsProvider, ObjectProvider oauth2ClientContextProvider) { @@ -66,8 +67,7 @@ public class UserInfoRestTemplateConfiguration { this.oauth2ClientContext = oauth2ClientContextProvider.getIfAvailable(); } - // Not a @Bean: use this method as a factory - public OAuth2RestTemplate userInfoRestTemplate() { + public OAuth2RestTemplate getUserInfoRestTemplate() { if (this.template == null) { this.template = getTemplate( this.details == null ? DEFAULT_RESOURCE_DETAILS : this.details); diff --git a/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java index 86a3cf5b5b..148fd146dc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 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. @@ -40,8 +40,7 @@ public class GsonJsonParser implements JsonParser { if (json != null) { json = json.trim(); if (json.startsWith("{")) { - TypeToken> type = new TypeToken>() { }; - return this.gson.fromJson(json, type.getType()); + return this.gson.fromJson(json, new MapTypeToken().getType()); } } throw new IllegalArgumentException("Cannot parse JSON"); @@ -52,11 +51,16 @@ public class GsonJsonParser implements JsonParser { if (json != null) { json = json.trim(); if (json.startsWith("[")) { - TypeToken> type = new TypeToken>() { }; + TypeToken> type = new TypeToken>() { + }; return this.gson.fromJson(json, type.getType()); } } throw new IllegalArgumentException("Cannot parse JSON"); } + private static final class MapTypeToken extends TypeToken> { + + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java index 0421979500..97d5e4ed97 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 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. @@ -33,8 +33,7 @@ public class JacksonJsonParser implements JsonParser { @Override public Map parseMap(String json) { try { - TypeReference> type = new TypeReference>() { }; - return new ObjectMapper().readValue(json, type); + return new ObjectMapper().readValue(json, new MapTypeReference()); } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse JSON", ex); @@ -44,7 +43,8 @@ public class JacksonJsonParser implements JsonParser { @Override public List parseList(String json) { try { - TypeReference> type = new TypeReference>() { }; + TypeReference> type = new TypeReference>() { + }; return new ObjectMapper().readValue(json, type); } catch (Exception ex) { @@ -52,4 +52,8 @@ public class JacksonJsonParser implements JsonParser { } } + private static class MapTypeReference extends TypeReference> { + + }; + } diff --git a/spring-boot/src/test/java/org/springframework/boot/json/JacksonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/JacksonJsonParserTests.java similarity index 81% rename from spring-boot/src/test/java/org/springframework/boot/json/JacksonParserTests.java rename to spring-boot/src/test/java/org/springframework/boot/json/JacksonJsonParserTests.java index 3927a75a05..263b3b3b21 100644 --- a/spring-boot/src/test/java/org/springframework/boot/json/JacksonParserTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/json/JacksonJsonParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 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. @@ -17,11 +17,11 @@ package org.springframework.boot.json; /** - * Tests for {@link JsonParser}. + * Tests for {@link JacksonJsonParser}. * * @author Dave Syer */ -public class JacksonParserTests extends AbstractJsonParserTests { +public class JacksonJsonParserTests extends AbstractJsonParserTests { @Override protected JsonParser getParser() {