Using interceptors in spring boot

Interceptor is the core content of spring MVC. Using the AOP (Aspect Oriented Programming) feature of spring, it is convenient to extract the user's business code horizontally and enhance the application function according to the specific business requirements.
Interceptor is used in SpringBoot and developed with full annotation, involving the following interfaces and classes:

  1. Handler Interceptor: processor interceptor. Handler is the processor. In spring boot web development, the controller handles web requests. Therefore, handler specifically refers to the controller
  2. Full annotation development is used. Through @ Configuration annotation, a java object is added to the IOC container and used as a Configuration object. The JavaConfig class here is equivalent to an xml Configuration file;
  3. In the previous xml configuration
    (1) Configure by introducing some tags. In JavaConfig, configure by inheriting a class or implementing an interface. The inherited class and implemented interface here are equivalent to the imported tags;
    (3) Personalized configuration can be realized by setting the attributes and values of the imported tags. Personalized configuration can be realized by overriding classes or interfaces in JavaConfig.

The following is a case to implement a custom interceptor
Intercept the request starting with / user /, and do not intercept the / usr/login request

1. Define interceptors

package cn.eis220.web;

import org.springframework.web.servlet.HandlerInterceptor;

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

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Yes logininterceptor of preHandle method");
        return true;
    }
}

2. Register the interceptor with JavaConfig

The java configuration class is equivalent to an xml configuration file
xml is configured by introducing interceptor tag, and java configuration class is configured by implementing WebMvcController;
xml can be configured by modifying the attributes and values of tags, and java configuration class can be configured by implementing WebMvcController

package cn.eis220.config;

import cn.eis220.web.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyAppCofnig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor = new LoginInterceptor();

        String[] path = {"/user/**"};
        String[] excludePath = {"/user/login"};
	registry.addInterceptor(loginInterceptor).addPathPatterns(path).excludePathPatterns(excludePath);
    }
}

3. Define the controller and test the interceptor

package cn.eis220.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BootController {
    @RequestMapping("/user/account")
    @ResponseBody
    public String userAccount(){
        return "/user/account";
    }

    @RequestMapping("/user/login")
    @ResponseBody
    public String userLogin(){
        return "/user/login";
    }
}

4. Summary

Steps:

  1. How to define your own Interceptor:
    Implement the method of HandlerInterceptor interface, and customize the interceptor
  2. How to register interceptors:
    The JavaConfig configuration class implements the addInterceptor method of the WebMvcConfigurer interface to register interceptors

Keywords: Spring Boot interceptor

Added by tomfra on Sat, 15 Jan 2022 08:23:10 +0200