Springboot2.x 拦截器

时间:2022-08-14 06:06:41

一,单个拦截器,实现接口  HandlerInterceptor

@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

@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");
}
}

 Springboot2.x 拦截器

二 ,多个拦截器

咱建个拦截器 2

@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");
}
}

  现在看看两个拦截器执行顺序

Springboot2.x 拦截器

现在把 拦截器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"); }
}

  然后顺序就变了,日志如下

Springboot2.x 拦截器

完了。