spring boot 实现多个 interceptor 并指定顺序
首先我们创建Interceptor,实现HandlerInterceptor覆写方法:
一、下面我创建了三个拦截器:
MyInterceptor,UserInterceptor,StudentInterceptor
@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor afterCompletion");
}
}
@Component
public class StudentInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("StudentInterceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("StudentInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("StudentInterceptor afterCompletion");
}
}
@Component
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("UserInterceptor preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("UserInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("UserInterceptor afterCompletion");
}
}
二、把拦截器加入到拦截器队列 实现WebMvcConfigurer
@Configuration
public class MyAdapter implements WebMvcConfigurer {
@Resource
private MyInterceptor myInterceptor;
@Resource
private UserInterceptor userInterceptor;
@Resource
private StudentInterceptor studentInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//1.加入的顺序就是拦截器执行的顺序,
//2.按顺序执行所有拦截器的preHandle
//3.所有的preHandle 执行完再执行全部postHandle 最后是postHandle
registry.addInterceptor(studentInterceptor).addPathPatterns("/**");
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
registry.addInterceptor(userInterceptor).addPathPatterns("/**");
}
}
三、最后访问controller
@RestController
public class TestController {
@RequestMapping("/test")
public String test() {
return "test";
}
}
————————————————
版权声明:本文为CSDN博主「小贼驴」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38362455/article/details/85029748