一、概念
拦截器(Interceptor)是springMVC框架中提供一种类似于过滤器功能的组件,主要作用是拦截用户的请求,是spring AOP的体现,应用场景主要有用户登录、权限验证、或者记录请求日志等在请求到达contrroller之前对请求进行处理的方式。springMVC中的拦截器是可插拔式设计,如果需要拦截器只需要在配置文件中配置该拦截器。不需要则去掉拦截器的配置,对springMVC框架不会有任何影响。在springMVC中拦截器是链接调用,即所有拦截器会组成一条拦截器链,请求会按照顺序依次经过每一个拦截器
二、拦截器的使用
(1) 实现HandlerInterceptor接口
public class LoggerPrintHandlerInterceptor1 implements HandlerInterceptor{
private static Logger logger = LoggerFactory.getLogger(LogPrintHandlerInterceptor.class);
/**
* 请求进入拦截器,进入一个拦截器最先执行该方法,该方法返回一个boolean值,当值为true,拦截器对请求放行,
* 请求进入下一个拦截器或者controller中值为false时,则表示该请求结束,不会再向下执行,因此可以在该方法
* 中对请求做预处理操作
* 参数:request 请求
* response 响应
* handler 该请求访问的资源路径
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.info("拦截方法: [" + handler.toString() + "]");
return true;
}
/**
* postHandle方法执行的前提是preHandle方法的返回值为true,该方法的执行时间介于
* 在controller层方法执行完成后,DispatcherServlet进行视图渲染之前,可以获取到
* modelAndView对象,因此可以对modelAndView对象进行操作
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("方法: [" + handler.toString() + "] 执行完成");
}
/**
* afterCompletion在整个请求完成之后执行,主要作用是进行资源清理,前提也是preHandle方法的返回值为true
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("请求执行完成");
}}
(2) 继承HandlerInterceptorAdapter类
public class LoggerPrintHandlerInterceptor2 extends HandlerInterceptorAdapter{
public LoggerPrintHandlerInterceptor2() {
super();
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
}
/**
* HandlerInterceptorAdapter类实现了AsyncHandlerInterceptor接口,与HandlerInterceptor接口不同的是增加一个afterConcurrentHandlingStarted方法,
* 该做方法与postHandler方法作用相似,但是不同的是postHandler是在controller方法完成后执行,afterConcurrentHandlingStarted是在controller方法执行的同时执行
*/
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
super.afterConcurrentHandlingStarted(request, response, handler);
}}
三、拦截器配置
XML配置:
<mvc:interceptors>
<bean class="com.fan.moxiangbooks.config.interceptor.LoggerPrintHandlerInterceptor2"/>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/web/**"/>
<bean class="com.fan.moxiangbooks.config.interceptor.LoggerPrintHandlerInterceptor1"/>
</mvc:interceptor></mvc:interceptors>
配置类配置:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new oggerPrintHandlerInterceptor1()).addPathPatterns("/**").excludePathPatterns("/web/**");
registry.addInterceptor(new LoggerPrintHandlerInterceptor2()).addPathPatterns("/admin/**");
}}