Class ForwardedHeaderUtils
java.lang.Object
org.springframework.web.util.ForwardedHeaderUtils
Utility class to assist with processing "Forwarded" and "X-Forwarded-*" headers.
Note: There are security considerations surrounding the use of forwarded headers. Those should not be used unless the application is behind a trusted proxy that inserts them and also explicitly removes any such headers coming from an external source.
In most cases, you should not use this class directly but rather rely on
ForwardedHeaderFilter for Spring MVC or
ForwardedHeaderTransformer in
order to extract the information from the headers as early as possible and discard
such headers. Underlying servers such as Tomcat, Jetty, and Reactor Netty also
provide options to handle forwarded headers even earlier.
- Since:
- 6.1
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordContainer for the combined results of parsing either the "Forwarded" header or the "X-Forwarded-*" alternative headers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic UriComponentsBuilderadaptFromForwardedHeaders(URI uri, HttpHeaders headers) Deprecated, for removal: This API element is subject to removal in a future version.static @Nullable InetSocketAddressparseForwardedBy(URI uri, HttpHeaders headers, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.static @Nullable InetSocketAddressparseForwardedFor(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress) Deprecated, for removal: This API element is subject to removal in a future version.parseStandardHeader(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Parse the "Forwarded" header.parseXForwardedHeaders(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Parse the "X-Forwarded-Proto", "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-For" headers, the alternative to the "Forwarded" header.
-
Constructor Details
-
ForwardedHeaderUtils
public ForwardedHeaderUtils()
-
-
Method Details
-
parseStandardHeader
public static ForwardedHeaderUtils.ForwardedInfo parseStandardHeader(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Parse the "Forwarded" header.- Parameters:
uri- the requestURIheaders- the HTTP headers to get the "Forwarded" header fromremoteAddress- for a default port for the parsed "for" valuelocalAddress- for a default port for the parsed "by" value- Returns:
- a
ForwardedHeaderUtils.ForwardedInfowith the parse results - Since:
- 7.0.9
- See Also:
-
parseXForwardedHeaders
public static ForwardedHeaderUtils.ForwardedInfo parseXForwardedHeaders(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Parse the "X-Forwarded-Proto", "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-For" headers, the alternative to the "Forwarded" header.There is no parsing of "X-Forwarded-By" currently, so the
byAddressinForwardedHeaderUtils.ForwardedInfois alwaysnull.- Parameters:
uri- the requestURIheaders- the HTTP headers to get the "Forwarded" header fromremoteAddress- for a default port for the parsed "for" valuelocalAddress- for a default port for the parsed "by" value; this argument is ignored currently and the byAddress is alwaysnull- Returns:
- a
ForwardedHeaderUtils.ForwardedInfowith the scheme, host, and port adapted from the "X-Forwarded-*" headers, and the parsed "for" address - Since:
- 7.0.9
-
adaptFromForwardedHeaders
@Deprecated(since="7.1", forRemoval=true) public static UriComponentsBuilder adaptFromForwardedHeaders(URI uri, HttpHeaders headers) Deprecated, for removal: This API element is subject to removal in a future version.Adapt the scheme+host+port of the givenURIfrom the "Forwarded" header (see RFC 7239) or from the "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers if "Forwarded" is not present.- Parameters:
uri- the requestURIheaders- the HTTP headers to consider- Returns:
- a
UriComponentsBuilderthat reflects the request URI and additional updates from forwarded headers
-
parseForwardedFor
@Deprecated(since="7.1", forRemoval=true) public static @Nullable InetSocketAddress parseForwardedFor(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress) Deprecated, for removal: This API element is subject to removal in a future version.Parse the first "Forwarded: for=..." or "X-Forwarded-For" header value to anInetSocketAddressrepresenting the address of the client.- Parameters:
uri- the requestURIheaders- the request headers that may contain forwarded headersremoteAddress- the current remote address- Returns:
- an
InetSocketAddresswith the extracted host and port, ornullif the headers are not present
-
parseForwardedBy
@Deprecated(since="7.1", forRemoval=true) public static @Nullable InetSocketAddress parseForwardedBy(URI uri, HttpHeaders headers, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.Parse the first "Forwarded: by=..." header value to anInetSocketAddressrepresenting the address of the server.- Parameters:
uri- the requestURIheaders- the request headers that may contain forwarded headerslocalAddress- the current local address- Returns:
- an
InetSocketAddresswith the extracted host and port, ornullif the headers are not present - Since:
- 7.0
-
parseStandardHeader(URI, HttpHeaders, InetSocketAddress, InetSocketAddress)andparseXForwardedHeaders(URI, HttpHeaders, InetSocketAddress, InetSocketAddress)