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)
 - <cors /> element must have at least one <mapping /> child
   element in order to be consistent with XML based configuration
   and have more explicit configuration

Issues: SPR-12933, SPR-13046
This commit is contained in:
Sebastien Deleuze
2015-06-05 16:10:29 +02:00
parent e8441edcb7
commit 0c3b34f7d5
11 changed files with 48 additions and 61 deletions

View File

@@ -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() {

View File

@@ -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<CorsRegistration> registrations = new ArrayList<CorsRegistration>();
/**
* 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/**).
*
* <p>By default, all origins, all headers and credentials are allowed. Max age is set to 30 minutes.</p>
* <p>By default, all origins, all headers, credentials and GET, HEAD, POST methods are allowed.
* Max age is set to 30 minutes.</p>
*/
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<String, CorsConfiguration> getCorsConfigurations() {
Map<String, CorsConfiguration> configs = new LinkedHashMap<String, CorsConfiguration>(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;
}

View File

@@ -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);
}
}

View File

@@ -875,19 +875,19 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
*/
protected final Map<String, CorsConfiguration> 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) {
}

View File

@@ -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);
}

View File

@@ -170,7 +170,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
* <p>This implementation is empty.
*/
@Override
public void configureCors(CorsConfigurer configurer) {
public void addCorsMappings(CorsRegistry registry) {
}
}

View File

@@ -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);
}
}