Class UrlHandlerFilter

java.lang.Object
org.springframework.web.filter.reactive.UrlHandlerFilter
All Implemented Interfaces:
WebFilter

public final class UrlHandlerFilter extends Object implements WebFilter
WebFilter 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/**").mutateRequest()
    .build();
 

This WebFilter should be ordered ahead of security filters.

Since:
6.2
Author:
Rossen Stoyanchev
  • Method Details

    • filter

      public reactor.core.publisher.Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain)
      Description copied from interface: WebFilter
      Process the Web request and (optionally) delegate to the next WebFilter through the given WebFilterChain.
      Specified by:
      filter in interface WebFilter
      Parameters:
      exchange - the current server exchange
      chain - provides a way to delegate to the next filter
      Returns:
      Mono<Void> to indicate when request processing is complete
    • trailingSlashHandler

      public static UrlHandlerFilter.Builder.TrailingSlashSpec trailingSlashHandler(String... pathPatterns)
      Add a handler that removes the trailing slash from URL paths to ensure consistent interpretation of paths with or without a trailing slash for requestion mapping purposes. This is important especially to avoid misalignment between URL-based authorization decisions and web framework request mappings.

      The root path "/" is excluded from trailing slash handling.

      Note: A method-level @RequestMapping("/") adds a trailing slash to a type-level prefix mapping, and therefore would never match to a URL with the trailing slash removed. Use @RequestMapping without a path instead to avoid the trailing slash in the mapping.

      Parameters:
      pathPatterns - patterns to map the handler to, e.g. "/path/*", "/path/**", "/path/foo/"
      Returns:
      a spec to configure the trailing slash handler with
      See Also: