Filter vs. Interceptor in Spring Boot

Photo by Krisztian Tabori on Unsplash

What is a Servlet?

What is a Servlet Container?

Filter

  • init(FilterConfig config) — This is invoked only once. It is used to initialize the filter.
  • doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain) — This method is invoked every time a user send a request to any resource, to which the filter is mapped. It is used to perform filtering tasks.
  • destroy() — This is invoked only once when filter is taken out of the service.
Servlet Invocation with and without Filters (docs.oracle.com)

Interceptor

  • preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) — This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.
  • postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
    ModelAndView modelAndView)
    — This is used to perform operations before sending the response to the client.
  • afterCompletion(HttpServletRequest request, HttpServletResponse response,
    Object handler, Exception exception)
    — This is used to perform operations after completing the request and response.
Filters and HandlerInterceptors (baeldung)

Filters vs HandlerInterceptors

  • Filter is related to the Servlet API and HandlerIntercepter is a Spring specific concept.
  • Interceptors will only execute after Filters.
  • Fine-grained pre-processing tasks are suitable for HandlerInterceptors (authorization checks, etc.)
  • Content handling related or generic flows are well-suited for Filters (such as multipart forms, zip compression, image handling, logging requests, authentication etc.)
  • Interceptor’s postHandle method will allow you to add more model objects to the view but you can not change the HttpServletResponse since it's already committed.
  • Filter’s doFilter method is much more versatile than Interceptor’s postHandle. You can change the request or response and pass it to the chain or even block the request processing.
  • A HandlerInterceptor gives more fine-grained control than a filter because you have access to the actual target “handler”. You can even check if the handler method has a specific annotation.

Spring Cloud Gateway Custom Filters

GlobalFilter

GatewayFilter

baeldung

I would love to change the world, but they won’t give me the source code | coding 👩🏼‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Various String Formatting approaches in Python

Python can type, what you say !

Life is a journey of twists and turns peaks and valleys mountains to climb and oceans to explore.

Docker on local Machine!

The Journey from Desktop to iPad as my Primary Device

Trailhead Badges Release : August 21’

Salesforce august release

Azure Kubernetes Service

ATM analogy for leetcode #104 Maximum Depth of Binary Tree

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nil Seri

Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏼‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨

More from Medium

Spring boot - handle Rest APIs exceptions using Controller advice

Build a Spring Boot REST API with Pagination and Sorting

One common mistake which we should avoid while using @Async and @Cacheable in Spring boot

SpringBoot: Rule Engine For Classifying Celestial Objects