Class UrlHandlerFilter

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

public final class UrlHandlerFilter extends OncePerRequestFilter
Filter that modifies the URL, and then either redirects or wraps the request to effect 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, before ServletRequestPathFilter, and before security filters.

Since:
6.2
Author:
Rossen Stoyanchev
  • Method Details

    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain chain) throws jakarta.servlet.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:
      jakarta.servlet.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/2;", "/path/2;2;", "/path/foo/".
      Returns:
      a spec to configure the trailing slash handler with
      See Also: