So sánh spring interceptor với filter

Filters, Interceptors, and Listeners are three common components of the Spring Boot framework whose primary function is to process and enhance requests and responses before they reach the controller or after they leave the controller. Although their roles are similar, the implementation mechanism, scope and usage differ between them.

Show

Filter (Filter) Filter is a component provided by the Servlet container, mainly used for pre-processing HTTP requests and post-processing HTTP responses. Filters are a global component that can intercept all requests and responses, including static resource files (such as HTML, CSS and JS files) and dynamic resource files (such as JSPs and Servlets). In Spring Boot, it can be registered using FilterRegistrationBean.

Interceptor Interceptor is a component provided by the Spring Framework and is mainly used to process and enhance requests and responses before they reach the controller or after the response leaves the controller. Interceptors can only intercept requests from the Controller layer and cannot intercept requests for static resources. In Spring Boot, you can customize interceptors by implementing HandlerInterceptor interface or by inheriting HandlerInterceptorAdapter class.

Listeners (Listener) A listener is a component of a JavaWeb application that is used to listen for events in the Web application such as requests, sessions, contexts, etc. In Spring Boot, custom listeners can be implemented by implementing the ApplicationListener interface.

Translated with www.DeepL.com/Translator (free version)

Filter

  • The filter is initialized once when the web application starts and destroyed when the web application stops
  • Can filter the requested URL and filter sensitive words
  • In the outer layer of the interceptor
  • Implements the javax.servlet. Filter interface, which is part of the Servlet specification
  • After the request enters the container, but before entering the servlet, the request ends after the servlet has finished processing
  • Dependency Web Container
  • Will be executed multiple times

Looking to compare Spring Interceptors and Filters? This ultimate comparison guide will help you understand the pros and cons of each and how they impact the execution flow of your Spring Boot application.

Context

When building Spring Boot applications, it’s common to come across the terms “interceptor” and “filter”. Both are used for intercepting requests and responses in the application, but there are some key differences between the two. In this blog post, we’ll explore the difference between Spring Interceptors and Filters.

Spring Interceptors are used to intercept requests and responses at the controller level. They are executed before the controller method is invoked and after the controller method has returned. This means that you can modify the request or response before it reaches the controller or after it has been processed by the controller.

Interceptors are typically used for:

  • Authentication and authorization
  • Logging
  • Performance monitoring
  • Customizing the request or response

Spring Interceptors are implemented using the HandlerInterceptor interface. This interface defines three methods:

  • preHandle() – Executed before the controller method is invoked. This method returns a boolean value indicating whether to continue processing the request or abort the request.
  • postHandle() – Executed after the controller method has returned, but before the view is rendered. This method allows you to modify the model or view before rendering.
  • afterCompletion() – Executed after the view has been rendered. This method allows you to perform any cleanup tasks.

Filters

Filters, on the other hand, are used to intercept requests and responses at the servlet level. They are executed before the request reaches the servlet and after the response has been generated. Filters can be used to modify the request or response or to perform some other action on the request or response.

Filters are typically used for:

  • Authentication and authorization
  • Compression and decompression
  • Encryption and decryption
  • URL rewriting

Filters are implemented using the jakarta.servlet.Filter interface. This interface defines one method:

  • doFilter() – Executed for each request and response that passes through the filter. This method allows you to modify the request or response, or to perform some other action on the request or response.

Key Differences

The key difference between Spring Interceptors and Filters is the level at which they operate. Interceptors operate at the controller level, while Filters operate at the servlet level. This means that Interceptors have access to the controller and can modify the model and view, while Filters do not have access to the controller and can only modify the request and response.

Another difference is the order in which they are executed. Interceptors are executed before and after the controller method, while Filters are executed before and after the servlet. This means that Interceptors have more fine-grained control over the request and response, while Filters have more general control over the request and response.

Sr. No.AspectSpring InterceptorsFilters1.Level of operationController levelServlet level2.AccessAccess to controllerNo access to controller3.Execution orderBefore/after controller methodBefore/after servlet4.Defined inSpring FrameworkServlet specification5.InterfaceHandlerInterceptorjakarta.servlet.FilterSpring Interceptors vs Filters

Request Execution Flow

  1. A request is received by the Spring Boot application.
  2. The request passes through the filter chain, which is responsible for handling low-level tasks such as authentication and URL rewriting. Each filter in the chain can modify the request or response before passing it on to the next filter.
  3. Once the request has passed through the filter chain, it reaches the DispatcherServlet.
  4. The DispatcherServlet matches the request to the appropriate controller method.
  5. Before the controller method is invoked, the request passes through the interceptor chain, which is responsible for handling high-level tasks such as logging and performance monitoring. Each interceptor in the chain can modify the request or response before passing it on to the next interceptor.
  6. Once the request has passed through the interceptor chain, the controller method is invoked.
  7. The controller method generates a response, which passes back through the interceptor chain.
  8. After the response has passed through the interceptor chain, it passes back through the filter chain. Each filter in the chain can modify the response before it is sent back to the client.

Conclusion

In summary, Spring Interceptors and Filters are both used for intercepting requests and responses in web applications. Interceptors operate at the controller level, while Filters operate at the servlet level. Interceptors have more fine-grained control over the request and response, while Filters have more general control over the request and response. Both Interceptors and Filters are useful tools for customizing and enhancing web applications, and it’s important to understand the differences between them when choosing which to use in your application.