SpringBoot 拦截器的使用

  • 使用注解@Configuration配置拦截器
  • 继承WebMvcConfigurerAdapterSpringBoot2.0以后可以直接实现WebMvcConfigurer接口,这涉及到 JDK8Interface 新特性
  • 重写addInterceptors添加需要的拦截器地址
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    registry.addInterceptor(new OneInterceptor())
    .addPathPatterns("/testController/**");
    // 拦截多个接口
    // registry.addInterceptor(new OneInterceptor())
    .addPathPatterns("/testController/**")
    .addPathPatterns("/test2Controller/**");
    // 拦截所有接口
    // registry.addInterceptor(new OneInterceptor())
    .addPathPatterns("/*/**");
    super.addInterceptors(registry);
  • 添加拦截器类实现HandlerInterceptor,比如OneInterceptor

本文地址:SpringBoot 拦截器的使用

本文地址: https://github.com/maxzhao-it/blog/post/35633/