Java filters handle AJAX requests, Java interceptors handle AJAX requests, interceptors AJAX requests
Java judgment request is not an AJAX request, Java judge whether it is an AJAX request
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>
? Copyright Sweet Potato Yiu August 10, 2017
http://www.cnblogs.com/fanshuyao/
First, the problem description:
When you visit a page that needs to log in, there will be filters or interceptors to filter interception, if the user is not logged in, then jump to the login page.
When the user has logged into the system, and then long time no operation, wait until the session expires, and then click on an AJAX request operation, then jump to the login page is not appropriate, because this is an AJAX operation, the interception after jumping to the page to return the results JS can not identify.
Second, the solution:
Identify on a filter or interceptor and process it separately for page jump requests and AJAX requests.
Page jumps no longer say, now is the AJAX request.
Direct Sticker Code:
Java code
- Import java.io.IOException;
- Import Java.io.PrintWriter;
- Import Javax.servlet.Filter;
- Import Javax.servlet.FilterChain;
- Import Javax.servlet.FilterConfig;
- Import javax.servlet.ServletException;
- Import Javax.servlet.ServletRequest;
- Import Javax.servlet.ServletResponse;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Import Org.slf4j.Logger;
- Import Org.slf4j.LoggerFactory;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import org.springframework.stereotype.Component;
- @Component ("Authenticationfilter")
- Public class Authenticationfilter implements Filter {
- @Autowired
- Sessioncontext Sessioncontext;
- private Logger log = Loggerfactory.getlogger (authenticationfilter. Class);
- @Override
- public void init (Filterconfig filterconfig) throws servletexception {
- }
- @Override
- public void DoFilter (ServletRequest request, servletresponse response,
- Filterchain chain) throws IOException, servletexception {
- HttpServletRequest req = (httpservletrequest) request;
- HttpServletResponse res = (httpservletresponse) response;
- //To determine if it is an AJAX request, default is not
- Boolean isajaxrequest = false;
- if (! Strutils.isblank (Req.getheader ("X-requested-with")) && Req.getheader ("X-requested-with"). Equals ( "XMLHttpRequest")) {
- Isajaxrequest = true;
- }
- Sysuser Sysuser = sessioncontext.getsysuserfromsession (req);
- if (sysuser! = null && sysuser.getuserid () = null) {
- Chain.dofilter (req, res);
- else{//session user is empty, login expires
- if (isajaxrequest) {//If it is an AJAX request, it is not a jump page, use response to return the result
- Res.setheader ("noauthentication", "true");
- Resultwithobject resultwithobject = new Resultwithobject (CC. Negative_1, CC. Result_message_text_default);
- Resultwithobject.setmsg ("Login has expired, please refresh the page or log back in!") ");
- Res.setcontenttype ("Application/json;charset=utf-8");
- PrintWriter writer = Res.getwriter ();
- Writer.write (jasonutils.object2string (resultwithobject));
- Writer.close ();
- Res.flushbuffer ();
- }else{
- Res.sendredirect ("Http://xxxx.com/loginUI");
- }
- }
- }
- @Override
- public Void Destroy () {
- }
- }
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>
? Copyright Sweet Potato Yiu August 10, 2017
http://www.cnblogs.com/fanshuyao/
Java filters handle AJAX requests, Java interceptors handle AJAX requests, interceptors AJAX requests