Polish CORS support classes

- Simplified "check" algorithms in CorsConfiguration

- Improved robustness of setter methods in CorsConfiguration in order to
  avoid attempts to modify immutable lists

- Improved CORS documentation and fixed typo

- Introduced constants in CorsConfiguration

- Removed auto-boxing in CorsRegistration
This commit is contained in:
Sam Brannen
2015-06-20 16:12:18 +02:00
parent 27d1ce84a3
commit de9f27872e
4 changed files with 162 additions and 102 deletions

View File

@@ -23,14 +23,21 @@ import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
/**
* Assists with the creation of a {@link CorsConfiguration} mapped to one or more path patterns.
* If no path pattern is specified, cross-origin request handling is mapped on "/**" .
* {@code CorsRegistration} assists with the creation of a
* {@link CorsConfiguration} instance mapped to a path pattern.
*
* <p>By default, all origins, all headers, credentials and GET, HEAD, POST methods are allowed.
* Max age is set to 30 minutes.</p>
* <p>If no path pattern is specified, cross-origin request handling is
* mapped to {@code "/**"}.
*
* <p>By default, all origins, all headers, credentials and {@code GET},
* {@code HEAD}, and {@code POST} methods are allowed, and the max age is
* set to 30 minutes.
*
* @author Sebastien Deleuze
* @author Sam Brannen
* @since 4.2
* @see CorsConfiguration
* @see CorsRegistry
*/
public class CorsRegistration {
@@ -40,15 +47,15 @@ public class CorsRegistration {
public CorsRegistration(String pathPattern) {
this.pathPattern = pathPattern;
// Same default values than @CrossOrigin annotation + allows simple methods
// Same implicit default values as the @CrossOrigin annotation + allows simple methods
this.config = new CorsConfiguration();
this.config.addAllowedOrigin("*");
this.config.addAllowedMethod(HttpMethod.GET.name());
this.config.addAllowedMethod(HttpMethod.HEAD.name());
this.config.addAllowedMethod(HttpMethod.POST.name());
this.config.addAllowedHeader("*");
this.config.setAllowCredentials(true);
this.config.setMaxAge(1800L);
this.config.addAllowedOrigin(CorsConfiguration.ALL);
this.config.addAllowedMethod(HttpMethod.GET);
this.config.addAllowedMethod(HttpMethod.HEAD);
this.config.addAllowedMethod(HttpMethod.POST);
this.config.addAllowedHeader(CorsConfiguration.ALL);
this.config.setAllowCredentials(Boolean.TRUE);
this.config.setMaxAge(CorsConfiguration.DEFAULT_MAX_AGE);
}
public CorsRegistration allowedOrigins(String... origins) {

View File

@@ -24,9 +24,10 @@ import java.util.Map;
import org.springframework.web.cors.CorsConfiguration;
/**
* Assist with the registration of {@link CorsConfiguration} mapped on a path pattern.
* @author Sebastien Deleuze
* {@code CorsRegistry} assists with the registration of {@link CorsConfiguration}
* mapped to a path pattern.
*
* @author Sebastien Deleuze
* @since 4.2
* @see CorsRegistration
*/
@@ -36,12 +37,14 @@ public class CorsRegistry {
/**
* 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/**).
* Enable cross origin request handling for the specified path pattern.
*
* <p>By default, all origins, all headers, credentials and GET, HEAD, POST methods are allowed.
* Max age is set to 30 minutes.</p>
* <p>Exact path mapping URIs (such as {@code "/admin"}) are supported as
* well as Ant-style path patterns (such as {@code "/admin/**"}).
*
* <p>By default, all origins, all headers, credentials and {@code GET},
* {@code HEAD}, and {@code POST} methods are allowed, and the max age
* is set to 30 minutes.
*/
public CorsRegistration addMapping(String pathPattern) {
CorsRegistration registration = new CorsRegistration(pathPattern);