gpt4 book ai didi

java - 我如何续订 Spring session ?

转载 作者:行者123 更新时间:2023-12-05 06:45:25 26 4
gpt4 key购买 nike

我有一个在 Websphere 上运行 spring MVC 的 Web 应用程序,我正在尝试实现 session 过期确认。如果他们选择注销,我现在可以重定向到哪里,但如果他们选择注销,我不知道如何续订 session 。

到目前为止,我知道我正在使用 jQuery 和 ajax 将请求发送到服务器的某处,但不知道在哪里(url),也不知道参数或预期的响应。有谁知道我是如何发现这一点的?它是 spring session 的标准函数还是我必须编写自定义函数来解决这个问题?

最佳答案

我能够通过结合来自多个来源的信息来弄清楚。我想出了以下代码:

    function timeoutMessage(){
var popupdate = new Date();
var renewSession = confirm('Your Session is about to expire!\n\nYou will be logged out in 2 minute.\nDo you want to stay signed in?');
if(renewSession){
var response = new Date();
if(response - popupdate > 120000){
alert("Response took too long, current session has ended. \nRedirecting to login.");
}else{
pingServer();
resetTimeout();
}
}else{
window.location.href = "{app logout url}";
}
}

function pingServer(){
jQuery.ajax({url: "{valid server page url}",type: "HEAD",complete: function (XMLHttpRequest, textStatus) {}});
}

function resetTimeout(){
window.setTimeout(function(){timeoutMessage();},1080000);
}

$(document).ready(function()
resetTimeout();
});

该函数设置一个与 session 超时前两分钟重合的计时器。大多数站点将提供上述大部分代码,但 pingServer() 函数中的代码;为了在服务器上扩展 session ,您可以向服务器发送一个类型为“HEAD”的简单 ajax 调用,而不会中断应用程序的主要流程。它被视为服务器对 session 的操作,因此重置/更新 session 的超时。如代码中所示,您无需对服务器响应执行任何操作;只要您发送请求, session 超时就会被重置/更新。

希望对您有所帮助。我花了一段时间才完全拼凑起来。

关于java - 我如何续订 Spring session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23742447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com