Ajax中Session失效跳轉(zhuǎn)登錄頁面的示例分析

小編給大家分享一下Ajax中Session失效跳轉(zhuǎn)登錄頁面的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)公司為企業(yè)級(jí)客戶提高一站式互聯(lián)網(wǎng)+設(shè)計(jì)服務(wù),主要包括成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)重慶App定制開發(fā)、微信平臺(tái)小程序開發(fā)、宣傳片制作、LOGO設(shè)計(jì)等,幫助客戶快速提升營銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗(yàn)豐富的經(jīng)驗(yàn),可以確保每一個(gè)作品的質(zhì)量和創(chuàng)作周期,同時(shí)每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。 

在Struts應(yīng)用中,我們發(fā)出的請(qǐng)求都會(huì)經(jīng)過 相應(yīng)的攔截器進(jìn)行相關(guān)處理,一般都會(huì)有一個(gè)用戶登錄攔截(Session失效攔截);一般請(qǐng)求的話,如果Session失效時(shí),我們會(huì)跳到登錄頁面,可是如果我們采用AJAX請(qǐng)求時(shí),將會(huì)返回登錄頁面的HTML代碼,這肯定不是我們想要的,那么我們?nèi)绾谓鉀Q呢?請(qǐng)看以下步驟:

一、建立攔截器

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請(qǐng)求結(jié)束處理方法

//全局的AJAX訪問,處理AJAX清求時(shí)SESSION超時(shí)
$.ajaxSetup({
contentType:"application/x-www-form-urlencoded;charset=utf-8",
complete:function(XMLHttpRequest,textStatus){
//通過XMLHttpRequest取得響應(yīng)頭,sessionstatus 
var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); 
if(sessionstatus=="timeout"){
//這里怎么處理在你,這里跳轉(zhuǎn)的登錄頁面
window.location.replace(PlanEap.getActionURI("login"));
}
}
});

也就是ajax發(fā)送請(qǐng)求時(shí)如果攔截返回一個(gè)表示就跳轉(zhuǎn),否則執(zhí)行正常操作。

以上是“Ajax中Session失效跳轉(zhuǎn)登錄頁面的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前標(biāo)題:Ajax中Session失效跳轉(zhuǎn)登錄頁面的示例分析
URL鏈接:http://muchs.cn/article44/pppdhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、自適應(yīng)網(wǎng)站、網(wǎng)站排名、ChatGPT網(wǎng)站維護(hù)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司