目录
一、springboot中如何配置拦截器
1.定义一个类并实现HandlerInterceptor接口,重写该接口内的三个方法
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("perHandle...");
String method = request.getRequestURI();
System.out.println(method);
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");
}
}
2.实现WebMvcConfigurer并注册你的拦截器
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/templates/");
}
}
注意我们这里放行了/templates/下资源的请求 ,在时间开发中,我们需要将静态资源和登录接口及根路径都给放行,通常配置为
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**");
3.可以使用thymeleaf观察下,引入thymeleaf依赖,
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在resources/templates下创建.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>
二、配置多个拦截器时的顺序问题及拦截器内方法执行问题
1.配置多个拦截器时的顺序问题
创建2个拦截器注册
public class MyInterceptor01 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("perHandle01...");
String method = request.getRequestURI();
System.out.println(method);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle01...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion01...");
}
}
public class MyInterceptor02 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("perHandle02...");
String method = request.getRequestURI();
System.out.println(method);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle02...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion02...");
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor01()).addPathPatterns("/**").excludePathPatterns("/templates/**");
registry.addInterceptor(new MyInterceptor02()).addPathPatterns("/**").excludePathPatterns("/templates/**");
}
}
发送请求,观察顺序:
perHandle01...
/test
perHandle02...
/test
testController...
postHandle02...
postHandle01...
afterCompletion02...
afterCompletion01...
首先,拦截器的先后顺序在不添加其他配置时默认为拦截器的注册顺序,这里MyInterceptor01先于MyInterceptor02执行拦截,但是拦截器内的方法却不同,首先,方法perHandle执行顺序与拦截器的顺序相同,而postHandle与 afterCompletion的执行顺序与拦截器顺序相反.
2.拦截器内方法执行时机
当请求进入dispatchservlet后,(一般来说就是没有被Filter拦截
),拦截器内的perHandle()一定会执行,这时分为三种情况:
1.所有拦截器内的perHandle()都返回true;
2.当有一个拦截器内的perHandle()返回false;
3.所有拦截器内的perHandle()都返回false;
第一种情况下,拦截器放行,接下来执行controller方法,controller方法返回结果后且视图渲染前执行postHandle()方法,此时可以对返回值做处理,当请求完成后,即dispatchservlet处理完请求后,执行afterCompletion()方法,这里可以简单理解为,只要afterCompletion()方法所属的拦截器内的perHandle()返回true,那么afterCompletion()方法一定执行,执行时间为整个请求完成后.
第二种情况下,当有一个拦截器中的perHandle()返回false,其他都返回true,首先,该拦截器之前的所有拦截器的perHandle()都会执行,该拦截器之后的所有拦截器的perHandle()都不会执行;该拦截器之前的所有拦截器的afterCompletion()都会执行,该拦截器之后的所有拦截器的afterCompletion()都不会执行;所有拦截器内的postHandle()都不会执行且所有的controller也不会执行.
第三种情况,除了第一个拦截器中的perHandle()执行外,其他所有的controller,postHandle(),afterCompletion()都不会执行.