Configuration and use of Interceptor in spring MVC

Posted by asanvicente on Thu, 17 Feb 2022 15:46:21 +0100

Interceptor

1.1. concept

Interceptors in Java are objects that dynamically intercept action calls. It provides a mechanism that allows developers to define the code to be executed before and after the execution of an action, or prevent the execution of an action before it is executed. At the same time, it also provides a way to extract the reusable part of an action. In AOP (aspect oriented programming), interceptors are used to intercept a method (including a constructor) or field before it is accessed, and then add some operations before or after it. In particular, at this stage, Spring itself only supports method based interception! If the method based interception operation cannot meet the requirements, AspectJ can be used to integrate with Spring to achieve finer grained or more aspects of interception operation.

1.2. principle

The interceptor of spring MVC is implemented based on Java dynamic proxy

1.3. effect

Interceptors are mainly used for user login processing, permission checking, logging, etc

1.4. Implementation mode

  1. By implementing the HandlerInterceptor interface
  2. Abstract class by inheriting AbstractInterceptor

public interface HandlerInterceptor {

	/*
		Pretreatment method
		It is executed before the controller method, in which the requested information, such as uri, can be obtained
		The request information can be used to determine whether to cut the request or release the request
		handler : Intercepted controller object
	*/
    default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

	/*
		Post processing method
		After the controller method is executed, the return value of the controller method can be obtained and the return result can be modified
	*/
    default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

	/*
		Executed after request processing is completed
		Generally do resource recycling
	*/
    default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }
}

The method permission modifier of the HandlerInterceptor interface is default, and you can choose your own implementation method (Spring 5.3.4)

1.5. Interceptor configuration

Configure the interceptor in the configuration file of spring MVC

<mvc:interceptors>
	<mvc:interceptor>
        <mvc:mapping path="The intercepted request path can use wildcard"/>
        <bean class="Fully qualified class name of interceptor object"/>
    </mvc:interceptor>
</mvc:interceptors>

1.6. Interceptor chain

There may be multiple interceptors in the project. What is the execution order of each method of multiple interceptors

public class MyInterceptor1 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("11111preHandle Method execution");
        return false;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("11111postHandle Method execution");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("11111afterCompletion Method execution");
    }
}

public class MyInterceptor2 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("22222preHandle Method execution");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("22222postHandle Method execution");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("22222afterCompletion Method execution");
    }
}

Define two interceptor objects and configure them in the configuration file. The order is myinterceptor1 > myinterceptor2
When both interceptors are released, the execution result is

When the first interceptor is released and the second interceptor is truncated, the execution result is

When the first interceptor is truncated, the execution result is

The execution diagram is as follows

Topics: Spring MVC