SpringBoot(五)SpringBoot拦截器
1. 继承HandlerInterceptorAdapter, 实现preHandle前置处理器,return true则进行后续处理,实现postHandle,afterCompletion后置处理。
2.实现WebMvcConfigurer, SpringBoot2实现WebMvcConfigurationSupport
1 @Configuration 2 public classs WebConfig extends WebMveConfigurationSupport 3 { 4 @Override 5 public void addInterceptors(InterceptrorRegistry registry){ 6 //设置拦截路径 7 registry.addInterceptor(new 8 MyHttpInterrcptor()).addPathPatterns("/**") 9 .excludePathPatterns("openApi/login"); 10 super.addInterceptors(registry); 11 } 12 }
3. 拦截流程
preHandle2 => preHandle1 => Controller => postHandle1 => postHandle2 => view rander => afterCompletion1 => afterCompletion2
4.应用场景
权限认证,日志记录,处理Cookie,本地化国际化,主题,性能监控