Spring MVC - configuration of interceptors - use of interceptors - detailed explanation of interceptors - execution process of multiple interceptors

1. Function of interceptor

The interceptor of Spring MVC is similar to the Filter in Servlet development, which is used to preprocess and post process the processor.

The interceptors are connected into a chain in a certain order, which is called Interceptor Chain. When accessing the intercepted method or field, the interceptors in the Interceptor Chain will be called in the order they were previously defined. Interceptor is also the concrete implementation of AOP idea.

Different from the filter, the interceptor only intercepts the processor (the method in the Controller), not some static resources such as JS and JSP

[see the bottom of the chapter for the link of the original quotation]

  1. Interceptor interceptor is an interceptor provided by spring MVC to intercept Controller layer methods. Its function type is similar to that of Filter filter;
  2. The difference is that the Interceptor is mainly used to record logs, judge whether users log in, and filter permissions (jump to log in without logging in); The filter is used for global filtering, which is mainly used for coding filtering and unified coding
  3. interceptor: a method used to intercept the Controller. It is mainly used to allocate permissions and intercept users without permissions (for example, shopping carts can be viewed only after logging in).
  4. [note] generally, requests are filtered through the filter before being processed by the interceptor to decide whether to release. If either of the two processes is not released, the Controller layer methods cannot be accessed.

2. Main methods of interceptor

  1. preHandle(): this method is called before the business processor processes the request. You can verify some permissions in this method. If the programmer decides to call other interceptors or business processors to process the request after the interceptor intercepts the request, return true; If the programmer decides not to call other components to process the request, false is returned.
  2. postHandle(): this method is called before rendering the view after the business processor processes the request. In this method, models and views in ModelAndView can be processed.
  3. afterCompletion(): this method is called after the DispatcherServlet has completely processed the request (that is, after the view rendering is completed). You can perform some resource cleaning operations in this method.

3. Implementation of single interceptor

Custom interceptors can implement the HandlerInterceptor interface or inherit the HandlerInterceptorAdapter adapter class.

  1. Create a class to implement HandlerInterceptor
package com.atguigu.springmvc.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstInterceptor implements HandlerInterceptor {

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

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("FirstInterceptor of postHandle Method execution");
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("FirstInterceptor of afterCompletion Method execution");
    }
}
  1. Configure interceptors in the core configuration file of spring MVC

Next, you need to introduce the mvc namespace
Configuration mode I

<!--Configuring Interceptors -->
<mvc:interceptors>
    <!--stay interceptors Directly through bean Configuration mode interceptor´╝îThe default is global interception-->
    <bean id="firstInterceptor" class="com.atguigu.springmvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptors>

Configuration mode II

<!--Configuring Interceptors -->
<mvc:interceptors>
    <mvc:interceptor>
        <!--Configure intercepted paths-->
        <mvc:mapping path="/testInterceptor"/>
        <!--Configure non intercepted paths-->
        <!--<mvc:exclude-mapping path="/testInterceptor"/>-->
        <bean id="firstInterceptor" class="com.atguigu.springmvc.interceptors.FirstInterceptor"/>
</mvc:interceptor>

Execution flow of a single interceptor


According to the source code, we can conclude that the execution process of a single interceptor is as follows:

4. Configure multiple interceptors

Who intercepts the same URL by multiple interceptors is determined by the order configured in the spring MVC configuration file.
Who intercepts the same URL by multiple interceptors is determined by the order configured in the spring MVC configuration file.
Who intercepts the same URL by multiple interceptors is determined by the order configured in the spring MVC configuration file.

package com.atguigu.springmvc.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondInterceptor implements HandlerInterceptor {

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

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

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("SecondInterceptor of afterCompletion Method execution");
    }
}
  1. Configure in the core configuration file of spring MVC
<!--Configuring Interceptors -->
<mvc:interceptors>
    <!--stay interceptors Directly through bean Configuration mode interceptor´╝îThe default is global interception-->
    <bean id="firstInterceptor" class="com.atguigu.springmvc.interceptors.FirstInterceptor"/>
    <mvc:interceptor>
        <!--Configure intercepted paths-->
        <mvc:mapping path="/testInterceptor"/>
        <!--Configure non intercepted paths-->
        <!--<mvc:exclude-mapping path="/testInterceptor"/>-->
        <bean id="secondInterceptor" class="com.atguigu.springmvc.interceptors.SecondInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

Execution flow of multiple interceptors

Therefore, we can see from the source code that the execution process of multiple interceptors is as follows:

You can also look at this picture

[link with original text]
[SSM - spring MVC chapter] 09 detailed explanation of spring MVC Interceptor, execution sequence of multiple interceptors, permission judgment and logging of interceptors, and differences between interceptors and filters

Tags: Java Spring Spring MVC interceptor

Posted by Thrakorzog on Mon, 20 Sep 2021 18:29:22 +0530