Ajax中Session失效跳转登录页面的示例分析

小编给大家分享一下Ajax中Session失效跳转登录页面的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联公司为企业级客户提高一站式互联网+设计服务,主要包括成都网站设计、网站建设重庆App定制开发、微信平台小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。 

在Struts应用中,我们发出的请求都会经过 相应的拦截器进行相关处理,一般都会有一个用户登录拦截(Session失效拦截);一般请求的话,如果Session失效时,我们会跳到登录页面,可是如果我们采用AJAX请求时,将会返回登录页面的HTML代码,这肯定不是我们想要的,那么我们如何解决呢?请看以下步骤:

一、建立拦截器

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);
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
String 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 PROCESS
response.setHeader("sessionstatus", ConstantsKey.MSG_TIME_OUT);
result = null;
} else {// NORMAL REQUEST PROCESS
result = 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失效跳转登录页面的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

当前标题:Ajax中Session失效跳转登录页面的示例分析
URL链接:https://www.cdcxhl.com/article44/pppdhe.html

成都网站建设公司_创新互联,为您提供网站策划自适应网站网站排名ChatGPT网站维护网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司