首页 > 开发 > AJAX > 正文

Ajax Session失效如何跳转登录页面

2024-09-01 08:30:34
字体:
来源:转载
供稿:网友

我们在Struts应用中经常会经过相应的拦截器进行相关处理,一般请求的时候Session失效时就会跳到登录页面,那么你知道Ajax Session失效如何跳转登录页面吗?

一、建立拦截器

package com.xxx.planeap.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.xxx.common.contants.ConstantsKey;import com.xxx.common.contants.SessionKey;import com.xxx.planeap.domain.User;import com.xxx.planeap.security.SecurityContextUtil;/*** * @author Goma OMA1989@YEAH.NET* @version v1.0* @since 2012-05-31* */public class SecurityInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L;private Logger logger = Logger.getLogger(SecurityInterceptor.class);@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubString className = invocation.getAction().getClass().getName();String action = className.substring(className.lastIndexOf(".")+1,className.length());String actionName = invocation.getProxy().getActionName();String result;HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();String type = request.getHeader("X-Requested-With");User user = (User) ActionContext.getContext().getSession().get(SessionKey.CURRENT_USER);if (user == null) {logger.debug("SECURITY CHECKED: NEED TO LOGIN");if ("XMLHttpRequest".equalsIgnoreCase(type)) {// AJAX REQUEST PROCESSresponse.setHeader("sessionstatus", ConstantsKey.MSG_TIME_OUT);result = null;} else {// NORMAL REQUEST PROCESSresult = ActionSupport.LOGIN;}} else {logger.debug("SECURITY CHECKED: USER HAS LOGINED");SecurityContextUtil.setCurrentUser(user);boolean hanPerm = SecurityContextUtil.hasPerm(action, actionName);logger.debug("SECURITY CHECKED: PERMISSION---"+action+"."+actionName+"="+hanPerm);result = invocation.invoke();}return result;}}

二、定义全局AJAX请求结束处理方法

//全局的AJAX访问,处理AJAX清求时SESSION超时$.ajaxSetup({contentType:"application/x-www-form-urlencoded;charset=utf-8",complete:function(XMLHttpRequest,textStatus){//通过XMLHttpRequest取得响应头,sessionstatus var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); if(sessionstatus=="timeout"){//这里怎么处理在你,这里跳转的登录页面window.location.replace(PlanEap.getActionURI("login"));}}});

也就是ajax发送请求时如果拦截返回一个表示就跳转,否则执行正常操作。

以上就是小编介绍Ajax Session失效如何跳转登录页面的内容,需要的朋友赶紧动手尝试一下吧,这在我们日后使用计算机的过程中会带来不少方便。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表