addInterceptors(InterceptorRegistry registry)
这个方法可以自定义拦截器
publicclassMyInterceptorimplementsHandlerInterceptor{
@Override
publicbooleanpreHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throwsException{
System.out.println("preHandle....");
return true;
}
@Override
publicvoidpostHandle(HttpServletRequest request,HttpServletResponse response, Object handler, ModelAndView modelAndView)throwsException{
System.out.println("postHandle");
}
@Override
publicvoidafterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throwsException{
System.out.println("afterCompletion...");
}
}
然后配置一下:
@Configuration
publicclassMyConfigurerimplementsWebMvcConfigurer{
@Bean
public MyInterceptor getMyInterceptor(){
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(this.getMyInterceptor()) .addPathPatterns("/abc","/configurePathMatch");
}
}