简体   繁体   English

断开连接或关闭浏览器后如何注销用户?

[英]How to logout user after disconnect or browser close?

I'm trying to work a solution to timing out a user from my web application. 我正在尝试一种解决方案,以使我的Web应用程序中的用户超时。 I'm currently using ng2-idle and it seems to only work on the active window rather than be tracked server side (angular server webpack) 我目前正在使用ng2-idle,它似乎仅在活动窗口上起作用,而不是在服务器端被跟踪(角度服务器Webpack)

I need to handle these two events in addition to the one above: 除上述事件外,我还需要处理以下两个事件:

  • On Browser Close 在浏览器上关闭
  • On Connection loss (Power cut, blue screen, etc..) 亮连接断开(断电,蓝屏等。)

After testing, my timeout was not being tracked after closing the window. 测试后,关闭窗口后未跟踪我的超时。 Ng2-idle has modules such as keepalive but I'm not exactly sure how to use it and if it solves my problem Ng2-idle具有诸如keepalive之类的模块,但是我不确定如何使用它以及它是否解决了我的问题

I will provide code if needed 如果需要,我将提供代码

thank you 谢谢

The main problem is, that the client and server are communicating in stateless manner. 主要问题是,客户端和服务器以无状态方式进行通信。 This means if the user disconnects no body knows. 这意味着如果用户断开连接,没有人知道。

If your browser has a hook function for closing or navigating to another site you could use that and send the logout request. 如果您的浏览器具有用于关闭或导航到另一个站点的挂钩功能,则可以使用该功能并发送注销请求。

Another thing which relavant is session expiration, you should use that. 与之相关的另一件事是会话到期,您应该使用它。 If you are using a token you will need to blacklist that, as non active as long as the session may be valid (or however). 如果您使用的是令牌,则您需要将其列入黑名单,只要该会话可能是有效的(或有效),就将其禁用。

Disconnect is a major problem (session expiration tried to solve that somehow). 断开连接是一个主要问题(会话过期试图以某种方式解决该问题)。 A more sophisticated way if it is really crucial to log out on disconnect you may need to use websockets or http long polling. 如果注销对注销至关重要,那么一种更复杂的方法可能需要使用websockets或http long轮询。 You would need to send a heartbeat and if it's not responding, after some time you will automatically logout the user. 您将需要发送心跳,如果它没有响应,则一段时间后,您将自动注销用户。

Hope these thoughts kind somehow help. 希望这些想法能有所帮助。

当您关闭浏览器会话时,会话将销毁。因此,您可以使用会话注销。

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

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