Interceptors for Spring MVC

Keywords: Java Spring Session xml

Original link: http://www.yiidian.com/springmvc/interceptor.html

Interceptors in Spring MVC are similar to filters in Servlet s, which are used to intercept user requests and process them accordingly.For example, the Interceptor can verify permissions, log requests, determine whether users are logged in, and so on.

To use interceptors in Spring MVC, you need to define and configure interceptor classes.Often, interceptor classes can be defined in two ways.

By implementing the HandlerInterceptor interface
Inherit the implementation class of the HandlerInterceptor interface (for example, HandlerInterceptor Adapter) to define it.

1 Write target controller

First, to demonstrate the effectiveness of the Spring MVC interceptor, we define a target controller

HelloController Target Controller:

package com.yiidian.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

/**
 * Target Controller
 * One Point Tutorial Web - www.yiidian.com
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String upload(HttpSession session, HttpServletResponse response) throws Exception {
        System.out.println("3.Target Controller-HelloController");
        return "success";
    }
}

2 Write a custom interceptor

Here we define two Spring MVC interceptors, and these two interceptors intercept a target controller at the same time. Look at the last execution sequence.

Demo1Interceptor first interceptor:

package com.yiidian.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *  custom interceptor
 *One Point Tutorial Web - www.yiidian.com
 */
public class Demo1Interceptor implements HandlerInterceptor{
    /**
     *preHandle: Executed before controller (target) method
     *   Return value: Controls whether the afterCompletion method is executed
     *       true: Execute afterCompletion
     *       false: Do not perform afterCompletion
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("1.Demo1Interceptor Of preHandle");
        return true;
    }

    /**
     * postHandle: After successful execution of the controller (target) method (note: controller method fails without execution)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("5.Demo1Interceptor Of postHandle");
    }

    /**
     *  afterCompletion: Execute after executing all the previous (interceptor and target) methods (Note: Controller methods will be executed regardless of success)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("7.Demo1Interceptor Of afterCompletion");
    }
}

Demo2interceptor second interceptor:

package com.yiidian.interceptor;

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

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

/**
 *  custom interceptor
 *One Point Tutorial Web - www.yiidian.com
 */
public class Demo2Interceptor implements HandlerInterceptor{
    /**
     *preHandle: Executed before controller (target) method
     *   Return value: Controls whether the afterCompletion method is executed
     *       true: Execute afterCompletion
     *       false: Do not perform afterCompletion
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("2.Demo2Interceptor Of preHandle");
        return true;
    }

    /**
     * postHandle: After successful execution of the controller (target) method (note: controller method fails without execution)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("4.Demo2Interceptor Of postHandle");
    }

    /**
     *  afterCompletion: Execute after executing all the previous (interceptor and target) methods (Note: Controller methods will be executed regardless of success)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("6.Demo2Interceptor Of afterCompletion");
    }
}

Note: The sequence of execution of one interceptor and multiple interceptors is shown below.

Execution order of an interceptor:

Execution order of multiple interceptors:

3 springmvc.xml configuration interceptor

<!-- Configuring Interceptors -->
<mvc:interceptors>
    <!-- Configure 2 interceptors -->
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean class="com.yiidian.interceptor.Demo1Interceptor"/>
    </mvc:interceptor>

    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean class="com.yiidian.interceptor.Demo2Interceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

Note: The order of interceptor configuration determines the execution order of interceptors, the first configuration will be executed first!

4 Run tests

Access: http://localhost:8080/hello

The console output is as follows:

Source download: https://pan.baidu.com/s/1 myzqCpvlhVb4aJkb5aucdQ

Welcome to my public number: A little tutorial.Get exclusively organized learning resources and daily dry delivery.
If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Posted by cordex on Sat, 21 Mar 2020 17:44:14 -0700