From 8383b761381f0a8dd0b739e8680f5e30e177a25f Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 28 Dec 2017 03:33:47 +0900 Subject: [PATCH 1/2] Move CorsEndpointProperties to the parent package `CorsEndpointProperties` lives in `endpoint.web.servlet` but is also used in `endpoint.web.reactive`, so this PR moves it to its common parent package. This commit also extracts `CorsConfiguration` creation logic duplicated in `WebMvcEndpointManagementContextConfiguration` and `WebFluxEndpointManagementContextConfiguration` into `CorsEndpointProperties`. See gh-11439 --- .../{servlet => }/CorsEndpointProperties.java | 30 +++++++++++++++++-- ...ndpointManagementContextConfiguration.java | 30 ++----------------- ...ndpointManagementContextConfiguration.java | 29 ++---------------- 3 files changed, 32 insertions(+), 57 deletions(-) rename spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/{servlet => }/CorsEndpointProperties.java (76%) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/CorsEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java similarity index 76% rename from spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/CorsEndpointProperties.java rename to spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java index 61c6242bf1..9b3e756bc0 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/CorsEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet; +package org.springframework.boot.actuate.autoconfigure.endpoint.web; import java.time.Duration; import java.time.temporal.ChronoUnit; @@ -23,9 +23,11 @@ import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit; +import org.springframework.util.CollectionUtils; +import org.springframework.web.cors.CorsConfiguration; /** - * Configuration properties for MVC endpoints' CORS support. + * Configuration properties for web endpoints' CORS support. * * @author Andy Wilkinson * @since 2.0.0 @@ -115,4 +117,28 @@ public class CorsEndpointProperties { this.maxAge = maxAge; } + public CorsConfiguration toCorsConfiguration() { + if (CollectionUtils.isEmpty(this.allowedOrigins)) { + return null; + } + CorsConfiguration configuration = new CorsConfiguration(); + configuration.setAllowedOrigins(this.allowedOrigins); + if (!CollectionUtils.isEmpty(this.allowedHeaders)) { + configuration.setAllowedHeaders(this.allowedHeaders); + } + if (!CollectionUtils.isEmpty(this.allowedMethods)) { + configuration.setAllowedMethods(this.allowedMethods); + } + if (!CollectionUtils.isEmpty(this.exposedHeaders)) { + configuration.setExposedHeaders(this.exposedHeaders); + } + if (this.maxAge != null) { + configuration.setMaxAge(this.maxAge.getSeconds()); + } + if (this.allowCredentials != null) { + configuration.setAllowCredentials(this.allowCredentials); + } + return configuration; + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java index 1324fc65d4..5b2947dedc 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java @@ -16,8 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive; +import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; -import org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.CorsEndpointProperties; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes; @@ -32,8 +32,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.endpoint.web.EndpointMapping; import org.springframework.context.annotation.Bean; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.util.CollectionUtils; -import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.reactive.DispatcherHandler; /** @@ -58,31 +56,7 @@ public class WebFluxEndpointManagementContextConfiguration { WebEndpointProperties webEndpointProperties) { return new WebFluxEndpointHandlerMapping( new EndpointMapping(webEndpointProperties.getBasePath()), - endpointDiscoverer.discoverEndpoints(), endpointMediaTypes, getCorsConfiguration(corsProperties)); - } - - private CorsConfiguration getCorsConfiguration(CorsEndpointProperties properties) { - if (CollectionUtils.isEmpty(properties.getAllowedOrigins())) { - return null; - } - CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(properties.getAllowedOrigins()); - if (!CollectionUtils.isEmpty(properties.getAllowedHeaders())) { - configuration.setAllowedHeaders(properties.getAllowedHeaders()); - } - if (!CollectionUtils.isEmpty(properties.getAllowedMethods())) { - configuration.setAllowedMethods(properties.getAllowedMethods()); - } - if (!CollectionUtils.isEmpty(properties.getExposedHeaders())) { - configuration.setExposedHeaders(properties.getExposedHeaders()); - } - if (properties.getMaxAge() != null) { - configuration.setMaxAge(properties.getMaxAge().getSeconds()); - } - if (properties.getAllowCredentials() != null) { - configuration.setAllowCredentials(properties.getAllowCredentials()); - } - return configuration; + endpointDiscoverer.discoverEndpoints(), endpointMediaTypes, corsProperties.toCorsConfiguration()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java index 43ffab96fe..4e1f35f4f2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet; +import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; @@ -30,8 +31,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.endpoint.web.EndpointMapping; import org.springframework.context.annotation.Bean; -import org.springframework.util.CollectionUtils; -import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.servlet.DispatcherServlet; /** @@ -58,32 +57,8 @@ public class WebMvcEndpointManagementContextConfiguration { WebMvcEndpointHandlerMapping handlerMapping = new WebMvcEndpointHandlerMapping( new EndpointMapping(webEndpointProperties.getBasePath()), endpointDiscoverer.discoverEndpoints(), endpointMediaTypes, - getCorsConfiguration(corsProperties)); + corsProperties.toCorsConfiguration()); return handlerMapping; } - private CorsConfiguration getCorsConfiguration(CorsEndpointProperties properties) { - if (CollectionUtils.isEmpty(properties.getAllowedOrigins())) { - return null; - } - CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(properties.getAllowedOrigins()); - if (!CollectionUtils.isEmpty(properties.getAllowedHeaders())) { - configuration.setAllowedHeaders(properties.getAllowedHeaders()); - } - if (!CollectionUtils.isEmpty(properties.getAllowedMethods())) { - configuration.setAllowedMethods(properties.getAllowedMethods()); - } - if (!CollectionUtils.isEmpty(properties.getExposedHeaders())) { - configuration.setExposedHeaders(properties.getExposedHeaders()); - } - if (properties.getMaxAge() != null) { - configuration.setMaxAge(properties.getMaxAge().getSeconds()); - } - if (properties.getAllowCredentials() != null) { - configuration.setAllowCredentials(properties.getAllowCredentials()); - } - return configuration; - } - } From 27381479476236b94f2540545aa35e9708277957 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Dec 2017 11:01:14 +0100 Subject: [PATCH 2/2] Polish "Move CorsEndpointProperties to the parent package" Closes gh-11439 --- .../endpoint/web/CorsEndpointProperties.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java index 9b3e756bc0..6d3c551d1c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit; import org.springframework.util.CollectionUtils; import org.springframework.web.cors.CorsConfiguration; @@ -121,23 +122,19 @@ public class CorsEndpointProperties { if (CollectionUtils.isEmpty(this.allowedOrigins)) { return null; } + PropertyMapper map = PropertyMapper.get(); CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(this.allowedOrigins); - if (!CollectionUtils.isEmpty(this.allowedHeaders)) { - configuration.setAllowedHeaders(this.allowedHeaders); - } - if (!CollectionUtils.isEmpty(this.allowedMethods)) { - configuration.setAllowedMethods(this.allowedMethods); - } - if (!CollectionUtils.isEmpty(this.exposedHeaders)) { - configuration.setExposedHeaders(this.exposedHeaders); - } - if (this.maxAge != null) { - configuration.setMaxAge(this.maxAge.getSeconds()); - } - if (this.allowCredentials != null) { - configuration.setAllowCredentials(this.allowCredentials); - } + map.from(this::getAllowedOrigins).to(configuration::setAllowedOrigins); + map.from(this::getAllowedHeaders).whenNot(CollectionUtils::isEmpty) + .to(configuration::setAllowedHeaders); + map.from(this::getAllowedMethods).whenNot(CollectionUtils::isEmpty) + .to(configuration::setAllowedMethods); + map.from(this::getExposedHeaders).whenNot(CollectionUtils::isEmpty) + .to(configuration::setExposedHeaders); + map.from(this::getMaxAge).whenNonNull().as(Duration::getSeconds) + .to(configuration::setMaxAge); + map.from(this::getAllowCredentials).whenNonNull() + .to(configuration::setAllowCredentials); return configuration; }