所有的拦截器都需要继承HandlerInterceptor接口,该接口源码如下
public interface HandlerInterceptor {
/**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 一般在此处进行登录信息拦截,大致写法如下
/**
* try {
* //统一拦截(查询当前session是否存在user)(这里user会在每次登陆成功后,写入session)
* User user=(User)request.getSession().getAttribute("USER");
* if(user!=null){
* return true;
* }
* response.sendRedirect(request.getContextPath()+"你的登陆页地址");
* } catch (IOException e) {
* e.printStackTrace();
* }
* return false;
*/
return true;//如果设置为false时,被请求时,拦截器执行到此处将不会继续操作
//如果设置为true时,请求将会继续执行后面的操作
}
// 处理器处理后的方法
/**
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
/**
* 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
*/
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
但是由于现在前后端分离,多数情况下在前端渲染
我们创建一个Interceptor类 实现HandlerInterceptor接口,我们先做一个简单的拦截器
public class Interceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("处理器前的方法, 执行preHandle");
// 返回true, 不会拦截后续的处理
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("处理器调用之后调用,执行postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("整个请求结束之后被调用,执行afterCompletion");
}
}
我们再创建一个HandlerConfig类,继承WebMvcConfigurer,并重写addInterceptors方法
@Configuration
public class HandlerConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册拦截器
InterceptorRegistration ir = registry.addInterceptor(new MyInterceptor());
// 指定拦截器的匹配模式
ir.addPathPatterns("/interceptor/*");
// ir.addPathPatterns("/**"); //所有路径都被拦截
// ir.excludePathPatterns( // 添加不拦截路径
// "你的登陆路径", // 登录
// "/**/*.html", //html静态资源
// "/**/*.js", //js静态资源
// "/**/*.css", //css静态资源
// "/**/*.woff",
// "/**/*.ttf"
*/ );
}
}
按照上文的代码,我的控制器只会拦截/interceptor/
开头的请求
执行 localhost:8080/interceptor/test 请求后结果如下
符合我们的预期
先注册的拦截器,是在最外层,preHandle 的触发顺序从外至内,postHandle、afterCompletion 的触发是从内到外。
下面展示一下:
public class MyInterceptorTwo implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("2.处理器前的方法, 执行preHandle");
// 返回true, 不会拦截后续的处理
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("2.处理器调用之后调用,执行postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("2.整个请求结束之后被调用(或者说视图层渲染后调用),执行afterCompletion");
}
}
public class MyInterceptorThree implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("3.处理器前的方法, 执行preHandle");
// 返回true, 不会拦截后续的处理
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("3.处理器调用之后调用,执行postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("3.整个请求结束之后被调用(或者说视图层渲染后调用),执行afterCompletion");
}
}
@Configuration
public class HandlerConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册拦截器
InterceptorRegistration ir = registry.addInterceptor(new MyInterceptor());
// 指定拦截器的匹配模式
ir.addPathPatterns("/interceptor/*");
// 注册拦截器2
InterceptorRegistration ir2 = registry.addInterceptor(new MyInterceptorTwo());
ir2.addPathPatterns("/interceptor/*");
// 注册拦截器3
InterceptorRegistration ir3 = registry.addInterceptor(new MyInterceptorThree());
ir3.addPathPatterns("/interceptor/*");
}
}
执行 localhost:8080/interceptor/test 请求后结果如下
正如我们所说的,先注册的拦截器,是在最外层,preHandle 的触发顺序从外至内,postHandle、afterCompletion 的触发是从内到外。