package com.chb.abc.common.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.chb.abc.common.util.UserActiveNoticeUtil;
import com.chb.abc.model.User;
import com.chb.abc.service.UserService;
public class CommonInterceptor implements HandlerInterceptor {
private Logger log = Logger.getLogger(CommonInterceptor.class);
public CommonInterceptor() {
}
//private String mappingURL;//利用正则映射到需要拦截的路径
// public void setMappingURL(String mappingURL) {
// this.mappingURL = mappingURL;
// }
@Autowired
private UserService userService;
/**
* 在业务处理器处理请求之前被调用
* 如果返回false
* 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
*
* 如果返回true
* 执行下一个拦截器,直到所有的拦截器都执行完毕
* 再执行被拦截的Controller
* 然后进入拦截器链,
* 从最后一个拦截器往回执行所有的postHandle()
* 接着再从最后一个拦截器往回执行所有的afterCompletion()
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generated method stub
log.info("==============执行顺序: 1、preHandle================");
Boolean activeFlag = true;//统一认证平台用户报活标志,true为无需报活或者报活成功,false为报活失败
HttpSession session = request.getSession();
String callbackURL = request.getServletPath();
//后台管理中心的拦截器
if(callbackURL.startsWith("/manage/"))
{
//1. 得到session中的sessionID
//2. 如果为空,到登录界面
String szUserId=(String)session.getAttribute("administerUserId");
Object lastActiveTimeObject=session.getAttribute("lastactivetime");
if(szUserId==null || lastActiveTimeObject==null)
{
session.invalidate();
request.getRequestDispatcher("/manage/login.html").forward(
request, response);
return false;
}
//3. 如果不为空,但登录时间过长,重新登录
long lastActiveTime = (Long)lastActiveTimeObject ;// 获取usessionid的最后一次报活时间戳
// 判断报活时间间隔是否超过25分钟
if ((System.currentTimeMillis() - lastActiveTime) > 25 * 60 * 1000) {
session.invalidate();
request.getRequestDispatcher("/manage/login.html").forward(
request, response);
return false;
}
else {
//4. 如果不为空,且登录时间小于25分钟,允许通过
session.setAttribute("lastactivetime",
System.currentTimeMillis());
return true;
}
}
else if(callbackURL.startsWith("/app/")){
log.info("app端请求");//TODO zhoujianbin 后续优化
}
else {
if (session.getAttribute("cmusessionid") != null
&& session.getAttribute("lastactivetime") != null) {
String usessionid = (String) session
.getAttribute("cmusessionid");// 获取usessionid
long lastActiveTime = (Long) session
.getAttribute("lastactivetime");// 获取usessionid的最后一次报活时间戳
// 判断报活时间间隔是否超过25分钟
if ((System.currentTimeMillis() - lastActiveTime) > 25 * 60 * 1000) {
if (UserActiveNoticeUtil.userActive(usessionid)) {// 判断报活是否成功
session.setAttribute("lastactivetime",
System.currentTimeMillis());
} else {
activeFlag = false;
}
}
}
Long userid = (Long) session.getAttribute("userid");
// 若在session中没有找到用户信息则把用户信息放入session,取名session_user
if (userid != null && session.getAttribute("session_user") == null) {
User user = new User();
user.setUserid(userid);
user = userService.getUserInfo(user);
session.setAttribute("session_user", user);
}
if (session.getAttribute("userid") == null || !activeFlag) {// 未登录过或者报活失败,则需要重新登录
session.invalidate();
request.setAttribute("callbackURL", callbackURL);
request.getRequestDispatcher("/user/login.html").forward(
request, response);
return false;
}
}
return true;
}
//在业务处理器处理请求执行完成后,生成视图之前执行的动作
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
log.info("==============执行顺序: 2、postHandle================");
}
/**
* 在DispatcherServlet完全处理完请求后被调用
*
* 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
log.info("==============执行顺序: 3、afterCompletion================");
}
}
通用拦截器
最新推荐文章于 2023-10-04 17:36:49 发布