Generally, we will judge the logon status in the filter. If we do not log on, we will jump to the logon page. The core Java code of the filter is as follows:
Useritem loginuser = (useritem) request. getsession (). getattribute ("loginuser"); If (loginuser = NULL) {response. sendredirect ("/login. Action ");}
This is no problem for common pages, but it is often not expected when Ajax is used. Because the page is not redirected at all, only the source code of the logon page is captured.
On the Internet, a bunch of mediocre people give a bunch of methods, mainly including.
1. The returned string is appended with the logon status;(So much, are you willing to change it, and you have to change both the front and back ends, so you have to always think about the status when coding)
2. Modify the HTTP header information;(Pack B deep type)
3. Modify jquery source file judgment;(It's barely a programmer, but you try not to use the source code. If you have performance problems, you have to decompress it and compress it. Others may not know that you have changed the source code)
My solution is similar to the idea of the third method. Rewrite the Ajax method to implement pre-processing. The Code is as follows:
Jquery (function ($) {// back up jquery's Ajax method VaR _ Ajax =$. ajax; // rewrite the Ajax method. First, judge that the success function is being executed during login $. ajax = function (OPT) {VaR _ success = opt & OPT. success | function (a, B) {}; VaR _ opt = $. extend (OPT, {success: function (data, textstatus) {// if the background redirects the request to the logon page, the source code of the logon page is stored in data, here we need to find the evidence that data is the login page (TAG) if (data. indexof ('weianjie ')! =-1) {window. location. href = globals. CTX + "/login. action "; return ;}_ success (data, textstatus) ;}}); _ Ajax (_ opt );};});
In the source code of the login page, you can randomly find a place to mark and hide it. For example, my mark is weinianjie. For $. Get and $. Post, you don't need to mention it. Both methods call the $. Ajax method, so the pre-judgment capability is automatically obtained.
As a result, note that the above extensions must be executed before all your Ajax requests.