简介
springmvc提供了拦截器,类似于过滤器,它将在我们请求处理之前先做检查,有权决定接下来是否继续执行,对我们的请求进行加工。拦截器可以设计多个。
实现
通过实现HandlerIntercepter,这是一个接口,定义了三个非常重要的方法
- 前置处理
- 后置处理
- 完成处理
案例一
拦截器实现方法耗时统计与警告
ackage com.cyk.interceptor;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 方法耗时统计的拦截器
*/
public class MethodTimerInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = Logger.getLogger(MethodTimerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//1.定义开始时间
long start = System.currentTimeMillis();
//2.将其存到请求域中
request.setAttribute("start",start);
//返回true才会去找下一个拦截器,如果没有下一个,则去找controller
LOGGER.info(request.getRequestURI() + "请求到达");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//1.取出start
long start = (long) request.getAttribute("start");
//2.得到end
long end = System.currentTimeMillis();
//3记录一下耗时
long spendTime = end - start;
if(spendTime >= 1000)
LOGGER.warn("方法耗时严重,请及时处理,耗时:" + spendTime + "毫秒");
else
LOGGER.info("方法耗时:" + spendTime + "毫秒,速度正常");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
log4j.properties(放在resources下)
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springmvc中拦截器的配置
<!-- 拦截器的配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<bean class="com.cyk.interceptor.MethodTimerInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
案例二
会话拦截器,做用户检查
后台
package com.cyk.interceptor;
import com.cyk.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 会话拦截器
*/
public class SessionInterceptor implements HandlerInterceptor {
private static final Logger LOGGER = Logger.getLogger(MethodTimerInterceptor.class);
//检查当前会话有没有User,如果有就放行,没有就不放
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("SESSION_USER");
if(user == null){
LOGGER.warn("您不具备权限,请先登录");
return false;
}
if(user instanceof User){
//再去数据库检查身份对不对,可能被冻结了
User u = (User) user;
u.setPwd(null);
request.getSession().setAttribute("SESSION_USER",u);
LOGGER.info(u.getName() + "处于登陆状态,可以执行操作。");
return true;
}
else{
LOGGER.warn("您不具备权限,请先登录");
return false;
}
}
}
springmvc
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**/*"/>
<!-- 排除登陆的这个URI-->
<mvc:exclude-mapping path="/user/login"/>
<bean class="com.cyk.interceptor.SessionInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
拦截器执行顺序:springmvc中,谁在前,先执行谁