• spring security:ajax请求的session超时处理


    原文:https://www.cnblogs.com/yanduanduan/p/6252203.html

    当前端在用ajax请求时,如果没有设置session超时时间并且做跳转到登录界面的处理,那么只是靠后台是很难完成超时的一系列动作的;但是如果后台

    没有封装一个ajax请求公共类,那么在ajax请求上下功夫解决session超时的问题是不行的,只有考虑在后台或者前台通过全局来进行对ajax请求超时的

    处理了。

     本人用的是spring security来处理的,想只通过后台来进行处理,但是尝试了很多种办法,但是一直没有成功,session一超时,前台页面就一直显示遮

    罩层,只有在刷新后才能正常操作。最终还是考虑在前台上下功夫,通过使用jquery的全局事件,就搞定了。

    1.在spring-security.xml配置session超时时触发的方法(配置在<security:http>标签内)

    <security:http>

    <security:session-management invalid-session-url="/timeout"></security:session-management>

    </security:http>

    2.超时处理方法代码

    @RequestMapping(value = "/timeout")
        public void sessionTimeout(HttpServletRequest request,HttpServletResponse response) throws IOException {  
            if (request.getHeader("x-requested-with") != null  
                    && request.getHeader("x-requested-with").equalsIgnoreCase(  
                            "XMLHttpRequest")) { // ajax 超时处理  
                response.getWriter().print("timeout");  //设置超时标识
                response.getWriter().close();
            } else {
                 response.sendRedirect("/login");  
            }
        }  

    3.前台监听超时方法

    $(document).ajaxComplete(function(event,obj,settings){
            if (obj.responseText == 'timeout') { //超时标识
                location.href='/login'; //跳转到登录页面
            }
        })

    这里使用jquery的全局事件,通过 ajaxComplete() 方法规定的函数会在请求完成时运行,即使请求并未成功

    或者至二级修改全局ajax的Complete方法

    $(document).ajaxComplete(function(event,obj,settings){
    alert(obj.status);
    })

  • 相关阅读:
    [LeetCode] 210. Course Schedule II
    [LeetCode] 207. Course Schedule
    [LeetCode] 450. Delete Node in a BST
    [LeetCode] 1122. Relative Sort Array
    [LeetCode] 1013. Partition Array Into Three Parts With Equal Sum
    [LeetCode] 173. Binary Search Tree Iterator
    [LeetCode] 208. Implement Trie (Prefix Tree)
    [LeetCode] 211. Add and Search Word
    [LeetCode] 449. Serialize and Deserialize BST
    [LeetCode] 236. Lowest Common Ancestor of a Binary Tree
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9406013.html
Copyright © 2020-2023  润新知