Empowering Businesses with Tech
Filter and Interceptor
Filter
In this section, we will learn about:
​
-
Spring Boot Filter
-
Implementation of filter
Spring Boot Filter
Spring Boot Filter is a Java class that is executed by the servlet container for each incoming HTTP request and each HTTP response.
Servlet Container
Servlets run in a servlet container that handles the networking side (e.g. parsing an HTTP request, connection handling, etc). Tomcat is the most popular one.
By using a filter, we can perform two operations at two instances −
​
-
Before sending the request to the controller.
-
Before sending a response to the client.
It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation, etc. For example, you would like to add a response header to each generated response. Instead of adding this header in each resource method, you would use a response filter to add this header.
We use filters to intercept incoming requests and outgoing responses. We can do so by creating filters and inheriting existing filters and overriding their methods at our convenience. We have made a filter in the “Spring Boot Security” session coming next.
Implementation of filter
We are going to implement a logging filter, which will log something which we provide every time a request is received.
-
Create a class and implement a Filter interface. The filter would be imported from the “javax.servlet” library.
-
Then we will override the “doFilter” of the filter interface. And create two logs that will be executed every time a request is received.
-
Now, we will run the application and test whether our filter is working or not.
-
So, we will execute the “authenticate” API and we can see the logs.
Fig: Logs showing server name every time a request is sent.
Interceptors
​Spring Interceptors are similar to Servlet Filters. As filters are primarily intended to manipulate request and response parameters like HTTP headers, URIs, and/or HTTP methods, interceptors are intended to manipulate entities, via manipulating entity input/output streams. If you for example need to encode the entity-body of a client request then you could implement an interceptor to do the work for you.
Implementation of Interceptor
-
We will create an interceptor that will extend “HandlerInterceptorAdapter” and we can override any of three methods or all as per our convenience:
-
prehandle(): It is called before the execution of the actual handler. It returns a boolean. If it returns true, it means the request will continue to come to its destination.​
-
postHandle(): It is called after the handler is executed. We can use this spring interceptor method to determine the time taken by the handler method to process the client request.
-
afterCompletion(): It is called after the complete request is finished and the view is generated. When a request is finished and the view is rendered, we may obtain request and response data, as well as information about exceptions.​​
-
-
We will create a “DemoInterceptor” class and annotate it with @Component.
-
Now we will override pre handle method and the after-completion method.
-
In pre handle method we will set an attribute of the request as start time by using “System.currentTimeMillis()”. We will also print the current time when the request came to pre handle method.
-
In the after-completion method, we will get the start time attribute of the request and print both the end time (which we will find by the current time) and the total time to process the request i.e. end time - start time.
-
Now, we need to create a web configuration class that will implement the “WebMvcConfigurer” interface. And we will override a method of “addInterceptors”. Here we can add multiple interceptors and specify where these interceptors will be used.
-
And whenever a request is sent, the message provided will be printed.