Top Description Fields Constructors Methods
org.springframework.web.server.adapter

public Class ForwardedHeaderTransformer

extends Object
implements Function<ServerHttpRequest, ServerHttpRequest>
Class Inheritance
All Implemented Interfaces
java.util.function.Function
Imports
java.net.InetSocketAddress, .URI, .URISyntaxException, java.util.Collections, .Locale, .Set, java.util.function.Function, org.springframework.context.ApplicationContext, org.springframework.http.HttpHeaders, org.springframework.http.server.reactive.ServerHttpRequest, org.springframework.lang.Nullable, org.springframework.util.LinkedCaseInsensitiveMap, .StringUtils, org.springframework.web.util.ForwardedHeaderUtils, .UriComponents

Extract values from "Forwarded" and "X-Forwarded-*" headers to override the request URI (i.e. ServerHttpRequest#getURI()) so it reflects the client-originated protocol and address.

An instance of this class is typically declared as a bean with the name "forwardedHeaderTransformer" and detected by WebHttpHandlerBuilder#applicationContext(ApplicationContext), or it can also be registered directly via WebHttpHandlerBuilder#forwardedHeaderTransformer(ForwardedHeaderTransformer).

There are security considerations for forwarded headers since an application cannot know if the headers were added by a proxy, as intended, or by a malicious client. This is why a proxy at the boundary of trust should be configured to remove untrusted Forwarded headers that come from the outside.

You can also configure the ForwardedHeaderFilter with removeOnly, in which case it removes but does not use the headers.

Author
Rossen Stoyanchev
Since
5.1
See Also
https://tools.ietf.org/html/rfc7239, Forwarded Headers

Field Summary

Modifier and TypeField and Description
pack-priv static final Set<String>
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static URI
public ServerHttpRequest
apply(ServerHttpRequest
the request
request
)

Implements java.util.function.Function.apply.

Apply and remove, or remove Forwarded type headers.
private static String
protected boolean
hasForwardedHeaders(ServerHttpRequest
the request
request
)

Whether the request has any Forwarded headers.

public boolean
isRemoveOnly()

Whether the "remove only" mode is on.

private void
public void
setRemoveOnly(boolean
whether to discard and ignore forwarded headers
removeOnly
)

Enable mode in which any "Forwarded" or "X-Forwarded-*" headers are removed only and the information in them ignored.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FORWARDED_HEADER_NAMESback to summary
pack-priv static final Set<String> FORWARDED_HEADER_NAMES
removeOnlyback to summary
private boolean removeOnly

Constructor Detail

ForwardedHeaderTransformerback to summary
public ForwardedHeaderTransformer()

Method Detail

adaptFromForwardedHeadersback to summary
private static URI adaptFromForwardedHeaders(URI uri, HttpHeaders headers)
applyback to summary
public ServerHttpRequest apply(ServerHttpRequest request)

Implements java.util.function.Function.apply.

Apply and remove, or remove Forwarded type headers.

Parameters
request:ServerHttpRequest

the request

Returns:ServerHttpRequest

Doc from java.util.function.Function.apply.

the function result

Annotations
@Override
getForwardedPrefixback to summary
private static String getForwardedPrefix(ServerHttpRequest request)
Annotations
@Nullable
hasForwardedHeadersback to summary
protected boolean hasForwardedHeaders(ServerHttpRequest request)

Whether the request has any Forwarded headers.

Parameters
request:ServerHttpRequest

the request

isRemoveOnlyback to summary
public boolean isRemoveOnly()

Whether the "remove only" mode is on.

See Also
setRemoveOnly
removeForwardedHeadersback to summary
private void removeForwardedHeaders(ServerHttpRequest.Builder builder)
setRemoveOnlyback to summary
public void setRemoveOnly(boolean removeOnly)

Enable mode in which any "Forwarded" or "X-Forwarded-*" headers are removed only and the information in them ignored.

Parameters
removeOnly:boolean

whether to discard and ignore forwarded headers