转载自 https://blog.csdn.net/weixin_36832313/article/details/79115693
简介
SpringMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于对请求进行拦截和处理。
常见应用场景
1、权限检查:如检测请求是否具有登录权限,如果没有直接返回到登陆页面。 2、性能监控:用请求处理前和请求处理后的时间差计算整个请求响应完成所消耗的时间。 3、日志记录:可以记录请求信息的日志,以便进行信息监控、信息统计等。
使用方法
1、配置文件
代码语言:html复制<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/lib/**" />
<beans:bean class="com.sunp.common.interceptor.Myinterceptor"/>
mvc:interceptor>
mvc:interceptors>
2、实现接口并继承方法(可以同时包含多个实现类)
代码语言:java复制public class myInterceptor implements HandlerInterceptor{
@Override
public boolean preHandler(HttpServletRequest request, HttpServletResponse response, Object obj)throws Exception{
/*该方法将在请求处理之前进行调用,只有该方法返回true,才会继续执行后续的Interceptor和Controller,当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法,如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法;*/
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,Object obj,Exception e)throws Exception {
/*该方法将在请求处理之后,DispatcherServlet进行视图返回渲染之前进行调用,可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。*/
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,Object arg2,ModelAndView arg3) throws Exception {
/*该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行,该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理。*/
}
}
执行顺序
1、单个实现类的执行顺序
preHandler -> Controller -> postHandler -> model渲染-> afterCompletion
2、多个实现类的执行顺序
———————preHandler1——————- ———————preHandler2——————- ———————preHandler3——————- ———————–Controller——————— ———————postHandler3—————— ———————postHandler2—————— ———————postHandler1—————— ———————postHandler1—————— ——————afterCompletion3—————- ——————afterCompletion2—————- ——————afterCompletion1—————-
简单练习代码
http://download.csdn.net/download/weixin_36832313/10215829
---------------------------------------------------------------------------------
package com.ibank.admin.ws.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.ibank.admin.service.AdminRedisService;
import com.tasfe.framework.redis.RedisService;
import com.tasfe.framework.support.model.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* Created by Dengw on 2018/1/4
*/
@Component
public class LoginInteceptor implements HandlerInterceptor{
private static Logger logger = LoggerFactory.getLogger(LoginInteceptor.class);
@Autowired
private AdminRedisService adminRedisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String accountNo = request.getHeader("accountNo");
String token = request.getHeader("token");
boolean judgeFlag = adminRedisService.judgeTimeout(accountNo,token);
if(!judgeFlag){
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
ResponseData responseData = ResponseData.error("登录超时,请重新登录!");
responseData.setStatus("2");
PrintWriter printWriter = response.getWriter();
printWriter.write(JSONObject.toJSONString(responseData));
printWriter.flush();
printWriter.close();
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}