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.