Class UrlHandlerFilter
- All Implemented Interfaces:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
Filter
that modifies the URL, and then redirects or
wraps the request to apply the change.
To create an instance, you can use the following:
UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler("/path1/**").redirect(HttpStatus.PERMANENT_REDIRECT) .trailingSlashHandler("/path2/**").wrapRequest() .build();
This Filter
should be ordered after ForwardedHeaderFilter
and before any security filters.
- Since:
- 6.2
- Author:
- Rossen Stoyanchev
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
doFilterInternal
(HttpServletRequest request, HttpServletResponse response, FilterChain chain) Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread.protected boolean
The dispatcher typejakarta.servlet.DispatcherType.ASYNC
means a filter can be invoked in more than one thread over the course of a single request.protected boolean
Whether to filter error dispatches such as when the servlet container processes and error mapped inweb.xml
.trailingSlashHandler
(String... pathPatterns) Create a builder by adding a handler for URL's with a trailing slash.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Method Details
-
shouldNotFilterAsyncDispatch
protected boolean shouldNotFilterAsyncDispatch()Description copied from class:OncePerRequestFilter
The dispatcher typejakarta.servlet.DispatcherType.ASYNC
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 (for example, 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 theServletContext
, servlet containers may enforce different defaults with respect 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.
- Overrides:
shouldNotFilterAsyncDispatch
in classOncePerRequestFilter
-
shouldNotFilterErrorDispatch
protected boolean shouldNotFilterErrorDispatch()Description copied from class:OncePerRequestFilter
Whether to filter error dispatches such as when the servlet container processes and error mapped inweb.xml
. The default return value is "true", which means the filter will not be invoked in case of an error dispatch.- Overrides:
shouldNotFilterErrorDispatch
in classOncePerRequestFilter
-
doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException Description copied from class:OncePerRequestFilter
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread. SeeOncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
- Specified by:
doFilterInternal
in classOncePerRequestFilter
- Throws:
ServletException
IOException
-
trailingSlashHandler
public static UrlHandlerFilter.Builder.TrailingSlashSpec trailingSlashHandler(String... pathPatterns) Create a builder by adding a handler for URL's with a trailing slash.- Parameters:
pathPatterns
- path patterns to map the handler to, for example,"/path/*"
,"/path/**"
,"/path/foo/"
.- Returns:
- a spec to configure the trailing slash handler with
- See Also:
-