Class UrlHandlerFilter

All Implemented Interfaces:
Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, EnvironmentCapable, ServletContextAware

public final class UrlHandlerFilter extends OncePerRequestFilter
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
  • Method Details

    • shouldNotFilterAsyncDispatch

      protected boolean shouldNotFilterAsyncDispatch()
      Description copied from class: OncePerRequestFilter
      The dispatcher type jakarta.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 (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 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 class OncePerRequestFilter
    • shouldNotFilterErrorDispatch

      protected boolean shouldNotFilterErrorDispatch()
      Description copied from class: OncePerRequestFilter
      Whether to filter error dispatches such as when the servlet container processes and error mapped in web.xml. The default return value is "true", which means the filter will not be invoked in case of an error dispatch.
      Overrides:
      shouldNotFilterErrorDispatch in class OncePerRequestFilter
    • doFilterInternal

      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException
      Description copied from class: OncePerRequestFilter
      Same contract as for 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.

      Specified by:
      doFilterInternal in class OncePerRequestFilter
      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, e.g. "/path/*", "/path/**", "/path/foo/".
      Returns:
      a spec to configure the trailing slash handler with
      See Also: