一、建立攔截器
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í)行正常操作。
更多Ajax Session失效跳轉(zhuǎn)登錄頁(yè)面的方法相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com