public class ForwardedHeaderFilter extends OncePerRequestFilter
getServerName()
getServerPort()
getScheme()
isSecure()
sendRedirect(String)
.
This filter can also be used in a removeOnly
mode
where "Forwarded" and "X-Forwarded-*" headers are eliminated, and not used.
Modifier and Type | Class and Description |
---|---|
private static class |
ForwardedHeaderFilter.ForwardedHeaderExtractingRequest
Extract and use "Forwarded" or "X-Forwarded-*" headers.
|
private static class |
ForwardedHeaderFilter.ForwardedHeaderExtractingResponse |
private static class |
ForwardedHeaderFilter.ForwardedHeaderRemovingRequest
Hide "Forwarded" or "X-Forwarded-*" headers.
|
private static class |
ForwardedHeaderFilter.ForwardedPrefixExtractor
Responsible for the contextPath, requestURI, and requestURL with forwarded
headers in mind, and also taking into account changes to the path of the
underlying delegate request (e.g.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.Set<java.lang.String> |
FORWARDED_HEADER_NAMES |
private UrlPathHelper |
pathHelper |
private boolean |
relativeRedirects |
private boolean |
removeOnly |
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
ForwardedHeaderFilter() |
Modifier and Type | Method and Description |
---|---|
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request within a single request thread. |
void |
setRelativeRedirects(boolean relativeRedirects)
Use this property to enable relative redirects as explained in
RelativeRedirectFilter , and also using the same response wrapper
as that filter does, or if both are configured, only one will wrap. |
void |
setRemoveOnly(boolean removeOnly)
Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are
removed only and the information in them ignored.
|
protected boolean |
shouldNotFilter(HttpServletRequest request)
Can be overridden in subclasses for custom filtering control,
returning
true to avoid filtering of the given request. |
protected boolean |
shouldNotFilterAsyncDispatch()
The dispatcher type
javax.servlet.DispatcherType.ASYNC introduced
in Servlet 3.0 means a filter can be invoked in more than one thread
over the course of a single request. |
protected boolean |
shouldNotFilterErrorDispatch()
Whether to filter error dispatches such as when the servlet container
processes and error mapped in
web.xml . |
doFilter, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
private static final java.util.Set<java.lang.String> FORWARDED_HEADER_NAMES
private final UrlPathHelper pathHelper
private boolean removeOnly
private boolean relativeRedirects
public void setRemoveOnly(boolean removeOnly)
removeOnly
- whether to discard and ignore forwarded headerspublic void setRelativeRedirects(boolean relativeRedirects)
RelativeRedirectFilter
, and also using the same response wrapper
as that filter does, or if both are configured, only one will wrap.
By default, if this property is set to false, in which case calls to
HttpServletResponse#sendRedirect(String)
are overridden in order
to turn relative into absolute URLs, also taking into account forwarded
headers.
relativeRedirects
- whether to use relative redirectsprotected boolean shouldNotFilter(HttpServletRequest request)
OncePerRequestFilter
true
to avoid filtering of the given request.
The default implementation always returns false
.
shouldNotFilter
in class OncePerRequestFilter
request
- current HTTP requestprotected boolean shouldNotFilterAsyncDispatch()
OncePerRequestFilter
javax.servlet.DispatcherType.ASYNC
introduced
in Servlet 3.0 means a filter can be invoked in more than one thread
over the course of a single request. Some filters only need to filter
the initial thread (e.g. request wrapping) while others may need
to be invoked at least once in each additional thread for example for
setting up thread locals or to perform final processing at the very end.
Note that although a filter can be mapped to handle specific dispatcher
types via web.xml
or in Java through the ServletContext
,
servlet containers may enforce different defaults with regards to
dispatcher types. This flag enforces the design intent of the filter.
The default return value is "true", which means the filter will not be invoked during subsequent async dispatches. If "false", the filter will be invoked during async dispatches with the same guarantees of being invoked only once during a request within a single thread.
shouldNotFilterAsyncDispatch
in class OncePerRequestFilter
protected boolean shouldNotFilterErrorDispatch()
OncePerRequestFilter
web.xml
. The default return value
is "true", which means the filter will not be invoked in case of an error
dispatch.shouldNotFilterErrorDispatch
in class OncePerRequestFilter
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request within a single request thread.
See OncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterInternal
in class OncePerRequestFilter
ServletException
java.io.IOException