如何使用java和javascript限制用户仅在一个选项卡/窗口中登录

How to restrict a user to login in only one tab/window using java and javascript?

本文关键字:选项 一个 登录 窗口 javascript java 何使用 用户      更新时间:2023-09-26

我的要求是为每个用户提供一次登录的访问权限,为此,我在用户登录时将登录状态更新为真,在用户注销时将登录状态更新为真。但问题是当用户关闭窗口而不注销时。

为了处理窗口关闭,我实现了以下js代码

 var validNavigation = false;
 function wireUpEvents() {
  var dont_confirm_leave = 0; 
   var leave_message = 'You sure you want to leave?'
  function goodbye(e) {
    if (!validNavigation) {
     if (dont_confirm_leave!==1) {
         if(!e) e = window.event;
       //e.cancelBubble is supported by IE - this will kill the bubbling process.
       e.cancelBubble = true;
        e.returnValue = leave_message;
       //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
         e.stopPropagation();
         e.preventDefault();
          }
        //return works for Chrome and Safari
        return leave_message;
      }
      window.location = "logout.jsp";
    }
  }
  window.onbeforeunload=goodbye;
   // Attach the event keypress to exclude the F5 refresh
  $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });
  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });
  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });
   // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });
  // Attach the event click for all inputs in the page
 $("input[type='button']").bind("click", function() {
   validNavigation = true;
 });
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
 wireUpEvents();
});

在这里它适用于窗口关闭,这意味着如果用户关闭窗口,它会转到注销页面,但问题是当用户重新加载页面时它会注销页面。

所以我需要绑定重新加载事件,就像上面的 js 代码一样,用于 f5、提交和锚标签。

请在这方面帮助我。

提前致谢...

我想

到了两件事:

首先,用户已登录的事实并不意味着有任何活动正在进行,有些人在浏览器运行的情况下打开了PC。

其次,即使用户关闭了一个页面,也不一定意味着他/她想要注销,如果用户使用两个选项卡在您的网站上导航,则尤其如此。

因此,与其尝试检查他们何时关闭选项卡,我建议考虑仅在他们从其他地方登录时才将它们注销和/或设置一个计时器,在一定程度的不活动后自动注销它们的想法。