Skip to content

Commit 7077346

Browse files
committed
Align default values with 5.0.x
Closes gh-25414
1 parent 6d524e1 commit 7077346

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
/**
2929
* Marks the annotated method or type as permitting cross origin requests.
3030
*
31-
* <p>By default all origins and headers are permitted, credentials are allowed,
31+
* <p>By default all origins and headers are permitted, credentials are not allowed,
3232
* and the maximum age is set to 1800 seconds (30 minutes). The list of HTTP
3333
* methods is set to the methods on the {@code @RequestMapping} if not
3434
* explicitly set on {@code @CrossOrigin}.
@@ -67,7 +67,7 @@
6767
* @deprecated as of Spring 4.3.4, in favor of using {@link CorsConfiguration#applyPermitDefaultValues}
6868
*/
6969
@Deprecated
70-
boolean DEFAULT_ALLOW_CREDENTIALS = true;
70+
boolean DEFAULT_ALLOW_CREDENTIALS = false;
7171

7272
/**
7373
* @deprecated as of Spring 4.3.4, in favor of using {@link CorsConfiguration#applyPermitDefaultValues}
@@ -133,7 +133,8 @@
133133
* An empty string ({@code ""}) means <em>undefined</em>.
134134
* {@code "true"} means that the pre-flight response will include the header
135135
* {@code Access-Control-Allow-Credentials=true}.
136-
* <p>If undefined, credentials are allowed.
136+
* <p>If undefined, this is set to {@code "false"} in which case credentials
137+
* are not allowed.
137138
*/
138139
String allowCredentials() default "";
139140

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -328,9 +328,6 @@ public CorsConfiguration applyPermitDefaultValues() {
328328
if (this.allowedHeaders == null) {
329329
this.addAllowedHeader(ALL);
330330
}
331-
if (this.allowCredentials == null) {
332-
this.setAllowCredentials(true);
333-
}
334331
if (this.maxAge == null) {
335332
this.setMaxAge(1800L);
336333
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,9 +122,10 @@ public CorsRegistration maxAge(long maxAge) {
122122
}
123123

124124
/**
125-
* Whether user credentials are supported.
126-
* <p>By default this is set to {@code true} in which case user credentials
127-
* are supported.
125+
* Whether user credentials are supported in which case the browser should
126+
* include any cookies associated with the domain of the request being
127+
* annotated.
128+
* <p>By default this is {@code false} and user credentials are not allowed.
128129
*/
129130
public CorsRegistration allowCredentials(boolean allowCredentials) {
130131
this.config.setAllowCredentials(allowCredentials);

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -920,13 +920,13 @@ public void testCorsMinimal() throws Exception {
920920
assertArrayEquals(new String[]{"GET", "HEAD", "POST"}, config.getAllowedMethods().toArray());
921921
assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
922922
assertNull(config.getExposedHeaders());
923-
assertTrue(config.getAllowCredentials());
923+
assertNull(config.getAllowCredentials());
924924
assertEquals(new Long(1800), config.getMaxAge());
925925
}
926926
}
927927

928928
@Test
929-
public void testCors() throws Exception {
929+
public void testCors() {
930930
loadBeanDefinitions("mvc-config-cors.xml");
931931

932932
String[] beanNames = appContext.getBeanNamesForType(AbstractHandlerMapping.class);
@@ -943,14 +943,14 @@ public void testCors() throws Exception {
943943
assertArrayEquals(new String[]{"GET", "PUT"}, config.getAllowedMethods().toArray());
944944
assertArrayEquals(new String[]{"header1", "header2", "header3"}, config.getAllowedHeaders().toArray());
945945
assertArrayEquals(new String[]{"header1", "header2"}, config.getExposedHeaders().toArray());
946-
assertFalse(config.getAllowCredentials());
946+
assertTrue(config.getAllowCredentials());
947947
assertEquals(Long.valueOf(123), config.getMaxAge());
948948
config = configs.get("/resources/**");
949949
assertArrayEquals(new String[]{"https://domain1.com"}, config.getAllowedOrigins().toArray());
950950
assertArrayEquals(new String[]{"GET", "HEAD", "POST"}, config.getAllowedMethods().toArray());
951951
assertArrayEquals(new String[]{"*"}, config.getAllowedHeaders().toArray());
952952
assertNull(config.getExposedHeaders());
953-
assertTrue(config.getAllowCredentials());
953+
assertNull(config.getAllowCredentials());
954954
assertEquals(Long.valueOf(1800), config.getMaxAge());
955955
}
956956
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
24-
import java.util.Arrays;
24+
import java.util.Collections;
2525
import java.util.Properties;
2626

2727
import org.junit.Before;
@@ -53,8 +53,13 @@
5353
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
5454
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
5555

56-
import static org.hamcrest.CoreMatchers.*;
57-
import static org.junit.Assert.*;
56+
import static org.hamcrest.CoreMatchers.containsString;
57+
import static org.junit.Assert.assertArrayEquals;
58+
import static org.junit.Assert.assertEquals;
59+
import static org.junit.Assert.assertFalse;
60+
import static org.junit.Assert.assertNotNull;
61+
import static org.junit.Assert.assertNull;
62+
import static org.junit.Assert.assertTrue;
5863

5964
/**
6065
* Test fixture for {@link CrossOrigin @CrossOrigin} annotated methods.
@@ -123,7 +128,7 @@ public void defaultAnnotation() throws Exception {
123128
assertNotNull(config);
124129
assertArrayEquals(new String[] {"GET"}, config.getAllowedMethods().toArray());
125130
assertArrayEquals(new String[] {"*"}, config.getAllowedOrigins().toArray());
126-
assertTrue(config.getAllowCredentials());
131+
assertNull(config.getAllowCredentials());
127132
assertArrayEquals(new String[] {"*"}, config.getAllowedHeaders().toArray());
128133
assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
129134
assertEquals(new Long(1800), config.getMaxAge());
@@ -151,8 +156,8 @@ public void customOriginDefinedViaValueAttribute() throws Exception {
151156
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
152157
CorsConfiguration config = getCorsConfiguration(chain, false);
153158
assertNotNull(config);
154-
assertEquals(Arrays.asList("https://example.com"), config.getAllowedOrigins());
155-
assertTrue(config.getAllowCredentials());
159+
assertEquals(Collections.singletonList("https://example.com"), config.getAllowedOrigins());
160+
assertNull(config.getAllowCredentials());
156161
}
157162

158163
@Test
@@ -162,8 +167,8 @@ public void customOriginDefinedViaPlaceholder() throws Exception {
162167
HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
163168
CorsConfiguration config = getCorsConfiguration(chain, false);
164169
assertNotNull(config);
165-
assertEquals(Arrays.asList("https://example.com"), config.getAllowedOrigins());
166-
assertTrue(config.getAllowCredentials());
170+
assertEquals(Collections.singletonList("https://example.com"), config.getAllowedOrigins());
171+
assertNull(config.getAllowCredentials());
167172
}
168173

169174
@Test
@@ -240,7 +245,7 @@ public void preFlightRequest() throws Exception {
240245
assertNotNull(config);
241246
assertArrayEquals(new String[] {"GET"}, config.getAllowedMethods().toArray());
242247
assertArrayEquals(new String[] {"*"}, config.getAllowedOrigins().toArray());
243-
assertTrue(config.getAllowCredentials());
248+
assertNull(config.getAllowCredentials());
244249
assertArrayEquals(new String[] {"*"}, config.getAllowedHeaders().toArray());
245250
assertTrue(CollectionUtils.isEmpty(config.getExposedHeaders()));
246251
assertEquals(new Long(1800), config.getMaxAge());

spring-webmvc/src/test/resources/org/springframework/web/servlet/config/mvc-config-cors.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<mvc:mapping path="/api/**" allowed-origins="https://domain1.com, https://domain2.com"
1313
allowed-methods="GET, PUT" allowed-headers="header1, header2, header3"
14-
exposed-headers="header1, header2" allow-credentials="false" max-age="123" />
14+
exposed-headers="header1, header2" allow-credentials="true" max-age="123" />
1515

1616
<mvc:mapping path="/resources/**" allowed-origins="https://domain1.com" />
1717

src/asciidoc/web-cors.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ implementation (https://github.com/spring-projects/spring-framework/blob/master/
2424
by default) in order to add the relevant CORS response headers (like `Access-Control-Allow-Origin`)
2525
based on the CORS configuration you have provided.
2626

27+
[NOTE]
28+
====
29+
Be aware that cookies are not allowed by default to avoid increasing the surface attack of
30+
the web application (for example via exposing sensitive user-specific information like
31+
CSRF tokens). Set `allowedCredentials` property to `true` in order to allow them.
32+
====
33+
2734
[NOTE]
2835
====
2936
Since CORS requests are automatically dispatched, you *do not need* to change the
@@ -151,7 +158,8 @@ public class WebConfig extends WebMvcConfigurerAdapter {
151158
.allowedMethods("PUT", "DELETE")
152159
.allowedHeaders("header1", "header2", "header3")
153160
.exposedHeaders("header1", "header2")
154-
.allowCredentials(false).maxAge(3600);
161+
.allowCredentials(true)
162+
.maxAge(3600);
155163
}
156164
}
157165
----
@@ -180,7 +188,7 @@ It is also possible to declare several CORS mappings with customized properties:
180188
allowed-origins="https://domain1.com, https://domain2.com"
181189
allowed-methods="GET, PUT"
182190
allowed-headers="header1, header2, header3"
183-
exposed-headers="header1, header2" allow-credentials="false"
191+
exposed-headers="header1, header2"
184192
max-age="123" />
185193
186194
<mvc:mapping path="/resources/**"

0 commit comments

Comments
 (0)