angular中的 登录检查 和 过期Session清理
angular利用ui-router进行登录检查
SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页.
监听ui-router的satte事件可以实现当state切换的时候检查登录情况
监听state变化处理页面刷新
//添加事件监听
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toState.name == 'register') return; // 如果是进入登录界面则允许
// 如果用户不存在
if (!$rootScope.session || !$rootScope.seccion.session_id) {
event.preventDefault(); // 取消默认跳转行为
$state.go("register", { from: fromState.name, w: 'notLogin' }); //跳转到登录界面
}
});
HttpProvider的Interceptor处理过期session
intercepto拦截器,可以处理所有的请求结果,能够实现:基于状态码统一的错误处理
如何注册interceptor:https://docs.angularjs.org/api/ng/service/$http#interceptors
//register interceptor as sercice
app.factory('UserInterceptor', ["$q","$rootScope",function ($q,$rootScope) {
return {
request:function(config){
config.headers["TOKEN"] = $rootScope.user.token;
return config;
},
responseError: function (response) {
var data = response.data;
// 判断错误码,如果是未登录
if(data["errorCode"] == "500999"){
// 清空用户本地token存储的信息
$rootScope.user = {token:""};
// 全局事件,方便其他view获取该事件,并给以相应的提示或处理
$rootScope.$emit("userIntercepted","notLogin",response);
}
// 如果是登录超时
if(data["errorCode"] == "500998"){
$rootScope.$emit("userIntercepted","sessionOut",response);
}
return $q.reject(response);
}
};
}]);
//push interceptor to interceptor list
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('UserInterceptor');
});
$rootScope.$on('userIntercepted',function(errorType){
// 跳转到登录界面,这里我记录了一个from,这样可以在登录后自动跳转到未登录之前的那个界面
$state.go("login",{from:$state.current.name,w:errorType});
});
相关
UI-State Events
$stateChangeCancel
$stateChangeError
$stateChangeStart
$stateChangeSuccess
$stateNotFound
Directive Hooks

详情:https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks
angular.module('do-check-module', [])
.component('app', {
template:
'Month: <input ng-model="$ctrl.month" ng-change="$ctrl.updateDate()">' +
'Date: {{ $ctrl.date }}' +
'<test date="$ctrl.date"></test>',
controller: function() {
this.date = new Date();
this.month = this.date.getMonth();
this.updateDate = function() {
this.date.setMonth(this.month);
};
}
})
.component('test', {
bindings: { date: '<' },
template:
'<pre>{{ $ctrl.log | json }}</pre>',
controller: function() {
var previousValue;
this.log = [];
//hook here
this.$doCheck = function() {
var currentValue = this.date && this.date.valueOf();
if (previousValue !== currentValue) {
this.log.push('doCheck: date mutated: ' + this.date);
previousValue = currentValue;
}
};
}
});
参考:
https://ui-router.github.io/docs/latest/modules/ng1_state_events.html
https://github.com/angular-ui/ui-router/wiki#state-change-events
http://www.brafox.com/post/2015/javascript/angularjs/angularjs-router-interceptor.html
angular中的 登录检查 和 过期Session清理的更多相关文章
- angular利用ui-router登录检查
angular利用ui-router登录检查 SAP都会有这个问题,session过期或者页面被刷新的情况下应该进入登录页. 监听ui-router的satte事件可以实现当state切换的时候检查登 ...
- oracle中的sys用户(修改密码)/////Oracle删除表空间的同时删除数据文件 ///// Oracle中如何保证用户只有一个session登录
oracle中的sys用户(修改密码) (2011-07-01 09:18:11) 转载▼ 标签: it 分类: oracle 概念: SYS用户是Oracle中权限最高的用户,而SYSTEM是一个用 ...
- Angular中ngCookies模块介绍
1.Cookie介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短 ...
- 授权认证登录之 Cookie、Session、Token、JWT 详解
一.先了解几个基础概念 什么是认证(Authentication) 通俗地讲就是验证当前用户的身份. 互联网中的认证: 用户名密码登录 邮箱发送登录链接 手机号接收验证码 只要你能收到邮箱/验证码,就 ...
- mvc通过ActionFilterAttribute做登录检查
1.0 创建Attribute using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- odoo11登录之后返回的session信息分析
{ "id": null, "jsonrpc": "2.0", "result": { "web_tours& ...
- 关于PHP中浏览器禁止Cookie后,Session能使用吗?
sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...
- 【struts2】自定义登录检查拦截器
在实际开发中,一个常见的功能要求是:有很多操作都需要登录后才能操作,如果操作的时候还没有登录,那么通常情况下会要求跳转回到登录页面. 1)如何实现这样的功能呢? 在具体实现之前,先来考虑几个问题: ( ...
- nutz中实现登录验证
一.nutz是什么 nutz是一个轻便的web端开发框架.主页如下:http://www.nutzam.com/core/nutz_preface.html 二.session简单介绍 大家都知道ht ...
随机推荐
- 【转】比较init-method,afterPropertiesSet和BeanPostProcessor
一.简单介绍 1.init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置.init-method需要在applicationContext.xml配置文档中bean的 ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- LeetCode——Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
- mysql 比较函数和操作符
MYSQL之中的比较函数和操作符: 1.[NOT] BETWEEN ... AND ... Check whether a value is within a range of values 说明: ...
- 实用ExtJS教程100例-004:等待对话框Ext.MessageBox.wait
在前面两节中,我们分别演示了ExtJS三种常用的对话框和ExtJS带有进度条的对话框.在本节内容中,我们来看看ExtJS中的等待对话框. 首先来看一个简单的例子[查看在线示例]: 这种对话框会一直滚动 ...
- Calendar获取当天的初始时间,当月的初始时间,当年的初始时间
如下:
- Installing Hyperledger Fabric v1.1 on Ubuntu 16.04 — Part II & Part III
This entire tutorial is the second part of the installation of Hyperledger Fabric v1.1. In the previ ...
- [转]Hive安装及使用攻略
转 : http://blog.fens.me/hadoop-hive-intro/
- 如何打开google,facebok等网站
用记事本打开 C:\WINDOWS\System32\drivers\etc\hosts 文件,粘贴如下蓝色内容到文件里,然后输入 例如 https://www.google.com https: ...