基本接口:WebMvcConfigurer或者WebMvcConfigurerAdapter;
函数:addInterceptors(InterceptorRegistry registry);
参数类型说明:InterceptorRegistry类只包含List<InterceptorRegistration>列表一个成员字段;InterceptorRegistration类封装了
Interceptor对象、includeUrl和excludeUrls;
(1)向系统注册一个拦截器
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyCustomInterceptor());
}
}
(2)实现一个拦截器,拦截器实现HandlerInterceptor接口
public class MyCustomInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response){
//your custom logic here.
return true;
}
}
(3)针对某些url进行拦截
InterceptorRegistration.addPathPatterns()和InterceptorRegistration.excludePathPatterns()函数实现