java – What are the Real time Usage of Spring Interceptor?

java – What are the Real time Usage of Spring Interceptor?

I would like to mention 2 facts about interceptors before mentioning where it could be used in a web application –

  • In spring interceptor is something that sits between the DispatecherServlet and Controller (you already know it)

  • The Interceptor interface is like –

public interface HandlerInterceptor {
  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);
  void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav);
  void afterCompletion(HttpServletRequest request, HttpServeletResponse response, Object handler, Exception ex);


Based on the above facts you can use interceptor in the following situations –

  • You can do extra work before rendering the view. For example, a web page can be handled when the Global Navigation Bar (GNB) requires items to be exposed differently depending on its permissions.

  • Can mimic AOP. @RequestMappingIf the declaration specifies a request for a request, then it HandlerMethod(@Controller)will be called with a handler HandlerMethod. HandlerMethodBy identifying additional information, such as method signatures, you can determine whether to run the logic. So that you can use interceptor as loggin (intercepting the HttpServletRequest), modify the JSON or validate it before or after going to the controller.

Some Real-Time Usage of Interceptor :

  • Maintaining Session / Session Management.
  • Email Shoot & Database Check/logging.
  • Authentication & Authorization Validations.
  • Request Time Calculations. / Performance tunning.

java – What are the Real time Usage of Spring Interceptor?

Leave a Reply

Your email address will not be published. Required fields are marked *