Example of Servlet Filter
Let us discuss some Real-Life examples to better understand the Spring Boot Servlet Filter.
1. Food Ordering: While ordering food on online platforms, a user may give specific cooking instructions like excluding onion or any other substance that a user may be allergic to.
So, this request made by the user will first go through a different procedure – Excluding onions while cooking their food. This same procedure will be applied to all the users who make similar requests. You can think of this procedure as a FILTER for these types of requests.
2. E-Commerce website: It offers bulky discounts when products are purchased in the given time span. So, all the requests made by clients will be applied to a filter of discounts during this time span!
In the above diagrams, we are applying a servlet filter for all those requests with a time stamp between 12:00 and 13:00. If a request is made by a client anywhere all over the world in this time span, it will go through this filter and be given high discounts.
- All the requests made before or after this timestamp will be treated as normal requests and won’t go through a filter.
Spring boot is an extension of the Spring framework & has an inbuilt mechanism for implementing servlet filters easily by using @Component Annotation. Furthermore, if we have more than one filter in our application then we can set the order using @Order annotation to set the execution order of servlet filters.
Here’s the order of execution of the Servlet Filter :
Now, while working with Custom Servlet filters, we will require the following classes and interfaces in our code in spring boot :
- Filter – Interface that our custom filter class must implement
- FilterRegistrationBean – This bean registers our Custom Servlet Filter with the spring context. This allows us to configure the servlet filter for our specific needs such as –
- URL Patterns
- Dispatcher type
- Invocation etc.
However, this one is optional in some cases
Types of Servlet Filters
There are mainly two types of servlet filters:
- Pre-Built Servlet Filter: Pre-built servlet Filters are a convenient and efficient way to implement a specific functionality such as
- authentication – Authentication Filter
- logging – CommonsRequestLoggingFilter
- authorization filter – FilterSecurityInterceptor
- These are some of the pre-built servlet filters provided by Spring Boot, that save us the time and effort of writing a customized filter.
- However, depending on our requirements we may need to implement a filter specific to our needs that’s where Custom Filters are.
- Custom Filters: Custom filters based on our specific needs can be implemented by implementing the Filter Interface.
In this article, we’ll be exploring how to build a custom servlet filter.
Spring Boot – Servlet Filter
Spring boot Servlet Filter is a component used to intercept & manipulate HTTP requests and responses. Servlet filters help in performing pre-processing & post-processing tasks such as:
- Logging and Auditing: Logging operations can be performed by the servlet filter logging the request and response which assists in debugging and troubleshooting.
- Authentication and Authorization: Authentication and Authorization refer to the process of providing access and checking if a user has certain privilege permissions to access a resource
- Caching: Caching is a mechanism that allows us to store data temporarily for faster access in the future. It can be done by caching response in servlet filters.
- Routing: Servlet filters can also be used for routing responses to different controllers giving us more control over handling requests