1. Method One: 1.1 Use filter and Ajaxsetup to intercept Ajax and jump to the login page
1 Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)2 throwsIOException, servletexception {3 4HttpServletResponse Hresponse =(httpservletresponse) response;5HttpServletRequest hrequest =(httpservletrequest) request;6Httpservletresponsewrapper wrapper =NewHttpservletresponsewrapper ((httpservletresponse) response);7 8String logonstrings = Config.getinitparameter ("logonstrings");//Login Landing Page9String Redirectpath = Hrequest.getcontextpath () + config.getinitparameter ("Redirectpath");//no landing turn pageTenString Disabletestfilter = Config.getinitparameter ("Disabletestfilter");//whether the filter is valid OneString restring =Hrequest.getrequesturi (); A if(Disabletestfilter.touppercase (). Equals ("Y")) {//Invalid filter - Chain.dofilter (request, response); - return; the } - //User user = (user) hrequest.getsession (). getattribute ("user");//determine if the user is logged on -String session_key = (string) hrequest.getsession (). getattribute ("token"); -String username = (string) hrequest.getsession (). getattribute ("username"); + if("". Equals (Session_key) | | Session_key = =NULL) { -string[] Logonlist = Logonstrings.split (","); + if( This. Iscontains (restring, logonlist)) {//do not filter the login page A Chain.dofilter (request, response); at return; -}Else{ -Request.setattribute ("username",NULL); - BooleanIsajaxrequest = This. Isajaxrequest (hrequest); - if(isajaxrequest) { - //root URL of the system in Chain.dofilter (request, response); - return; to } + Wrapper.sendredirect (redirectpath); - return; the } *}Else { $ if(Username! =NULL|| Username. Equals ("")) {Panax NotoginsengPattern pattern = pattern.compile ("/iad/"); -Matcher Matcher =Pattern.matcher (restring); thePattern pattern1 = Pattern.compile ("/iad/views/login/login.jsp"); +Matcher Matcher1 =Pattern1.matcher (restring); A if(Matcher.matches () | |matcher1.matches ()) { theWrapper.sendredirect ("/iad/views/home/index.jsp"); + return; -}Else { $ Chain.dofilter (request, response); $ return; - } -}Else{ the BooleanIsajaxrequest = This. Isajaxrequest (hrequest); - if(isajaxrequest) {Wuyi Chain.dofilter (request, response); the return; - } WuPattern pattern = Pattern.compile (". *\\/views\\/login\\/login\\.jsp")); -Matcher Matcher =Pattern.matcher (restring); AboutPattern pattern2 = Pattern.compile (". *\\/oms/")); $Matcher Matcher2 =Pattern2.matcher (restring); - //whether it is a landing page - if(Matcher.matches () | |matcher2.matches ()) { -Request.setattribute ("username", username); AWrapper.sendredirect ("/iad/views/home/index.jsp"); + return; the } - Chain.dofilter (request, response); $ return; the } the the } the -}
Determine if it is Ajax
1 Public Static Booleanisajaxrequest (HttpServletRequest request) {2String Header = Request.getheader ("X-requested-with");3 if(Header! =NULL&& "XMLHttpRequest". Equals (header))4 return true;5 Else6 return false;7}
1.2 Global JS code can extract the public JS into the page you want
1 $.ajaxsetup ({2Type: "POST",//Use the post mode by default3Headers: {//Add request header by default4"Author": "Codeplayer" ,5"Powered-by": "Codeplayer"6 } ,7Error:function (XHR, Textstatus, errormsg) {//Default handler function On Error8var sessionstatus = xhr.getresponseheader (' Sessionstatus ');9 if(Sessionstatus = = ' Timeout ') {Tenvar top =Gettopwinow (); Onevar ids=layer.alert ("Import succeeded! "); AWindow.location.href = "/iad/veiws/login/login.jsp"; - } - the } -} );
Summary: There is a problem with this method, that is, the error method in Ajax is bound to go, the reminder message will appear two times
Method Two: Use the JS timer to query the foreground session (if you want to put this JS on the public page)
1 @ResponseBody2@RequestMapping ("/logincheck")3 PublicBoolean Logincheck (httpservletrequest request, httpservletresponse response) {4 5String session_key = (string) request.getsession (). getattribute ("token");6String username = (string) request.getsession (). getattribute ("username");7 if(Session_key = =NULL|| Username = =NULL) {8Response.setheader ("Sessionstatus", "timeout");9 return false;Ten } One return true; A}
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:"The session has expired because you have not operated for a long time, please sign in again"}). On (function (e) {window.location.href= "/iad/veiws/login/login.jsp"; }) } } }); };
Summary: This method can be defined time extension can be two seconds to check, to determine the first 1. Your system will have a public page, 2. Scheduled queries can cause slow system
Session expires, block Ajax requests and jump to login page