当前端在用ajax请求时,若是没有设置session超时时间而且作跳转到登陆界面的处理,那么只是靠后台是很难完成超时的一系列动做的;可是若是后台前端
没有封装一个ajax请求公共类,那么在ajax请求上下功夫解决session超时的问题是不行的,只有考虑在后台或者前台经过全局来进行对ajax请求超时的jquery
处理了。ajax
本人用的是spring security来处理的,想只经过后台来进行处理,可是尝试了不少种办法,可是一直没有成功,session一超时,前台页面就一直显示遮spring
罩层,只有在刷新后才能正常操做。最终仍是考虑在前台上下功夫,经过使用jquery的全局事件,就搞定了。session
1.在spring-security.xml配置session超时时触发的方法(配置在<security:http>标签内)app
<security:http>函数
<security:session-management invalid-session-url="/timeout"></security:session-management>
url
</security:http>spa
2.超时处理方法代码.net
@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() 方法规定的函数会在请求完成时运行,即便请求并未成功