session过期,拦截ajax请求并跳转登录页面

2018-06-18 02:50:47来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1.方法一 :1.1使用filter 和ajaxsetup 对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     }

判断是否为ajax

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 引入到你想要的页面

 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方法是一定走的, 提醒信息会出现两次

方法二:使用js定时器对前台的session进行查询(前提是你要将这个js放到公共页面)

 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.定时查询可能导致系统变慢

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:failed to install tomcat6 service ,check your setting and pe

下一篇:06_JavaSE之OOP--面向对象(final、抽象类、接口)