When doing web development, when the session expires, if it is not an AJAX request, it is easy to jump to the specified page. But there are problems with Ajax requests. When the session expires, clicking on the AJAX request will pop up some page source files.
First, an interceptor was built to determine the session timeout. User Login will save user information in a session, in the session of the listener, session timeout will destroy the user information stored in the session, and the interceptor through the session whether there is user information to determine the session timeout.
interceptor is the SPRING-MVC interceptor, which in the interceptor determines whether it is an AJAX request:
public class logininterceptor extends handlerinterceptoradapter{public boolean Prehandle (httpservletrequest request, httpservletresponse response, object handler) throws Exception { object obj = request.getsession (). getattribute (Securityconstants.login_user); if (null == obj) { //not logged in if (Request.getheader ("X-requested-with") != null && Request.getheader ("X-requested-with"). Equalsignorecase ("XMLHttpRequest")) { //If an AJAX request response header will be available, x-requested-with Response.setheader ("Sessionstatus", "timeout");//In response header setting sEssion Status }else{ response.sendredirect (Request.getcontextpath () + "/account/login"); } return false; } return Super.prehandle (Request, response, handler);}}
Thus, if the session times out and is an AJAX request, it will be in the response header, Sessionstatus has a timeout;
Then use a global method to process the session timeout to jump to the page.
jquery can use the $.ajaxsetup method, ext also has a similar method
$.ajaxsetup ({ contenttype: " Application/x-www-form-urlencoded;charset=utf-8 ", Complete:function (xmlhttprequest,textstatus) { var sessionstatus=xmlhttprequest.getresponseheader ("Sessionstatus"), //obtains the response head through XMLHttpRequest, Sessionstatus, if (sessionstatus== "Timeout") { alert ("Login timeout, please login again!") //If the   is processed when timed out, specify the page to jump to window.location.replace (webPath.webRoot + " Account/login "); } } });
Spring MVC session timeout, processing AJAX requests