OncePerRequestFilter is an abstract base class in Spring Web (org.springframework.web.filter) that guarantees your filter logic runs at most once per request, even when the servlet container dispatches the same request more than once (forwards, includes, error dispatches, async dispatches). You extend it and implement doFilterInternal() instead of the raw doFilter() method of jakarta.servlet.Filter.
It is the standard base class for custom authentication, logging and request-correlation filters in Spring MVC and Spring Security applications, including Spring Boot 3 and later, which use the jakarta.servlet namespace.
A servlet filter is mapped to dispatcher types such as REQUEST, FORWARD, INCLUDE, ERROR and ASYNC. A single client request can therefore pass through the same filter chain several times, for example when a controller forwards to another path, when the container renders an error page after an exception, or when an asynchronous handler completes and the request is dispatched again.
For work that must happen exactly once, such as validating a JWT, writing an audit entry or starting a timer, running it again is at best wasteful and at worst wrong (double logging, double token processing, duplicated metrics).
OncePerRequestFilter solves this by setting a request attribute named after the filter (the filter name plus the suffix .FILTERED) the first time the filter runs, and skipping the logic when that attribute is already present.
The public doFilter() method is declared final. It checks whether the request was already filtered, whether the filter should be skipped, and then delegates to your code:
HttpServletRequest or HttpServletResponse, it throws a ServletException..FILTERED attribute is already set, or shouldNotFilter() returns true, or the dispatch type is async or error and the corresponding shouldNotFilter... method returns true, it simply calls chain.doFilter().doFilterInternal(), and removes the attribute in a finally block.Because doFilter() is final, you customise behaviour only through the protected hooks below.
| Method | Purpose | Default |
|---|---|---|
doFilterInternal(request, response, filterChain) | Your filter logic (abstract) | none |
shouldNotFilter(request) | Skip the filter for some requests, for example by path | false |
shouldNotFilterAsyncDispatch() | Skip the filter on async re-dispatches | true |
shouldNotFilterErrorDispatch() | Skip the filter on error dispatches | true |
getAlreadyFilteredAttributeName() | Name of the marker attribute | filter name plus .FILTERED |
Since Spring Framework 6, async and error dispatches are skipped by default. If your filter must also wrap those dispatches, override the relevant method to return false.
The following filter adds a correlation ID to every request, exposes it in the response and in the logging MDC, and always cleans up:
Spring Boot detects any Filter bean and registers it with the embedded servlet container for all URLs, so annotating the class with @Component is enough for a plain Spring MVC application. The finally block matters: the filter chain can throw, and a leaked MDC entry would pollute the next request handled by the same thread.
The most common use is a JWT or API-key authentication filter placed in the Spring Security chain:
You then add it to the security filter chain:
JwtService here stands for your own token helper; it is not part of Spring. Disabling CSRF is appropriate only for stateless APIs that do not rely on cookies for authentication.
A Filter bean is registered twice if it is both a Spring Boot servlet filter and part of the Security chain: once by Boot in the container, and once inside FilterChainProxy. The OncePerRequestFilter marker prevents the logic from running twice, but the cleaner fix is to disable Boot's own registration:
Alternatively, do not declare the filter as a bean at all and instantiate it inside the security configuration.
Filters registered by Boot run in the order of @Order or FilterRegistrationBean.setOrder(); lower values run first. Filters inside the Spring Security chain run in the position you give them with addFilterBefore, addFilterAfter or addFilterAt.
To skip a filter for specific requests, override shouldNotFilter(). Typical examples are health endpoints, static resources and public authentication routes. Overriding it is cheaper and clearer than testing the path inside doFilterInternal().
Filter is the raw servlet interface, and its doFilter() can run several times for one client request because of forwards, error dispatches and async dispatches. OncePerRequestFilter wraps that interface and guarantees a single execution per request through a marker attribute.
Yes, whenever your filter has side effects that must not repeat. Use the jakarta.servlet imports, since javax.servlet was replaced in Spring Framework 6 and Spring Boot 3.
Since Spring Framework 6, shouldNotFilterAsyncDispatch() and shouldNotFilterErrorDispatch() return true by default. Override them to return false if you need the filter on those dispatches.
No. It is built on the servlet API. In WebFlux, implement WebFilter, which is already invoked once per exchange.
OncePerRequestFilter gives you a servlet filter that runs once per request, with hooks to skip paths and dispatch types. Extend it, implement doFilterInternal(), always continue the chain with filterChain.doFilter() unless you intentionally reject the request, and clean up in finally. In Spring Security, register it with addFilterBefore() and prevent duplicate registration with a disabled FilterRegistrationBean.