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