在Struts應(yīng)用中,我們發(fā)出的請(qǐng)求都會(huì)經(jīng)過 相應(yīng)的攔截器進(jìn)行相關(guān)處理,一般都會(huì)有一個(gè)用戶登錄攔截(Session失效攔截);一般請(qǐng)求的話,如果Session失效時(shí),我們會(huì)跳到登錄頁(yè)面,可是如果我們采用AJAX請(qǐng)求時(shí),將會(huì)返回登錄頁(yè)面的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)的登錄頁(yè)面
window.location.replace(PlanEap.getActionURI("login"));
}
}
});
也就是ajax發(fā)送請(qǐng)求時(shí)如果攔截返回一個(gè)表示就跳轉(zhuǎn),否則執(zhí)行正常操作。
您可能感興趣的文章:- Java Web實(shí)現(xiàn)session過期后自動(dòng)跳轉(zhuǎn)到登陸頁(yè)功能【基于過濾器】
- 詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁(yè)面
- php頁(yè)面跳轉(zhuǎn)session cookie丟失導(dǎo)致不能登錄等問題的解決方法
- webix+springmvc session超時(shí)跳轉(zhuǎn)登錄頁(yè)面
- jQuery ajax全局函數(shù)處理session過期后的ajax跳轉(zhuǎn)問題
- ajax提交session超時(shí)跳轉(zhuǎn)頁(yè)面使用全局的方法來處理
- Jsp中解決session過期跳轉(zhuǎn)到登陸頁(yè)面并跳出iframe框架的方法
- Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面的實(shí)例代碼
- Session過期后實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)登錄頁(yè)面