Backend Development 9 min read

Spring Boot 3: 7 Key Differences Between Filters and Interceptors

This article explains the fundamental distinctions between Spring Boot filters and interceptors—including execution timing, scope, registration methods, request handling, modification capabilities, exception mechanisms, and typical use cases—while providing practical code examples for each.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Boot 3: 7 Key Differences Between Filters and Interceptors

1. Introduction

In Spring Boot development, filters and interceptors are both used for pre‑ and post‑processing of requests, but they differ in implementation, execution timing, and application scenarios. This article analyzes seven core differences.

2. Comparison

2.1 What is a Filter?

A filter is part of the Servlet specification (Jakarta EE) and works at a lower level, processing every incoming request before it reaches the DispatcherServlet.

When to use a Filter

Logging – trace request and response details.

GZIP compression – reduce response size.

Character encoding – ensure consistent text encoding.

Security checks – firewalls, rate limiting, access control.

Request/response modification – wrap, sanitize, cache.

Example

<code>@WebFilter("/*")
public class LoggingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("Request entering...");
        chain.doFilter(request, response);
        System.out.println("Response sent...");
    }
}
</code>

2.2 What is an Interceptor?

An interceptor is part of Spring MVC and works inside the Spring framework, allowing modifications specific to controllers.

When to use an Interceptor

Authentication and authorization – verify user credentials.

Execution time logging – measure controller handling time.

Model/View modification – adjust data before sending response.

Adding common attributes – e.g., user details to all responses.

Example

<code>@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println("Checking authentication...");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) {
        System.out.println("Controller executed.");
    }
}
</code>

2.3 Differences

Execution point : Filters run before the DispatcherServlet, affecting all incoming HTTP traffic; interceptors run before/after controller methods, only within Spring MVC.

Scope : Filters apply to all requests, including static resources; interceptors apply only to controller requests.

Registration : Filters can be registered with @WebFilter + @ServletComponentScan or via FilterRegistrationBean ; interceptors are added through WebMvcConfigurer#addInterceptors .

Request type : Filters handle every HTTP request; interceptors handle only Spring MVC controller requests.

Modification of request/response : Filters can modify headers, encoding, compression, etc.; interceptors can modify the model or view but not the raw request/response body.

Exception handling : Filters cannot directly use @ControllerAdvice ; interceptors can rely on global exception handlers.

Typical use cases : Filters are suited for logging, compression, security checks, and request/response transformations across the whole application; interceptors are ideal for authentication, timing, and view/model adjustments.

Filter registration example

<code>@Bean
public FilterRegistrationBean<LoggingFilter> loggingFilter() {
    FilterRegistrationBean<LoggingFilter> reg = new FilterRegistrationBean<>(new LoggingFilter());
    reg.addUrlPatterns("/*");
    return reg;
}
</code>

Interceptor registration example

<code>@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor())
                .addPathPatterns("/api/**");
    }
}
</code>

Global exception handling example

<code>@RestControllerAdvice
public class GlobalControllerAdvice {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> handle(Exception e) {
        return ResponseEntity.ok("Error: " + e.getMessage());
    }
}
</code>

Result of the filter example:

Filter result
Filter result

Result of the interceptor example:

Interceptor result
Interceptor result
JavaWebbackend developmentSpring BootInterceptorFilter
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.