Angular 会话超时和管理 [英] Angular session timeout and management

查看:27
本文介绍了Angular 会话超时和管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 Angularjs 管理用户会话吗?,我的意思是::

Is there any way to manage user session using Angularjs?, I mean::

  • 会话超时 - 系统空闲时.
  • 在会话即将到期时发出警报,并提供恢复会话的选项.
  • 尝试在会话已过期的情况下发出请求时重定向(或任何其他操作).

拦截器可能是解决这个问题的一个好选择吗?你能举个例子吗?

Could be Interceptors one good option to solve this problem? Can you provide an example?

推荐答案

尝试 ng-idle.它是一个简单的组件,您可以在其中设置超时和到达超时之前的警告时间.然后您可以查询服务器以获取用户注销或类似信息.

Try ng-idle. It's simple component where you can set the timeout and warning time before the timeout is reached. Then you can query server for user logout or something similar.

myApp.config(function(IdleProvider, KeepaliveProvider) {
  IdleProvider.idle(900); // 15 min
  IdleProvider.timeout(60);
  KeepaliveProvider.interval(600); // heartbeat every 10 min
  KeepaliveProvider.http('/api/heartbeat'); // URL that makes sure session is alive
});

myApp.run(function($rootScope, Idle) {
  Idle.watch();
  $rootScope.$on('IdleStart', function() { /* Display modal warning or sth */ });
  $rootScope.$on('IdleTimeout', function() { /* Logout user */ });
});

在上述配置中,当用户空闲 900 秒(不移动鼠标,按任意键或按钮等)时,会显示警告.然后它将等待 60 秒并注销用户(向可能破坏服务器会话的服务器发送请求).

In the above configuration, when user is idle for 900s (does not move mouse, press any key or button etc), warning is being displayed. It will then wait 60s and log out user (send request to a server that possibly destroys server session).

为了确保服务器会话不会过期(即使用户所做的一切都是移动鼠标),Keepalive 服务将每 10 分钟向服务器发送一次请求.这个时间必须小于服务器会话过期时间.

In order to make sure server session does not expire (even if everything user is doing is moving mouse) the Keepalive service will send a request to the server every 10 minutes. This time has to less than server session expiration time.

查看演示.

这篇关于Angular 会话超时和管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆