Class ForwardedHeaderUtils
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.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)andparseXForwardedHeaders(URI, HttpHeaders)static @Nullable InetSocketAddressparseForwardedFor(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress) Deprecated, for removal: This API element is subject to removal in a future version.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)andparseXForwardedHeaders(URI, HttpHeaders)parseStandardHeader(URI uri, HttpHeaders headers) Parse the "Forwarded" header.parseStandardHeader(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)which does not use a fallback port from the remote or local addressparseXForwardedHeaders(URI uri, HttpHeaders headers) Parse the "X-Forwarded-Proto", "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-For" headers, the alternative to the "Forwarded" header.parseXForwardedHeaders(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.as of 7.1 in favor ofparseXForwardedHeaders(URI, HttpHeaders)which does not use a fallback port from the remote address
-
Constructor Details
-
ForwardedHeaderUtils
public ForwardedHeaderUtils()
-
-
Method Details
-
parseStandardHeader
Parse the "Forwarded" header.If the parsed "for" or "by" value does not include a port, the resulting
InetSocketAddresshas port 0 to indicate it is unknown.- Parameters:
uri- the requestURIheaders- the HTTP headers to get the "Forwarded" header from- Returns:
- a
ForwardedHeaderUtils.ForwardedInfowith the parse results - Since:
- 7.1
- See Also:
-
parseStandardHeader
@Deprecated(since="7.1", forRemoval=true) public static ForwardedHeaderUtils.ForwardedInfo parseStandardHeader(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)which does not use a fallback port from the remote or local addressParse 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:
- 6.1.29
- See Also:
-
parseXForwardedHeaders
public static ForwardedHeaderUtils.ForwardedInfo parseXForwardedHeaders(URI uri, HttpHeaders headers) Parse the "X-Forwarded-Proto", "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-For" headers, the alternative to the "Forwarded" header.The "X-Forwarded-For" value does not include a port, and therefore the resulting
InetSocketAddresshas port 0 to indicate that the port is not known.There is no parsing of "X-Forwarded-By" currently, so the
byAddressinForwardedHeaderUtils.ForwardedInfois alwaysnull.- Parameters:
uri- the requestURIheaders- the HTTP headers to get the "X-Forwarded-*" headers from- Returns:
- a
ForwardedHeaderUtils.ForwardedInfowith the scheme, host, and port adapted from the "X-Forwarded-*" headers, and the parsed "for" address - Since:
- 7.1
-
parseXForwardedHeaders
@Deprecated(since="7.1", forRemoval=true) public static ForwardedHeaderUtils.ForwardedInfo parseXForwardedHeaders(URI uri, HttpHeaders headers, @Nullable InetSocketAddress remoteAddress, @Nullable InetSocketAddress localAddress) Deprecated, for removal: This API element is subject to removal in a future version.as of 7.1 in favor ofparseXForwardedHeaders(URI, HttpHeaders)which does not use a fallback port from the remote addressParse 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:
- 6.1.29
-
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.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)andparseXForwardedHeaders(URI, HttpHeaders)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.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)andparseXForwardedHeaders(URI, HttpHeaders)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.as of 7.1 in favor ofparseStandardHeader(URI, HttpHeaders)andparseXForwardedHeaders(URI, HttpHeaders)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)andparseXForwardedHeaders(URI, HttpHeaders)