HandlerInterceptor
是 SpringMVC
中的一个接口,在SpringMVC
应用中它提供了一种实现应用级拦截器
的机制。
第1步:引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
第2步:定义拦截器
import com.zhangziwa.practisesvr.utils.response.ResponseContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;@Component
public class ResponsePostInterceptor implements HandlerInterceptor {# 在进入Controller执行之前调用# 该方法返回 true,则继续执行后续的Controller# 该方法返回 false,则中断,不会执行后续的Controller 和 Interceptor# 验证行为:登录验证、权限控制# 记录行为:性能监控、日志记录等操作@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {# 记录行为:比如此处为记录一下日志System.err.println("***ResponsePostInterceptor.preHandle***");return true;}# controller执行之后,页面渲染之前调用# 数据补充行为:对请求处理的结果(如 ModelAndView)进行进一步的修改或操作@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {# 记录行为:比如此处为记录一下日志System.err.println("***ResponsePostInterceptor.postHandle***");# 数据补充行为:比如此处对HttpHeaders 和 HttpStatus 进行补充if (response.isCommitted()) {System.out.println("response is committed");return;}HttpHeaders headers = ResponseContext.getHeaders();if (headers != null) {headers.forEach((key, values) -> values.forEach((value) -> {if (!response.getHeaderNames().contains(key)) {response.addHeader(key, value);}}));}if (ResponseContext.getResponseCode() != null) {response.setStatus(ResponseContext.getResponseCode().value());}}# 页面渲染之后调用,无论是否发生异常都会被调用# 数据清理行为:通常用于资源清理# 记录行为:记录请求结束时间等@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {# 记录行为:比如此处为记录一下日志System.err.println("***ResponsePostInterceptor.afterCompletion***");# 数据清理行为:比如此处用于资源清理ResponseContext.clear();}
}
第3步:注册拦截器
注册后拦截器才正常使用,所以使用前要先注册,所谓注册就是放到指定地方,Spring就知道有这个拦截器了。
import com.zhangziwa.practisesvr.interceptor.ResponsePostInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Autowiredprivate ResponsePostInterceptor responsePostInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(responsePostInterceptor);}
}
第4步:使用
***ResponsePostInterceptor.preHandle***
***Controller.getAllStudents4***
***ResponsePostInterceptor.postHandle***
***ResponsePostInterceptor.afterCompletion***
参考
springboot之HandlerInterceptor拦截器入门
Spring MVC拦截器HandlerInterceptor全解!