How can I add a filter class in Spring Boot?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Spring Boot, a filter is the right place for cross-cutting HTTP concerns such as logging, header checks, correlation IDs, or simple authentication rules. The usual pattern is to implement a servlet filter, then register it either as a bean or with a FilterRegistrationBean when you need URL patterns or explicit ordering.
The Simplest Option: Extend OncePerRequestFilter
For most applications, extending OncePerRequestFilter is easier than implementing the raw Filter interface because it already handles some servlet edge cases cleanly.
Because the class is annotated with @Component, Spring Boot auto-detects it and adds it to the servlet filter chain.
Registering with FilterRegistrationBean
If you need more control, register the filter explicitly.
This is useful when:
- the filter should apply only to certain URL patterns
- the filter order matters
- you do not want to rely on component scanning alone
A Header Validation Example
Filters are often used to reject requests before they reach controllers.
Notice that the filter either ends the response early or calls filterChain.doFilter(...) to continue.
When to Use a Filter Versus Other Spring Features
A filter works at the servlet layer, before Spring MVC controller methods are invoked. That makes it a good fit for generic HTTP-level concerns.
Use a filter when you need:
- request logging
- header checks
- correlation IDs
- low-level request wrapping
Use controller advice, interceptors, or Spring Security when the concern is more framework-specific or authentication-heavy. Not every cross-cutting concern belongs in a filter.
Ordering Matters
If multiple filters exist, order determines who sees the request first. A logging filter may need to run before an authentication filter, or a correlation-ID filter may need to run very early so later logs can reuse that ID.
That is one reason FilterRegistrationBean is helpful in non-trivial applications.
Common Pitfalls
One common mistake is forgetting to call filterChain.doFilter(...) when the request should continue. If you leave that out, the request stops in the filter and never reaches the controller.
Another issue is doing too much application logic in the filter layer. Filters should stay focused on cross-cutting request and response concerns.
A third pitfall is using a filter when Spring Security already provides a better, more maintainable solution for the problem. That choice matters a lot in production systems.
Summary
- In Spring Boot, filters are used for cross-cutting HTTP request and response handling.
- '
OncePerRequestFilteris usually the easiest base class to extend.' - Use
@Componentfor simple registration orFilterRegistrationBeanfor more control. - Always either continue with
filterChain.doFilter(...)or end the response explicitly. - Keep filter logic focused on generic request-processing concerns.

