session過時,攔截ajax請求並跳轉登陸頁面

1.方法一 :1.1使用filter 和ajaxsetup 對ajax進行攔截並跳轉登陸頁面ajax

 1 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  2             throws IOException, ServletException {  3 
 4         HttpServletResponse hresponse = (HttpServletResponse)response;  5         HttpServletRequest hrequest = (HttpServletRequest)request;  6         HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);  7 
 8         String logonStrings = config.getInitParameter("logonStrings");        // 登陸登錄頁面
 9         String redirectPath = hrequest.getContextPath() + config.getInitParameter("redirectPath");// 沒有登錄轉向頁面
10         String disabletestfilter = config.getInitParameter("disabletestfilter");// 過濾器是否有效
11         String reString = hrequest.getRequestURI(); 12         if (disabletestfilter.toUpperCase().equals("Y")) {    // 過濾無效
13  chain.doFilter(request, response); 14             return; 15  } 16 // User user = ( User ) hrequest.getSession().getAttribute("user");//判斷用戶是否登陸
17         String session_key = (String) hrequest.getSession().getAttribute("token"); 18         String username = (String) hrequest.getSession().getAttribute("username"); 19         if ("".equals(session_key) || session_key == null) { 20             String[] logonList = logonStrings.split(","); 21             if (this.isContains(reString, logonList)) {// 對登陸頁面不進行過濾
22  chain.doFilter(request, response); 23                 return; 24             }else{ 25                 request.setAttribute("username", null); 26                 boolean isAjaxRequest = this.isAjaxRequest(hrequest); 27                 if (isAjaxRequest) { 28                     //系統的根url
29  chain.doFilter(request, response); 30                     return; 31  } 32  wrapper.sendRedirect(redirectPath); 33                 return; 34  } 35         }else { 36             if(username !=null || username .equals("")) { 37                 Pattern pattern = Pattern.compile("/iad/"); 38                 Matcher matcher = pattern.matcher(reString); 39                 Pattern pattern1 = Pattern.compile("/iad/views/login/login.jsp"); 40                 Matcher matcher1 = pattern1.matcher(reString); 41                 if(matcher.matches() ||matcher1.matches()){ 42                     wrapper.sendRedirect("/iad/views/home/index.jsp"); 43                     return; 44                 }else { 45  chain.doFilter(request, response); 46                     return; 47  } 48             }else{ 49                 boolean isAjaxRequest = this.isAjaxRequest(hrequest); 50                 if (isAjaxRequest) { 51  chain.doFilter(request, response); 52                     return; 53  } 54                 Pattern pattern = Pattern.compile(".*\\/views\\/login\\/login\\.jsp"); 55                 Matcher matcher = pattern.matcher(reString); 56                 Pattern pattern2 = Pattern.compile(".*\\/oms/"); 57                 Matcher matcher2 = pattern2.matcher(reString); 58                 // 是不是登錄頁面
59                 if (matcher.matches() || matcher2.matches()) { 60                     request.setAttribute("username", username); 61                     wrapper.sendRedirect("/iad/views/home/index.jsp"); 62                     return; 63  } 64  chain.doFilter(request, response); 65                 return; 66  } 67 
68  } 69 
70     }

判斷是否爲ajaxjson

1  public static boolean isAjaxRequest(HttpServletRequest request) { 2         String header = request.getHeader("X-Requested-With"); 3         if (header != null && "XMLHttpRequest".equals(header)) 4             return true; 5         else
6             return false; 7     }

1.2  全局js代碼 能夠抽取公共js 引入到你想要的頁面session

 1 $.ajaxSetup( {  2         type: "POST" , // 默認使用POST方式
 3         headers: { // 默認添加請求頭
 4             "Author": "CodePlayer" ,  5             "Powered-By": "CodePlayer"
 6  } ,  7         error: function(xhr, textStatus, errorMsg){ // 出錯時默認的處理函數
 8           var sessionStatus = xhr.getResponseHeader('sessionstatus');  9         if(sessionStatus == 'timeout') { 10             var top = getTopWinow(); 11             var ids=layer.alert("導入成功!"); 12             window.location.href = "/iad/veiws/login/login.jsp"; 13  } 14 
15  } 16     } );

總結: 這種方法的有個問題,就是ajax裏面的error方法是必定走的, 提醒信息會出現兩次app

方法二:使用js定時器對前臺的session進行查詢(前提是你要將這個js放到公共頁面)jsp

 1  @ResponseBody  2     @RequestMapping("/loginCheck")  3     public Boolean loginCheck(HttpServletRequest request, HttpServletResponse response) {  4 
 5         String session_key = (String) request.getSession().getAttribute("token");  6         String username = (String) request.getSession().getAttribute("username");  7         if (session_key == null || username == null) {  8             response.setHeader("sessionstatus", "timeout");  9             return false; 10  } 11         return true; 12     }
var timeid=window.setInterval("checkSession()", 1000); checkSession= function () { $.ajax({ url: "../../loginCheck", type: "POST", dataType: "json", success: function (result) { if (result != true) { window.clearInterval(timeid); Ewin.alert({message: "因爲您長時間沒有操做, session已過時, 請從新登陸"}).on(function (e) { window.location.href = "/iad/veiws/login/login.jsp"; }) } } }); };

總結:這個方法能夠將定義時間延長能夠兩秒查一次,肯定是首先1.你的系統要有公共的頁面,2.定時查詢可能致使系統變慢函數

相關文章
相關標籤/搜索