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

webix+springmvc session timeout jumps to login page

高洛峰
Release: 2017-01-07 09:32:14
Original
1454 people have browsed it

Introduction

Recently working on a project, I found that ajax requests cannot be redirected directly to the login page in the server. After checking some information, I found that someone has given a method for jquery's ajax request. But there are some differences between webix's ajax request and jquery's. This imitates jquery's processing method to implement webix's ajax request session timeout jump.

Specific approach:

1. Check the webix.js source code and find that webix.ajax only has the pre-request listening function "onBeforeAjax". To obtain the return status and jump to the login page, it must be A returned listening function, but the source code does not. So I modified the source code and added a returned listening function "onAfterAjax".

The red marked part is the code I added. When it detects that ajax is completed, "onAfterAjax" is automatically executed. (The location of the code can be searched for webix.js, the condition is "onBeforeAjax", and then just add the red code at the corresponding location

if (webix.callEvent("onBeforeAjax", [s, t, e, a, o, null, r])) {
var h = !1;
if ("GET" !== s) {
var l = !1;
for (var c in o)"content-type" == c.toString().toLowerCase() && (l = !0, "application/json" == o[c] && (h = !0));
l || (o["Content-Type"] = "application/x-www-form-urlencoded")
}
if ("object" == typeof e)if (h)e = this.stringify(e); else {
var u = [];
for (var d in e) {
var f = e[d];
(null === f || f === webix.undefined) && (f = ""), "object" == typeof f && (f = this.stringify(f)), u.push(d + "=" + encodeURIComponent(f))
}
e = u.join("&")
}
e && "GET" === s && (t = t + (-1 != t.indexOf("?") ? "&" : "?") + e,
e = null), a.open(s, t, !this.H);
var b = this.Tw;
b && (a.responseType = b);
for (var c in o)a.setRequestHeader(c, o[c]);
var x = this;
return this.master = this.master || n, a.onreadystatechange = function () {
if (!a.readyState || 4 == a.readyState) {
if (webix.callEvent("onAfterAjax", [a]) === !1) {
return false;
};
if (webix.ajax.count++, i && x && !a.aborted) {
if (-1 != webix.ly.find(a))return webix.ly.remove(a);
var t, e, s = x.master || x, r = a.status >= 400 || 0 === a.status;
"blob" == a.responseType || "arraybuffer" == a.responseType ? (t = "", e = a.response) : (t = a.responseText || "", e = x.J(a)), webix.ajax.$callback(s, i, t, e, a, r)
}
x && (x.master = null), i = x = n = null
}
}, this.qh && (a.timeout = this.qh), this.H ? a.send(e || null) : setTimeout(function () {
a.aborted || (-1 != webix.ly.find(a) ? webix.ly.remove(a) : a.send(e || null));
}, 1), this.master && this.master.Ve && this.master.Ve.push(a), this.H ? a : r
}
Copy after login

2. The webix.ajx request has no obvious sign, and the logo of jquery.ajax is x- requested-with, so I simulated giving a logo requestFlag="webix" (you can set your own preference), and set it with "onBeforeAjax"

webix.attachEvent("onBeforeAjax",function(s, t, e, a, o){o["requestFlag"]="webix"})
Copy after login

3 , Monitoring return status

webix.attachEvent("onAfterAjax",function(xhr){if(xhr.getResponseHeader("sessionstatus")=='timeout'){window.location.href='/webix/login.html'}});
Copy after login

4. Background code

4.1 Interceptor code

package com.ljx.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class UserInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0,
HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception {
response.sendRedirect("/webix/login.html");
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
Object obj = request.getSession().getAttribute("LOGIN");
if (null == obj) { // 未登录
if (request.getHeader("requestFlag") != null
&& request.getHeader("requestFlag").equalsIgnoreCase(
"webix")) { // 如果是ajax请求响应头会有,requestFlag
response.setHeader("sessionstatus", "timeout");// 在响应头设置session状态
} else {
response.sendRedirect(request.getContextPath() + "/login");
}
return false;
}
return true;
}
}
Copy after login

4.2 Add the interceptor configuration to the spring configuration file

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/mvc/*" />
<bean class="com.ljx.filter.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
Copy after login

4.3 Check the effect of webix.ajax under F12 console execution

webix.ajax().get("/webix/mvc/login.action")
Copy after login

The above is the webix+springmvc session timeout jump login page introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. , the editor will reply to everyone in time.

For more articles related to webix+springmvc session timeout jump login page, please pay attention to 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!