Configure Interceptor With a Spring Boot

Posted By : Amarjeet Yadav | 21-Apr-2020

 

In spring Boot when a request is sent to the spring controller, it will have to pass through Interceptors (0 or more) before being processed by Controller.

 

When you come to a company and want to meet the company's manager. You need to go through interceptors which may be a gatekeeper, a receptionist,...

Spring interceptor could be a construct that's rather like Servlet Filter.

Spring interceptor is simply applied to requests that are sending to a Controller.

You can use an associate interceptor to feature the request header before sending the request to the controller and add the response header before sending the response to the consumer.

You can use an interceptor to try to some tasks like writing log, adding or change configurations before a request is processed by Controller.

To work with the interceptor, you would like to form a @Component category that supports it and it ought to implement the HandlerInterceptor interface.

 

You need to implement three abstract methods:

 

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

 

public void postHandle( request,  response, handler, modelAndView)

 

public void afterCompletion( request, response,  handler,exception)

 

 

package com.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class CustomInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle
      (HttpServletRequest request, HttpServletResponse response, Object handler) 
      throws Exception {
      
      System.out.println("Pre Handle method is Calling");
      return true;
   }

 @Override
   public void postHandle(
      HttpServletRequest request, HttpServletResponse response, Object handler, 
      ModelAndView modelAndView) throws Exception {}

  @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
      Object handler, Exception exception) throws Exception {}
}

 

You will have to register  Interceptor with InterceptorRegistry by using WebMvcConfigurerAdapter as shown below −

 

 

package com.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
   @Autowired
   CustomInterceptor customInterceptor;

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
      registry.addInterceptor(customInterceptor);
   }
}

 

Conclusion

After going through this blog, you can use interceptors in an effective way. Just like adding request header(s) before sending the request to the controller or adding the response header(s) before sending the response to the client.

About Author

Author Image
Amarjeet Yadav

He is having knowledge of core java , advance java servlet, jsp springMvc, Mysql etc.

Request for Proposal

Name is required

Comment is required

Sending message..