Ajax Session Expiration Jump Login page method _ajax related

Source: Internet
Author: User

In struts application, we send out the request will be related to the relevant interceptor processing, generally there will be a user login interception (session failure interception); General requests, if the session fails, we will jump to the login page, but if we use AJAX request, will return the HTML code for the login page, which is definitely not what we want, so how do we solve it? Please see the following steps:

First, the establishment of interceptors

Package com.xxx.planeap.interceptor;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.log4j.Logger;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
Import com.opensymphony.xwork2.ActionInvocation;
Import Com.opensymphony.xwork2.ActionSupport;
Import Com.opensymphony.xwork2.interceptor.AbstractInterceptor;
Import Com.xxx.common.contants.ConstantsKey;
Import Com.xxx.common.contants.SessionKey;
Import Com.xxx.planeap.domain.User;
Import Com.xxx.planeap.security.SecurityContextUtil; /** * * @author Goma OMA1989@YEAH.NET * @version v1.0 * @since Info */public class Securityinterceptor extends Abstractinterceptor {private static final long serialversionuid = 1L; private Logger Logger = Logger.getlogger (securityin
Terceptor.class); @Override public String Intercept (actioninvocation invocation) throws Exception {//TODO auto-generated method Stub Strin G ClassName = InvocatioN.getaction (). GetClass (). GetName (); String action = classname.substring (Classname.lastindexof (".")
+1,classname.length ());
String actionname = Invocation.getproxy (). Getactionname ();
String result;
HttpServletRequest request = Servletactioncontext.getrequest ();
HttpServletResponse response = Servletactioncontext.getresponse ();
String type = Request.getheader ("X-requested-with");
User user = (user) Actioncontext.getcontext () getsession (). get (Sessionkey.current_user);  if (user = = null) {Logger.debug ("checked:need to LOGIN"); if ("XMLHttpRequest". Equalsignorecase (Type)) {//AJAX
REQUEST PROCESS response.setheader ("Sessionstatus", constantskey.msg_time_out);
result = NULL; else {//NORMAL REQUEST PROCESS result = Actionsupport.login;}}
else {logger.debug ("Security Checked:user has logined");
Securitycontextutil.setcurrentuser (user);
Boolean hanperm = Securitycontextutil.hasperm (action, actionname); Logger.debug ("Security checked:permission---" +action+ ".) +actionname+ "=" +hanpERM);
result = Invocation.invoke ();
return result; }
}

Defining a global AJAX request-end processing method

Global AJAX access, processing AJAX $.ajaxsetup Session Timeout
({
contentType: "application/x-www-form-urlencoded;charset= Utf-8 ",
complete:function (xmlhttprequest,textstatus) {
//Get response header via XMLHttpRequest, Sessionstatus 
var Sessionstatus=xmlhttprequest.getresponseheader ("Sessionstatus"); 
if (sessionstatus== "timeout") {
//Here's how to deal with you, here jump to the login page
window.location.replace (Planeap.getactionuri (" Login ");}
}
);

That is, Ajax to send a request if the interception returns a representation on the jump, otherwise perform normal operation.

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.