Ajax通讯时,检查Session是否过期
实现:这里用到一个拦截器,检查Session中的用户是否存在
<span style="font-size:18px;">public class LoginFilter extends AbstractInterceptor{ private static final long serialVersionUID = 1L; private Logger logger =Logger.getLogger(LoginFilter.class.getName()); private static String Error = "errorLogin"; private String msg ; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String intercept(ActionInvocation arg0) throws Exception { logger.debug("...LoginFilter开始载拦..."); //取得请求的action实例 ActionContext ctx = arg0.getInvocationContext(); //取得提交URI HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE); String actionName = ctx.getContext().getName(); if("goAIOPortal".equals(actionName)||"goRealware".equals(actionName)||"loginCheck".equals(actionName)||"loginCh".equals(actionName)||"ukeyloginCheck".equals(actionName)||"loginCheckPass".equals(actionName)) { return arg0.invoke(); } //获取session logger.debug("...LoginFilter:判断用户登录..."); Map<String,Object> session =ctx.getSession(); //异步传输 if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { if (session.get("user") == null) { //if(!autoLogin(session,request)){ msg = "用户未登录或登录超时,请重新登录!"; logger.debug("用户未登录,重新登录!"); //request.setAttribute("msg", msg); response.setContentType("text/json;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter pw = null; try { pw = response.getWriter(); pw.write("overdue"); } catch (IOException e) { throw new EVoucherException("获取http写入流异常" + e.getMessage()); } finally { if (pw != null) { pw.close(); } } return null; //} }else{ return arg0.invoke(); } //同步传输 }else{ if (session.get("user") == null) { logger.debug("用户未登录,重新登录!"); return Action.LOGIN; }else{ return arg0.invoke(); } } } //自动登陆 private boolean autoLogin(Map<String,Object> session, HttpServletRequest request){ return false; } }</span>
然后在检测的时候判断执行以下方法就行了。
<span style="font-size:18px;">/** * ajax通讯模式下,客户端校验Session是否过期。 * @param msg */ function checkSessionOverdue(msg){ if (msg == 'overdue'){ alert("登录时间过长,将重新登录!!") window.top.location.href = '/realware/jsp/common/Login.jsp'; } } </span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。