分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

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

发布时间:2023-09-06 01:45责任编辑:董明明关键词:暂无标签

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 ????????????????????//系统的根url29 ????????????????????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 ????????else6 ????????????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.定时查询可能导致系统变慢

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

原文地址:https://www.cnblogs.com/zhanghongjie/p/8548371.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved