Java filters handle AJAX requests, Java interceptors handle AJAX requests, interceptors AJAX requests

Source: Internet
Author: User

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
  1. Import java.io.IOException;
  2. Import Java.io.PrintWriter;
  3. Import Javax.servlet.Filter;
  4. Import Javax.servlet.FilterChain;
  5. Import Javax.servlet.FilterConfig;
  6. Import javax.servlet.ServletException;
  7. Import Javax.servlet.ServletRequest;
  8. Import Javax.servlet.ServletResponse;
  9. Import Javax.servlet.http.HttpServletRequest;
  10. Import Javax.servlet.http.HttpServletResponse;
  11. Import Org.slf4j.Logger;
  12. Import Org.slf4j.LoggerFactory;
  13. Import org.springframework.beans.factory.annotation.Autowired;
  14. Import org.springframework.stereotype.Component;
  15. @Component ("Authenticationfilter")
  16. Public class Authenticationfilter implements Filter {
  17. @Autowired
  18. Sessioncontext Sessioncontext;
  19. private Logger log = Loggerfactory.getlogger (authenticationfilter.    Class);
  20. @Override
  21. public void init (Filterconfig filterconfig) throws servletexception {
  22. }
  23. @Override
  24. public void DoFilter (ServletRequest request, servletresponse response,
  25. Filterchain chain) throws IOException, servletexception {
  26. HttpServletRequest req = (httpservletrequest) request;
  27. HttpServletResponse res = (httpservletresponse) response;
  28. //To determine if it is an AJAX request, default is not
  29. Boolean isajaxrequest = false;
  30. if (! Strutils.isblank (Req.getheader ("X-requested-with")) && Req.getheader ("X-requested-with"). Equals ( "XMLHttpRequest")) {  
  31. Isajaxrequest = true;
  32. }
  33. Sysuser Sysuser = sessioncontext.getsysuserfromsession (req);
  34. if (sysuser! = null && sysuser.getuserid () = null) {
  35. Chain.dofilter (req, res);
  36. else{//session user is empty, login expires
  37. if (isajaxrequest) {//If it is an AJAX request, it is not a jump page, use response to return the result
  38. Res.setheader ("noauthentication", "true");
  39. Resultwithobject resultwithobject = new Resultwithobject (CC. Negative_1, CC.  Result_message_text_default);
  40. Resultwithobject.setmsg ("Login has expired, please refresh the page or log back in!")  ");
  41. Res.setcontenttype ("Application/json;charset=utf-8");
  42. PrintWriter writer = Res.getwriter ();
  43. Writer.write (jasonutils.object2string (resultwithobject));
  44. Writer.close ();
  45. Res.flushbuffer ();
  46. }else{
  47. Res.sendredirect ("Http://xxxx.com/loginUI");
  48. }
  49. }
  50. }
  51. @Override
  52. public Void Destroy () {
  53. }
  54. }

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>

? 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.