ajax访问遇到Session失效问题
最近由于一個(gè)項(xiàng)目,模塊切換為ajax請(qǐng)求數(shù)據(jù),當(dāng)Session失效后,ajax請(qǐng)求后沒(méi)有返回值,只有響應(yīng)的html:<html><script type='text/javascript'>window.open('http://192.168.0.118:8080/welcomeAction/loginUI.do','_top');</script></html>
現(xiàn)在Ajax在Web項(xiàng)目中應(yīng)用廣泛,幾乎可以說(shuō)無(wú)處不在,這就帶來(lái)另外一個(gè)問(wèn)題:當(dāng)Ajax請(qǐng)求遇到Session超時(shí),應(yīng)該怎么辦?
顯而易見(jiàn),傳統(tǒng)的頁(yè)面跳轉(zhuǎn)在此已經(jīng)不適用,因?yàn)锳jax請(qǐng)求是XMLHTTPRequest對(duì)象發(fā)起的而不是瀏覽器,在驗(yàn)證失敗后的頁(yè)面跳轉(zhuǎn)無(wú)法反應(yīng)到瀏覽器中,因?yàn)榉?wù)器返回(或輸出)的信息被JavaScript(XMLHTTPRequest對(duì)象)接到了。
那么應(yīng)該怎么處理這種情況呢?
方法
?
既然服務(wù)器返回的消息被XMLHTTPRequest對(duì)象接收,而XMLHTTPRequest對(duì)象又是在JavaScript的掌控之中,那么我們是否可以利用JavaScript來(lái)完成頁(yè)面跳轉(zhuǎn)呢?
當(dāng)然可以,而且很容易實(shí)現(xiàn)!但有一點(diǎn),我們需要判斷一下HTTP請(qǐng)求是否為Ajax請(qǐng)求(因?yàn)锳JAX請(qǐng)求和普通的請(qǐng)求需要分開(kāi)處理),這又如何判斷呢?其實(shí)Ajax請(qǐng)求和普通的HTTP請(qǐng)求是不同的,這體現(xiàn)在HTTP請(qǐng)求的頭信息中,如下所示:
上面兩張圖片是用火狐的Firebug截取的,前者是普通的HTTP請(qǐng)求頭信息;后者為Ajax請(qǐng)求的請(qǐng)求頭信息。注意第一圖片被紅框圈起來(lái)的部分,這就是Ajax請(qǐng)求與普通請(qǐng)求不同的地方,AJAX請(qǐng)求頭中帶有X-Requested-With信息,其值為XMLHttpRequest,這正是我們可以利用的地方。
下面看一下代碼如何實(shí)現(xiàn)。
Interceptor過(guò)濾器
? ?在使用Struts2時(shí),我們一般使用Interceptor(攔截器)來(lái)攔截權(quán)限問(wèn)題。
攔截器部分代碼:
1 public String intercept(ActionInvocation invocation) throws Exception { 2 // TODO Auto-generated method stub 3 ActionContext ac = invocation.getInvocationContext(); 4 HttpServletRequest request = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST); 5 String requestType = request.getHeader("X-Requested-With"); 6 System.out.println("+++++++++++++++++++++++reqestType:"+requestType); 7 HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE); 8 // String basePath = request.getContextPath(); 9 String path = request.getContextPath(); 10 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; 11 //獲取session 12 Map session = ac.getSession(); 13 //判斷session是否存在及session中的user信息是否存在,如果存在不用攔截 14 if(session != null && session.get(Constants.FE_SESSION_BG_USER) != null && session.get(Constants.FE_SESSION_BG_AUTH) != null){ 15 System.out.println(invocation.getProxy().getActionName()+"++++++++++++++++++++++++"); 16 System.out.println("namespace:"+invocation.getProxy().getNamespace()); 17 //訪問(wèn)路徑 18 String visitURL = invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName() + Constants.FE_STRUTS_ACTION_EXTENSION; 19 visitURL = visitURL.substring(1); 20 Map<String , Object> authMap = (Map<String, Object>) session.get(Constants.FE_SESSION_BG_AUTH); 21 Map<Integer, String> actionMap = (Map<Integer, String>) authMap.get(Constants.FE_BG_ACTIONMAP); 22 if(actionMap != null && !actionMap.isEmpty() && visitURL != null){ 23 if (actionMap.containsValue(visitURL)) { 24 System.out.println(visitURL+"-----------------------"); 25 return invocation.invoke(); 26 } else{ 27 String forbidden = basePath + Constants.FE_BG_FORBIDDEN; 28 response.sendRedirect(forbidden); 29 return null; 30 } 31 } 32 return invocation.invoke(); 33 }else{ 34 if(StringUtils.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")){ 35 response.setHeader("sessionstatus", "timeout"); 36 response.sendError(518, "session timeout."); 37 return null; 38 }else { 39 40 String actionName = invocation.getProxy().getActionName(); 41 System.out.println(actionName); 42 //如果攔截的actionName是loginUI或login,則不做處理,否則重定向到登錄頁(yè)面 43 if (StringUtils.isNotBlank(actionName) && actionName.equals(Constants.FE_BG_LOGINUI)) { 44 return invocation.invoke(); 45 }else if(StringUtils.isNotBlank(actionName) && actionName.equals(Constants.FE_BG_LOGIN)){ 46 return invocation.invoke(); 47 }else{ 48 String login = basePath + "/" + Constants.FE_BG_LOGIN_NAMESPACE + "/" + Constants.FE_BG_LOGINUI + Constants.FE_STRUTS_ACTION_EXTENSION; 49 // System.out.println("+++++++++++++++++++++++++++basePath:"+basePath); 50 // response.sendRedirect(login); 51 PrintWriter out = response.getWriter(); 52 // out.println("<html>"); 53 // out.println("<script>"); 54 // out.println("window.open ('"+login+"','_top');"); 55 // out.println("</script>"); 56 // out.println("</html>"); 57 out.write("<html><script type='text/javascript'>window.open('"+login+"','_top');</script></html>"); 58 return null; 59 } 60 } 61 } 62 63 } View Code由上面代碼可以看出,當(dāng)Session驗(yàn)證失敗(即Session超時(shí))后,我們通過(guò)HttpServletRequest取得請(qǐng)求頭信息X-Requested-With的值,如果不為空且等于XMLHttpRequest,那么就說(shuō)明此次請(qǐng)求是Ajax請(qǐng)求,我們作出的反應(yīng)就是向響應(yīng)中添加一條頭信息(自定義)并且使響應(yīng)對(duì)象HttpServletResponse返回服務(wù)器錯(cuò)誤信息(518狀態(tài)是自己隨便定義的);這些信息都會(huì)被JavaScript接收,那么下面的工作就要將由JavaScript代碼了。
Javascript代碼
?
$.ajaxSetup方法是來(lái)設(shè)置AJAX請(qǐng)求默認(rèn)選項(xiàng)的,我們可以認(rèn)為是全局的選項(xiàng)設(shè)置,因此可以將這段代碼提到外部JS文件中,在需要的頁(yè)面引用。
?
1 /** 2 * 設(shè)置未來(lái)(全局)的AJAX請(qǐng)求默認(rèn)選項(xiàng) 3 * 主要設(shè)置了AJAX請(qǐng)求遇到Session過(guò)期的情況 4 */ 5 $.ajaxSetup({ 6 type: 'POST', 7 complete: function(xhr,status) { 8 var sessionStatus = xhr.getResponseHeader('sessionstatus'); 9 if(sessionStatus == 'timeout') { 10 var top = getTopWinow(); 11 var yes = confirm('由于您長(zhǎng)時(shí)間沒(méi)有操作, session已過(guò)期, 請(qǐng)重新登錄.'); 12 if (yes) { 13 top.location.href = '/skynk/index.html'; 14 } 15 } 16 } 17 }); 18 19 /** 20 * 在頁(yè)面中任何嵌套層次的窗口中獲取頂層窗口 21 * @return 當(dāng)前頁(yè)面的頂層窗口對(duì)象 22 */ 23 function getTopWinow(){ 24 var p = window; 25 while(p != p.parent){ 26 p = p.parent; 27 } 28 return p; 29 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/qixing/p/3679991.html
總結(jié)
以上是生活随笔為你收集整理的ajax访问遇到Session失效问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Node.js开发之Express框架安
- 下一篇: 我的QT5学习之路(目录)