繁体   English   中英

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

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

我的要求是像每个用户一个登录名一样授予访问权限,为此,我已将登录状态更新为db(在用户登录时为true),在用户注销时为false。 但是问题是当用户关闭窗口而不注销时。

为了处理窗口关闭,我实现了以下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();
});

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

因此,我还需要像上述f5的js代码一样绑定重载事件,提交和定位标记。

在这方面,请帮助我。

提前致谢...

我想到两件事:

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

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

因此,与其尝试检查他们何时关闭选项卡,不如考虑只在他们从其他地方登录时注销他们和/或设置一个计时器,使其在一定的不活动状态后自动注销的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM