gpt4 book ai didi

java - 防止 mozilla session 恢复

转载 作者:行者123 更新时间:2023-11-30 09:35:17 26 4
gpt4 key购买 nike

Mozilla 能够恢复我的 Java EE 银行应用程序的 session (崩溃时,恢复 session 选项)。

有什么方法可以通过编写一些代码来阻止 mozilla 恢复 session 吗?或者是否可以识别 session 恢复时发出的请求并强制用户重新登录?

最佳答案

我正在研究同样的问题。

我认为让浏览器恢复旧 session 通常不是一个好主意。但是我尝试了几件事,例如设置缓存控制 header ,但这对我不起作用。

所以我确实在考虑在脚本中解决这个问题。

但是我发现不同的解决方案会带来不同的问题。

1) 在浏览器关闭时删除 cookie--> 问题:你应该用 javascript 添加漏洞来做到这一点,因为 http_only 应该设置为 false

2) 将 cookie_lifetime 从 0(浏览器关闭时)设置为例如 3600(1 小时)。--> 问题:设置为高会丢失安全性,将其设置为低会给用户一个“ session 已过期”的错误消息,这意味着他们必须重新登录。

3) 添加第二个带有过期日期的 cookie。如果 cookie 存在,则一切正常。--> 问题:(几乎)与 2) 相同,这只有在您可以在设置 cookie 后更改到期日期时才有效。在这种情况下,用户应该保持 Activity 状态至少 XX 分钟,否则他将失去他的 session (对我来说似乎是正确的)。

后面的我自己去看看。

所以在这种情况下,我使用的是 PHP(不是 Java),但我想总体思路是相同的:

//create expiration cookie
private function setExpireCookie(){

//expire cookie: name, value, expiration in seconds, path, domain, https, http-only
setcookie("Test_Expire", "Expire", time()+3600, "/test", null, false, true);
}

//check if expiration cookie exits
private function expireExists() {

return isset($_COOKIE['Test_Expire']);

}

//set the session check for expiration cookie
private function getSession() {

//does the session cookie exists and the expiration cookie doesnt?
if (!$this->expireExists() && isset($_COOKIE['Test_Cookie'])) {

//to remove the cookie by setting the expiration time before now
setcookie("Test_Cookie", $_COOKIE['Test_Cookie'], time()-3600, "/test");
}

//now we can set the new expiration cookies en start the new session
$this->setExpireCookie();

//set cookie params: lifetime, path, domain, https, http-only
session_set_cookie_params(0, "/api", null, false, true);

session_name('Test_Cookie');

//start session
session_start();
}

这对我有用。所以希望你可以在 Java 中应用它

关于java - 防止 mozilla session 恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11433220/

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