一,单个拦截器,实现接口 HandlerInterceptorjava
@Component public class MyInterceptor1 implements HandlerInterceptor { /** * 进入controller层以前拦截请求 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("============1111111111---preHandle============"); return true; } /** * 处理请求完成后视图渲染以前的处理操做 * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { System.out.println("============1111111111---postHandle ============"); } /** * 视图渲染以后的操做 * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { System.out.println("============1111111111---afterCompletion============"); } }
新建类 InterceptorConfig 实现 WebMvcConfigurer ide
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Resource private MyInterceptor1 myInterceptor1; @Override public void addInterceptors(InterceptorRegistry registry) { //添加要拦截的url 拦截的路径 放行的路径 registry.addInterceptor(myInterceptor1).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); } }
二 ,多个拦截器post
咱建个拦截器 2url
@Component public class MyInterceptor2 implements HandlerInterceptor { /** * 进入controller层以前拦截请求 * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("============2222222---preHandle============"); return true; } /** * 处理请求完成后视图渲染以前的处理操做 * @param request * @param response * @param handler * @param modelAndView * @throws Exception */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { System.out.println("============2222222---postHandle ============"); } /** * 视图渲染以后的操做 * @param request * @param response * @param handler * @param ex * @throws Exception */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { System.out.println("============2222222---afterCompletion============"); } }
而后在 InterceptorConfig 添加配置日志
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Resource private MyInterceptor1 myInterceptor1; @Resource private MyInterceptor2 myInterceptor2; @Override public void addInterceptors(InterceptorRegistry registry) { //添加要拦截的url 1 拦截的路径 放行的路径 registry.addInterceptor(myInterceptor1).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); //添加要拦截的url 2 registry.addInterceptor(myInterceptor2).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); } }
如今看看两个拦截器执行顺序blog
如今把 拦截器1和2的位置换下接口
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Resource private MyInterceptor1 myInterceptor1; @Resource private MyInterceptor2 myInterceptor2; @Override public void addInterceptors(InterceptorRegistry registry) { //添加要拦截的url 2 registry.addInterceptor(myInterceptor2).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); //添加要拦截的url 1 拦截的路径 放行的路径 registry.addInterceptor(myInterceptor1).addPathPatterns("/admin/**").excludePathPatterns("/admin/login"); } }
而后顺序就变了,日志以下io
完了。class