From 0c3b34f7d5448f26b4484856bdabb5f225d37453 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 5 Jun 2015 16:10:29 +0200 Subject: [PATCH] Polish CORS global configuration This commit introduces the following changes: - configureCors(CorsConfigurer configurer) is renamed to addCorsMappings(CorsRegistry registry) - enableCors(String... pathPatterns) is renamed to addMapping(String pathPattern) - element must have at least one child element in order to be consistent with XML based configuration and have more explicit configuration Issues: SPR-12933, SPR-13046 --- .../config/annotation/CorsRegistration.java | 10 ++--- ...{CorsConfigurer.java => CorsRegistry.java} | 20 +++++----- .../DelegatingWebMvcConfiguration.java | 4 +- .../WebMvcConfigurationSupport.java | 10 ++--- .../config/annotation/WebMvcConfigurer.java | 4 +- .../annotation/WebMvcConfigurerAdapter.java | 2 +- .../annotation/WebMvcConfigurerComposite.java | 4 +- .../web/servlet/config/spring-mvc-4.2.xsd | 8 ++-- ...gurerTests.java => CorsRegistryTests.java} | 39 +++++++------------ ...MvcConfigurationSupportExtensionTests.java | 4 +- .../config/mvc-config-cors-minimal.xml | 4 +- 11 files changed, 48 insertions(+), 61 deletions(-) rename spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/{CorsConfigurer.java => CorsRegistry.java} (73%) rename spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/{CorsConfigurerTests.java => CorsRegistryTests.java} (58%) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java index 24bf7351e81..572f1f54a83 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java @@ -34,12 +34,12 @@ import org.springframework.web.cors.CorsConfiguration; */ public class CorsRegistration { - private final String[] pathPatterns; + private final String pathPattern; private final CorsConfiguration config; - public CorsRegistration(String... pathPatterns) { - this.pathPatterns = (pathPatterns.length == 0 ? new String[]{ "/**" } : pathPatterns); + public CorsRegistration(String pathPattern) { + this.pathPattern = pathPattern; // Same default values than @CrossOrigin annotation + allows simple methods this.config = new CorsConfiguration(); this.config.addAllowedOrigin("*"); @@ -81,8 +81,8 @@ public class CorsRegistration { return this; } - protected String[] getPathPatterns() { - return this.pathPatterns; + protected String getPathPattern() { + return this.pathPattern; } protected CorsConfiguration getCorsConfiguration() { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistry.java similarity index 73% rename from spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsConfigurer.java rename to spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistry.java index 7fbb6e30b4e..be129d138ec 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistry.java @@ -24,25 +24,27 @@ import java.util.Map; import org.springframework.web.cors.CorsConfiguration; /** - * Assist with the registration of {@link CorsConfiguration} mapped to one or more path patterns. + * Assist with the registration of {@link CorsConfiguration} mapped on a path pattern. * @author Sebastien Deleuze * * @since 4.2 * @see CorsRegistration */ -public class CorsConfigurer { +public class CorsRegistry { private final List registrations = new ArrayList(); /** - * Enable cross origin requests on the specified path patterns. If no path pattern is specified, - * cross-origin request handling is mapped on "/**" . + * Enable cross origin requests processing on the specified path pattern. + * Exact path mapping URIs (such as "/admin") are supported as well as Ant-stype path + * patterns (such as /admin/**). * - *

By default, all origins, all headers and credentials are allowed. Max age is set to 30 minutes.

+ *

By default, all origins, all headers, credentials and GET, HEAD, POST methods are allowed. + * Max age is set to 30 minutes.

*/ - public CorsRegistration enableCors(String... pathPatterns) { - CorsRegistration registration = new CorsRegistration(pathPatterns); + public CorsRegistration addMapping(String pathPattern) { + CorsRegistration registration = new CorsRegistration(pathPattern); this.registrations.add(registration); return registration; } @@ -50,9 +52,7 @@ public class CorsConfigurer { protected Map getCorsConfigurations() { Map configs = new LinkedHashMap(this.registrations.size()); for (CorsRegistration registration : this.registrations) { - for (String pathPattern : registration.getPathPatterns()) { - configs.put(pathPattern, registration.getCorsConfiguration()); - } + configs.put(registration.getPathPattern(), registration.getCorsConfiguration()); } return configs; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java index 148ea078849..084acd90c6f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java @@ -133,8 +133,8 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { } @Override - protected void configureCors(CorsConfigurer configurer) { - this.configurers.configureCors(configurer); + protected void addCorsMappings(CorsRegistry registry) { + this.configurers.addCorsMappings(registry); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 57edc7b8403..65dd5fd9aa5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -875,19 +875,19 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv */ protected final Map getCorsConfigurations() { if (this.corsConfigurations == null) { - CorsConfigurer registry = new CorsConfigurer(); - configureCors(registry); + CorsRegistry registry = new CorsRegistry(); + addCorsMappings(registry); this.corsConfigurations = registry.getCorsConfigurations(); } return this.corsConfigurations; } /** - * Override this method to configure cross-origin requests handling. + * Override this method to configure cross origin requests processing. * @since 4.2 - * @see CorsConfigurer + * @see CorsRegistry */ - protected void configureCors(CorsConfigurer configurer) { + protected void addCorsMappings(CorsRegistry registry) { } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java index 8f5114a6dc8..3e9c509f7e9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.java @@ -183,9 +183,9 @@ public interface WebMvcConfigurer { void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); /** - * Configure cross-origin requests handling. + * Configure cross origin requests processing. * @since 4.2 */ - void configureCors(CorsConfigurer configurer); + void addCorsMappings(CorsRegistry registry); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java index 71e689d09ee..90d4b6aefe1 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.java @@ -170,7 +170,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { *

This implementation is empty. */ @Override - public void configureCors(CorsConfigurer configurer) { + public void addCorsMappings(CorsRegistry registry) { } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java index c845683782b..c88ba0ff3b6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurerComposite.java @@ -154,9 +154,9 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer { } @Override - public void configureCors(CorsConfigurer configurer) { + public void addCorsMappings(CorsRegistry registry) { for (WebMvcConfigurer delegate : this.delegates) { - delegate.configureCors(configurer); + delegate.addCorsMappings(registry); } } diff --git a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.2.xsd b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.2.xsd index 01808861848..fc8dab620b8 100644 --- a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.2.xsd +++ b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.2.xsd @@ -1237,17 +1237,15 @@ - + diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsRegistryTests.java similarity index 58% rename from spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsConfigurerTests.java rename to spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsRegistryTests.java index ccc0e9b0ba9..3fa78447730 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/CorsRegistryTests.java @@ -27,50 +27,37 @@ import org.junit.Test; import org.springframework.web.cors.CorsConfiguration; /** - * Test fixture with a {@link CorsConfigurer}. + * Test fixture with a {@link CorsRegistry}. * * @author Sebastien Deleuze */ -public class CorsConfigurerTests { +public class CorsRegistryTests { - private CorsConfigurer configurer; + private CorsRegistry registry; @Before public void setUp() { - this.configurer = new CorsConfigurer(); + this.registry = new CorsRegistry(); } @Test - public void noCorsConfigured() { - assertTrue(this.configurer.getCorsConfigurations().isEmpty()); + public void noMapping() { + assertTrue(this.registry.getCorsConfigurations().isEmpty()); } @Test - public void multipleCorsConfigured() { - this.configurer.enableCors("/foo"); - this.configurer.enableCors("/bar"); - assertEquals(2, this.configurer.getCorsConfigurations().size()); + public void multipleMappings() { + this.registry.addMapping("/foo"); + this.registry.addMapping("/bar"); + assertEquals(2, this.registry.getCorsConfigurations().size()); } @Test - public void defaultCorsRegistration() { - this.configurer.enableCors(); - Map configs = this.configurer.getCorsConfigurations(); - assertEquals(1, configs.size()); - CorsConfiguration config = configs.get("/**"); - assertEquals(Arrays.asList("*"), config.getAllowedOrigins()); - assertEquals(Arrays.asList("GET", "HEAD", "POST"), config.getAllowedMethods()); - assertEquals(Arrays.asList("*"), config.getAllowedHeaders()); - assertEquals(true, config.getAllowCredentials()); - assertEquals(Long.valueOf(1800), config.getMaxAge()); - } - - @Test - public void customizedCorsRegistration() { - this.configurer.enableCors("/foo").allowedOrigins("http://domain2.com", "http://domain2.com") + public void customizedMapping() { + this.registry.addMapping("/foo").allowedOrigins("http://domain2.com", "http://domain2.com") .allowedMethods("DELETE").allowCredentials(false).allowedHeaders("header1", "header2") .exposedHeaders("header3", "header4").maxAge(3600); - Map configs = this.configurer.getCorsConfigurations(); + Map configs = this.registry.getCorsConfigurations(); assertEquals(1, configs.size()); CorsConfiguration config = configs.get("/foo"); assertEquals(Arrays.asList("http://domain2.com", "http://domain2.com"), config.getAllowedOrigins()); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java index 312b197ad49..42ab5fc6587 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java @@ -404,8 +404,8 @@ public class WebMvcConfigurationSupportExtensionTests { } @Override - public void configureCors(CorsConfigurer registry) { - registry.enableCors("/resources/**"); + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/resources/**"); } } diff --git a/spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-cors-minimal.xml b/spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-cors-minimal.xml index 3e5ba455a1a..fdd16129aba 100644 --- a/spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-cors-minimal.xml +++ b/spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-cors-minimal.xml @@ -6,7 +6,9 @@ http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> - + + +