Request Interceptor
Request Interceptor is an additional component class that intercepts all the incoming and outgoing requests (before any action is performed). It has the following 3 methods :
1. preHandle(): When an interceptor is implemented, any request before reaching the desired controller will be intercepted by this interceptor and some pre-processing can be performed like logging, authentication, redirection, etc.
- This method takes 3 parameters :
- HttpServletRequest request – represents the request being handled,
- HttpServletResponse response – represents the HTTP response to be sent back to the client,
- Object handler – the target controller method that will handle this request.
- Boolean return type: If the method returns true then the request will be directed towards the target control else the target controller method won’t be invoked if this method returns false and the request will be halted.
2. postHandle(): This method is executed after the request is served but just before the response is sent back to the client. It intercepts the request in the final stage, giving us a chance to make any final trivial adjustments.
- We can modify the view response, for certain specific conditions.
- It takes 4 parameters –
- 3 are same as previous but there is one more
- ‘ModelAndView’. It contains information about the model (data that is shipped across the parts of our web application) and the view that is rendered by the client.
- It can be used for debugging, logging, and capturing final response data.
3. afterCompletion(): This method is executed after the request and response mechanism is completed.
- This method can turn out to be very useful in cleaning up the resources once the request is served completely.
- It also takes 4 parameters, but the ‘ModelAndView’ object is replaced by an Exception object which contains information if any Exceptions occurred while serving the request.
@Component annotation tells the component scanning mechanism that this class should be registered for component scanning.
Java
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class RequestInterceptor implements HandlerInterceptor { // Request is intercepted by this method before reaching the Controller @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //* Business logic just when the request is received and intercepted by this interceptor before reaching the controller try { System.out.println( "1 - preHandle() : Before sending request to the Controller" ); System.out.println( "Method Type: " + request.getMethod()); System.out.println( "Request URL: " + request.getRequestURI()); } //* If the Exception is caught, this method will return false catch (Exception e) { e.printStackTrace(); return false ; } return true ; } // Response is intercepted by this method before reaching the client @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { //* Business logic just before the response reaches the client and the request is served try { System.out.println( "2 - postHandle() : After the Controller serves the request (before returning back response to the client)" ); } catch (Exception e) { e.printStackTrace(); } } // This method is called after request & response HTTP communication is done. @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { //* Business logic after request and response is Completed try { System.out.println( "3 - afterCompletion() : After the request and Response is completed" ); } catch (Exception e) { e.printStackTrace(); } } } |
Spring Boot – Interceptor
Spring Boot Interceptor is an additional component that will intercept every request and response dispatch and perform some operations on it.
Interceptors in web applications play a major in performing initial or finalization work.
Interceptors have one primary purpose – “To intercept an incoming or outgoing request”.
This intercepting behavior is done to perform some operation BEFORE or AFTER an action invocation. You can think of an interceptor as a mediator between the request and the business logic reserved for that request. Interceptor in Spring Boot can be implemented either by extending the HandlerInterceptorAdapter class or by implementing HandlerInterceptor Interface.
Interceptors are useful for many purposes some of which are listed below :
1. Logging
Logging is a common practice to understand the flow of code & debugging. Interceptors are widely for the same, Documenting or recording the requests made by the client or the responses sent back to them is generally useful in debugging and monitoring purposes.
2. Authentication
Authentication is a security mechanism that can be implemented inside an Interceptor before the request even reaches its appropriate controller. It can be intercepted by the Interceptor & authentication can be verified, if denied then the request doesn’t get delegated to the controller which not only minimizes the code but reduces a great overhead.
3. Request/Response Modification
Interceptors are also used for modifying requests & responses. It can be done in the following ways :
- Adding or modifying request parameters
- Modifying request headers, or response head
- Change status code
- Redirection to a different URL
- Handle custom error requests/responses which frees the controller or an addition of an external component just to handle the exceptions.
Contact Us