In a modern Spring Boot backend, it’s common to intercept HTTP requests for cross-cutting concerns like authentication, authorization, logging, or header injection.
Spring provides two powerful mechanisms to achieve this:
-
Servlet Filters
-
Spring MVC Interceptors
They may seem similar, but they’re designed for different stages of the request lifecycle. In this blog, we’ll explore the differences, real-world examples, and when to use which — backed by best practices.
Quick Comparison: Filter vs Interceptor
Feature | Filter | Interceptor |
---|---|---|
API Level | Servlet (jakarta.servlet.Filter ) | Spring MVC (HandlerInterceptor ) |
Execution Point | Before DispatcherServlet | Before/after controller methods |
Intercepts static resources? | ✅ Yes | ❌ No |
Access to controller method? | ❌ No | ✅ Yes (via handler ) |
Can block requests? | ✅ Yes (by skipping chain.doFilter() ) | ✅ Yes (by returning false in preHandle ) |
Use Cases | CORS, headers, logging, early auth checks | Role-based auth, audit logging, business pre-checks |
Request Lifecycle in Spring Boot
When Should You Use a Filter?
Filters are ideal for low-level request manipulation and global request concerns, such as:
-
Logging all traffic (including static resources)
-
Enabling CORS headers
-
Authentication token parsing
-
Charset/encoding setup
Example: Global CORS Filter
When Should You Use an Interceptor?
Interceptors are perfect for Spring MVC–specific use cases, such as:
-
Role-based access control (RBAC)
-
Request preprocessing based on controller annotations
-
Audit logging
-
Adding attributes to the model
✅ Example: Interceptor for Admin Authorization
🛠Register in WebMvcConfigurer
Real-World Use Case: Combining Filter and Interceptor
Let’s say you’re building a secure API:
-
You validate JWT tokens in every request → ✅ Filter
-
You check user roles after token validation → ✅ Interceptor
Step 1: JWT Filter
Step 2: Role-Based Interceptor
Summary: Filter vs Interceptor
Use Case | Filter | Interceptor |
---|---|---|
Logging static and dynamic requests | ✅ Yes | ❌ No |
Authentication (e.g., token parsing) | ✅ Yes | ❌ Not ideal |
Authorization (based on role/annotation) | ❌ No | ✅ Yes |
Modify request before controller | ✅ Yes | ✅ Yes |
Access handler method/controller details | ❌ No | ✅ Yes |
Execution order | First | After filter |
Final Thoughts
In most Spring Boot applications, you’ll find yourself using both Filters and Interceptors — not as alternatives but as complements:
-
Use Filters for infrastructure-level concerns (tokens, CORS, headers)
-
Use Interceptors for application-specific logic (RBAC, audit, pre/post handling)
Understanding where each fits in the request lifecycle ensures cleaner architecture, easier debugging, and maintainable code.
0 Comments