您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何停止浏览器之间的选项卡共享会话?

如何停止浏览器之间的选项卡共享会话?

成功登录后,在sessionStorage.setItem(’userId’,userId)中输入一些值,当用户打开新标签页并尝试登录时,请检查sessionStorage.getItem(’userId’)是否为null(这意味着它是一个标签页)/重定向登录页面

会话存储特定于选项卡,并且不同选项卡之间不共享数据。会话存储在现代浏览器中运行。

检查此链接获取详细信息

尝试下面的代码

<script>

  if(typeof(Storage) !== "undefined") {
      sessionStorage.setItem("uniqueIdSessionStorage", "xyz");
  }
</script>


sessionStorage.getItem('uniqueIdSessionStorage') // this will be a tab specific you will not get xyz for other tabs.

1)检查sessionStorage.getItem(’uniqueIdSessionStorage’)是否不为null,如果null表示新的标签页和新的用户

2)在服务器端始终存储会话属性,如下面的代码

 session.setAttribute("userId"+UniqueValuePerUser,userId);

3)这样,您可以使用单个会话对象进行多次登录,因为每个用户密钥都是唯一的。

4)在请求参数中以某种方式传递sessionStorage值服务器端。一种方法是发送url或隐藏在输入中的某处。

5)现在,如果您从选项卡中获得了12345的值。然后使用以下代码从会话中获取详细信息

String uniqueId= request.getParameter("uniqueId"); // value will be 12345
session.getAttribute("userId"+uniqueId);

如果从选项卡中获得45678的值,则

String uniqueId= request.getParameter("uniqueId"); // value will be 45678
session.getAttribute("userId"+uniqueId) // and other details from session using unique id;

6)这样,在单个会话中使用唯一密钥可以实现多次登录,但是如果一个注销使会话无效,则其他用户也将被注销,因为会话对象是具有唯一密钥的对象。

7)从会话中删除该特定密钥,而不是使会话无效。

session.removeAttribute("userId"+uniqueId);
其他 2022/1/1 18:14:12 有548人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶