Skip to content

Commit de4da5e

Browse files
committed
Improve docs on forwarded headers
Issue: SPR-16660
1 parent e8c656d commit de4da5e

File tree

8 files changed

+64
-21
lines changed

8 files changed

+64
-21
lines changed

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

Lines changed: 8 additions & 1 deletion
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-2018 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.
@@ -88,6 +88,13 @@
8888
* header of both the pre-flight response and the actual response.
8989
* {@code "*"} means that all origins are allowed.
9090
* <p>If undefined, all origins are allowed.
91+
* <p><strong>Note:</strong> CORS checks use values from "Forwarded"
92+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
93+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
94+
* if present, in order to reflect the client-originated address.
95+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
96+
* central place whether to extract and use, or to discard such headers.
97+
* See the Spring Framework reference for more on this filter.
9198
* @see #value
9299
*/
93100
@AliasFor("value")

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -106,6 +106,13 @@ public CorsConfiguration(CorsConfiguration other) {
106106
* Set the origins to allow, e.g. {@code "http://domain1.com"}.
107107
* <p>The special value {@code "*"} allows all domains.
108108
* <p>By default this is not set.
109+
* <p><strong>Note:</strong> CORS checks use values from "Forwarded"
110+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
111+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
112+
* if present, in order to reflect the client-originated address.
113+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
114+
* central place whether to extract and use, or to discard such headers.
115+
* See the Spring Framework reference for more on this filter.
109116
*/
110117
public void setAllowedOrigins(List<String> allowedOrigins) {
111118
this.allowedOrigins = (allowedOrigins != null ? new ArrayList<String>(allowedOrigins) : null);

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
278278
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
279279
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
280280
* "Forwarded" is not found.
281+
* <p><strong>Note:</strong> this method uses values from forwarded headers,
282+
* if present, in order to reflect the client-originated protocol and address.
283+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
284+
* central place whether to extract and use, or to discard such headers.
285+
* See the Spring Framework reference for more on this filter.
281286
* @param request the source request
282287
* @return the URI components of the URI
283288
* @since 4.1.5

spring-web/src/main/java/org/springframework/web/util/WebUtils.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -790,6 +790,13 @@ public static MultiValueMap<String, String> parseMatrixVariables(String matrixVa
790790
* Check the given request origin against a list of allowed origins.
791791
* A list containing "*" means that all origins are allowed.
792792
* An empty list means only same origin is allowed.
793+
* <p><strong>Note:</strong> this method may use values from "Forwarded"
794+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
795+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
796+
* if present, in order to reflect the client-originated address.
797+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
798+
* central place whether to extract and use, or to discard such headers.
799+
* See the Spring Framework reference for more on this filter.
793800
* @return {@code true} if the request origin is valid, {@code false} otherwise
794801
* @since 4.1.5
795802
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
@@ -814,6 +821,13 @@ else if (CollectionUtils.isEmpty(allowedOrigins)) {
814821
* Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
815822
* {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and
816823
* @code X-Forwarded-Port} headers.
824+
* <p><strong>Note:</strong> this method uses values from "Forwarded"
825+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
826+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
827+
* if present, in order to reflect the client-originated address.
828+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
829+
* central place whether to extract and use, or to discard such headers.
830+
* See the Spring Framework reference for more on this filter.
817831
* @return {@code true} if the request is a same-origin one, {@code false} in case
818832
* of cross-origin request
819833
* @since 4.2

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

Lines changed: 8 additions & 2 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-2018 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.servlet.config.annotation;
1818

19-
import java.util.ArrayList;
2019
import java.util.Arrays;
2120

2221
import org.springframework.web.cors.CorsConfiguration;
@@ -60,6 +59,13 @@ public CorsRegistration(String pathPattern) {
6059
* Set the origins to allow, e.g. {@code "http://domain1.com"}.
6160
* <p>The special value {@code "*"} allows all domains.
6261
* <p>By default, all origins are allowed.
62+
* <p><strong>Note:</strong> CORS checks use values from "Forwarded"
63+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
64+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
65+
* if present, in order to reflect the client-originated address.
66+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
67+
* central place whether to extract and use, or to discard such headers.
68+
* See the Spring Framework reference for more on this filter.
6369
*/
6470
public CorsRegistration allowedOrigins(String... origins) {
6571
this.config.setAllowedOrigins(Arrays.asList(origins));

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@
8282
* {@link #relativeTo(org.springframework.web.util.UriComponentsBuilder)}.
8383
* </ul>
8484
*
85-
* <p><strong>Note:</strong> This class extracts and uses values from the headers
86-
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
87-
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
88-
* "Forwarded" is not found, in order to reflect the client-originated protocol
89-
* and address. As an alternative consider using the
90-
* {@link org.springframework.web.filter.ForwardedHeaderFilter} to have such
91-
* headers extracted once and removed, or removed only (without being used).
92-
* See the reference for further information including security considerations.
85+
* <p><strong>Note:</strong> This class uses values from "Forwarded"
86+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
87+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
88+
* if present, in order to reflect the client-originated protocol and address.
89+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
90+
* central place whether to extract and use, or to discard such headers.
91+
* See the Spring Framework reference for more on this filter.
9392
*
9493
* @author Oliver Gierke
9594
* @author Rossen Stoyanchev

spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
* UriComponentsBuilder with additional static factory methods to create links
3636
* based on the current HttpServletRequest.
3737
*
38-
* <p><strong>Note:</strong> This class extracts and uses values from the headers
39-
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
40-
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
41-
* "Forwarded" is not found, in order to reflect the client-originated protocol
42-
* and address. As an alternative consider using the
43-
* {@link org.springframework.web.filter.ForwardedHeaderFilter} to have such
44-
* headers extracted once and removed, or removed only (without being used).
45-
* See the reference for further information including security considerations.
38+
* <p><strong>Note:</strong> This class uses values from "Forwarded"
39+
* (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
40+
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
41+
* if present, in order to reflect the client-originated protocol and address.
42+
* Consider using the {@code ForwardedHeaderFilter} in order to choose from a
43+
* central place whether to extract and use, or to discard such headers.
44+
* See the Spring Framework reference for more on this filter.
4645
*
4746
* @author Rossen Stoyanchev
4847
* @since 3.1

spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.3.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,12 @@
13231323
<xsd:documentation><![CDATA[
13241324
Comma-separated list of origins to allow, e.g. "http://domain1.com, http://domain2.com".
13251325
The special value "*" allows all domains (default).
1326+
1327+
Note that CORS checks use values from "Forwarded" (RFC 7239), "X-Forwarded-Host",
1328+
"X-Forwarded-Port", and "X-Forwarded-Proto" headers, if present, in order to reflect
1329+
the client-originated address. Consider using the ForwardedHeaderFilter in order to
1330+
choose from a central place whether to extract and use such headers, or whether to
1331+
discard them. See the Spring Framework reference for more on this filter.
13261332
]]></xsd:documentation>
13271333
</xsd:annotation>
13281334
</xsd:attribute>

0 commit comments

Comments
 (0)