SpringBoot 拦截器的使用
2019-03-05 本文已影响0人
maxzhao_
- 使用注解
@Configuration
配置拦截器 - 继承
WebMvcConfigurerAdapter
,SpringBoot2.0
以后可以直接实现WebMvcConfigurer
接口,这涉及到JDK8
中Interface
新特性 - 重写
addInterceptors
添加需要的拦截器地址
registry.addInterceptor(new OneInterceptor())
.addPathPatterns("/testController/**");
// 拦截多个接口
// registry.addInterceptor(new OneInterceptor())
.addPathPatterns("/testController/**")
.addPathPatterns("/test2Controller/**");
// 拦截所有接口
// registry.addInterceptor(new OneInterceptor())
.addPathPatterns("/*/**");
super.addInterceptors(registry);
- 添加拦截器类实现
HandlerInterceptor
,比如OneInterceptor