Package org.springframework.web.filter

# Class UrlHandlerFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.UrlHandlerFilter

All Implemented Interfaces:
  `Filter`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.EnvironmentAware`, `org.springframework.core.env.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

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`UrlHandlerFilter.Builder`
Builder for `UrlHandlerFilter`.

## Field Summary

### Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

`ALREADY_FILTERED_SUFFIX`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread.
`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.

### Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

`doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch`

### Methods inherited from class org.springframework.web.filter.GenericFilterBean

`addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### 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)
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:
  - `UrlHandlerFilter.Builder.trailingSlashHandler(String...)`