最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ajax Session失效跳轉(zhuǎn)登錄頁面的方法

 更新時間:2016年06月03日 11:08:12   投稿:mrr  
這篇文章主要介紹了Ajax Session失效跳轉(zhuǎn)登錄頁面的方法,非常不錯具有參考借鑒價值,感興趣的朋友一起看下吧

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

一、建立攔截器

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

//全局的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"){
//這里怎么處理在你,這里跳轉(zhuǎn)的登錄頁面
window.location.replace(PlanEap.getActionURI("login"));
}
}
});

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

相關(guān)文章

最新評論

磐安县| 顺义区| 宜阳县| 昌图县| 临湘市| 聊城市| 嘉黎县| 和田市| 潮安县| 黄龙县| 武冈市| 兰西县| 墨江| 奉化市| 邮箱| 陵川县| 稻城县| 界首市| 昌宁县| 建昌县| 咸阳市| 古田县| 深水埗区| 辉南县| 九寨沟县| 昌乐县| 禄劝| 西贡区| 西安市| 永吉县| 茂名市| 芜湖县| 巫山县| 孟连| 天台县| 镇平县| 都匀市| 平乡县| 秭归县| 闻喜县| 闸北区|