Home > Web Front-end > JS Tutorial > body text

Immediately jump to the login page after Ajax+Session fails

php中世界最好的语言
Release: 2018-04-04 14:09:16
Original
1633 people have browsed it

This time I will bring you the method of jumping to the login page immediately after Ajax+Session fails, and how to jump to the login page immediately after Ajax+Session fails. take a look. In the Struts application, the requests we send will be processed by the corresponding interceptor. Generally, there will be a

User login

interception (Session failure interception); for general requests, if the Session When it fails, we will jump to the login page, but if we use AJAX to request, the HTML code of the login page will be returned. This is definitely not what we want, so how do we solve it? Please see the following steps:

1. Create an interceptor

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 2012-05-31
* 
*/
public class SecurityInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger(SecurityInterceptor.class);
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
String 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("SECURITY 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;
}
}
Copy after login

2. Define the global AJAX request end processing method

//全局的AJAX访问,处理AJAX清求时SESSION超时
$.ajaxSetup({
contentType:"application/x-www-form-urlencoded;charset=utf-8",
complete:function(XMLHttpRequest,textStatus){
//通过XMLHttpRequest取得响应头,sessionstatus 
var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); 
if(sessionstatus=="timeout"){
//这里怎么处理在你,这里跳转的登录页面
window.location.replace(PlanEap.getActionURI("login"));
}
}
});
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Solution to the problem that ajax internal values ​​cannot be called externally


How to preview HTML5+ajax Picture effect

The above is the detailed content of Immediately jump to the login page after Ajax+Session fails. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!