Skip to content

Commit 882fe12

Browse files
committed
Polish CORS support
1 parent 9c46228 commit 882fe12

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/CrossOrigin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@Documented
4040
public @interface CrossOrigin {
4141

42-
String[] DEFAULT_ORIGIN = { "*" };
42+
String[] DEFAULT_ORIGINS = { "*" };
4343

4444
String[] DEFAULT_ALLOWED_HEADERS = { "*" };
4545

spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public class CorsConfiguration {
4242
*/
4343
public static final String ALL = "*";
4444

45-
/**
46-
* Default maximum age (30 minutes).
47-
*/
48-
public static final Long DEFAULT_MAX_AGE = Long.valueOf(1800);
49-
5045
private List<String> allowedOrigins;
5146

5247
private List<String> allowedMethods;

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121

2222
import org.springframework.http.HttpMethod;
23+
import org.springframework.web.bind.annotation.CrossOrigin;
2324
import org.springframework.web.cors.CorsConfiguration;
2425

2526
/**
@@ -49,13 +50,12 @@ public CorsRegistration(String pathPattern) {
4950
this.pathPattern = pathPattern;
5051
// Same implicit default values as the @CrossOrigin annotation + allows simple methods
5152
this.config = new CorsConfiguration();
52-
this.config.addAllowedOrigin(CorsConfiguration.ALL);
53-
this.config.addAllowedMethod(HttpMethod.GET);
54-
this.config.addAllowedMethod(HttpMethod.HEAD);
55-
this.config.addAllowedMethod(HttpMethod.POST);
56-
this.config.addAllowedHeader(CorsConfiguration.ALL);
57-
this.config.setAllowCredentials(Boolean.TRUE);
58-
this.config.setMaxAge(CorsConfiguration.DEFAULT_MAX_AGE);
53+
this.config.setAllowedOrigins(Arrays.asList(CrossOrigin.DEFAULT_ORIGINS));
54+
this.config.setAllowedMethods(Arrays.asList(HttpMethod.GET.name(),
55+
HttpMethod.HEAD.name(), HttpMethod.POST.name()));
56+
this.config.setAllowedHeaders(Arrays.asList(CrossOrigin.DEFAULT_ALLOWED_HEADERS));
57+
this.config.setAllowCredentials(CrossOrigin.DEFAULT_ALLOW_CREDENTIALS);
58+
this.config.setMaxAge(CrossOrigin.DEFAULT_MAX_AGE);
5959
}
6060

6161
public CorsRegistration allowedOrigins(String... origins) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ protected CorsConfiguration initCorsConfiguration(Object handler, Method method,
300300
updateCorsConfig(config, methodAnnotation);
301301

302302
if (CollectionUtils.isEmpty(config.getAllowedOrigins())) {
303-
config.setAllowedOrigins(Arrays.asList(CrossOrigin.DEFAULT_ORIGIN));
303+
config.setAllowedOrigins(Arrays.asList(CrossOrigin.DEFAULT_ORIGINS));
304304
}
305305
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
306306
for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {

0 commit comments

Comments
 (0)