所有分类
  • 所有分类
  • 未分类

SpringBoot拦截器(Interceptor)的使用

简介 

说明

本文用示例介绍SpringBoot(SpringMVC)中的拦截器的用法。

使用场景

  • 日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等
  • 权限检查:如登录检测,进入处理器检测检测是否登录
  • 性能监控:通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间。(反向代理,如apache也可以自动记录);
  • 通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实现。

Interceptor 的相关方法

Spring 中主要通过 HandlerInterceptor 接口来实现请求的拦截,实现 HandlerInterceptor 接口需要实现下面三个方法:

  1. preHandle()           – 在handler执行之前,返回 boolean 值,true 表示继续执行,false 为停止执行并返回。
  2. postHandle()         – 在handler执行之后, 可以在返回之前对返回的结果进行修改
  3. afterCompletion() – 在请求完全结束后调用,可以用来统计请求耗时等等

推荐写法

  • 实现WebMvcConfigurer接口是官方推荐的

不推荐写法

  • 实现WebMvcConfigurerAdapter:Spring5.0之后是废弃的。
  • 继承WebMvcConfigurationSupport:会导致yml的配置失效。原因:WebMvcAutoConfiguration有个条件注解:@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

实例1:单拦截器拦截所有

代码

拦截器类

package com.example.interceptor;

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

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

public class MyInterceptor implements HandlerInterceptor {

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

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

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

配置类 

package com.example.interceptor;

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 InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
//        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/hello/test2");
    }
}

 Controller

package com.example.controller;

import com.example.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/test1")
    public User test1(User user) {
        System.out.println("HelloController.test1");
        return user;
    }
}

实体类 

package com.example.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

测试

postman访问http://localhost:8080/hello/test1?name=Tony

后端结果:

MyInterceptor.preHandle
HelloController.test1
MyInterceptor.postHandle
MyInterceptor.afterCompletion

postman结果

{
    "id": null,
    "name": "Tony",
    "age": null
}

实例2:单拦截器拦截特定URL

代码

拦截器类

package com.example.interceptor;

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

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

public class MyInterceptor implements HandlerInterceptor {

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

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

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

配置类 

package com.example.interceptor;

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 InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new MyInterceptor());
       registry.addInterceptor(new MyInterceptor()).addPathPatterns("/hello/test2");
    }
}

 Controller

package com.example.controller;

import com.example.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/test1")
    public User test1(User user) {
        System.out.println("HelloController.test1");
        return user;
    }
}

实体类 

package com.example.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

测试

postman访问:http://localhost:8080/hello/test1?name=Tony

后端结果:

HelloController.test1

postman结果

{
    "id": null,
    "name": "Tony",
    "age": null
}

实例3:单拦截器结合注解

使用场景示例:在需要登录验证的Controller的方法上使用注解。当然,也可以直接通过HttpServletRequest获得URI来判断。

代码

注解类

package com.example.interceptor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 在需要登录验证的Controller的方法上使用此注解
 */
@Target({ElementType.METHOD})      // 可用在方法名上
@Retention(RetentionPolicy.RUNTIME)// 运行时有效
public @interface LoginRequired {
	
}

拦截器类

package com.example.interceptor;

import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class MyInterceptor3 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("MyInterceptor3.preHandle");
        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        // 方法注解级拦截器
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        // 判断接口是否需要登录。也可用method.isAnnotationPresent(LoginRequired.class)
        LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);
        // 有 @LoginRequired 注解,需要认证
        if (methodAnnotation != null) {
            // 取缓存,SESSION,权限判断等
            System.out.println("   权限判断等");
            return true;
        }
        return true;
    }

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

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

拦截器配置

package com.example.interceptor;

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 InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //这个顺序会决定拦截器执行顺序
        registry.addInterceptor(new MyInterceptor3());
        // registry.addInterceptor(new MyInterceptor2());
        // registry.addInterceptor(new MyInterceptor1());
        // registry.addInterceptor(new MyInterceptor()).addPathPatterns("/hello/test2");
    }
}

controller

package com.example.controller;

import com.example.entity.User;
import com.example.interceptor.LoginRequired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @LoginRequired
    @RequestMapping("/test1")
    public User test1(User user) {
        System.out.println("HelloController.test1");
        return user;
    }
}

实体类 

package com.example.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

测试

postman访问http://localhost:8080/hello/test1?name=Tony

后端结果

MyInterceptor3.preHandle
   权限判断等
HelloController.test1
MyInterceptor3.postHandle
MyInterceptor3.afterCompletion

postman结果

{
    "id": null,
    "name": "Tony",
    "age": null
}

实例4:多个拦截器拦截所有

代码

拦截器1

package com.example.interceptor;

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

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

public class MyInterceptor1 implements HandlerInterceptor {

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

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

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

拦截器2

package com.example.interceptor;

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

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

public class MyInterceptor2 implements HandlerInterceptor {

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

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

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

配置类

package com.example.interceptor;

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 InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果不写.order,则按照先后顺序
        registry.addInterceptor(new MyInterceptor1()).order(0);
        registry.addInterceptor(new MyInterceptor2()).order(1);
    }
}

 controller

package com.example.controller;

import com.example.entity.User;
import com.example.interceptor.LoginRequired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @LoginRequired
    @RequestMapping("/test1")
    public User test1(User user) {
        System.out.println("HelloController.test1");
        return user;
    }
}

实体类 

package com.example.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

测试

测试1:测试接口

postman访问:http://localhost:8080/hello/test1?name=Tony

后端结果

MyInterceptor1.preHandle
MyInterceptor2.preHandle
HelloController.test1
MyInterceptor2.postHandle
MyInterceptor1.postHandle
MyInterceptor2.afterCompletion
MyInterceptor1.afterCompletion

postman结果

{
    "id": null,
    "name": "Tony",
    "age": null
}

测试2:颠倒顺序

配置类(颠倒顺序)

package com.example.interceptor;

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 InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果不写.order,则按照先后顺序
        registry.addInterceptor(new MyInterceptor2()).order(0);
        registry.addInterceptor(new MyInterceptor1()).order(1);
    }
}

测试结果(颠倒顺序后)

postman访问:http://localhost:8080/hello/test1?name=Tony

后端结果

MyInterceptor2.preHandle
MyInterceptor1.preHandle
HelloController.test1
MyInterceptor1.postHandle
MyInterceptor2.postHandle
MyInterceptor1.afterCompletion
MyInterceptor2.afterCompletion

postman结果

{
    "id": null,
    "name": "Tony",
    "age": null
}
0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录