Dys*_*cuk 22 javascript authentication http-status-code-401 access-token angularjs
我正在与AngularJS建立一个与服务(JAVA)通信的SPA.
当用户发送他的用户名/密码时,服务会发回两个:访问令牌和刷新令牌.我正在尝试处理:如果我得到状态401的响应,则发送回刷新令牌,然后再次发送您的最后一个请求.我尝试使用包含$ http来做到这一点,但angular不允许我将它包含在这个拦截器中.有没有办法用我收到的响应参数重新创建原始请求?
就像是:
在错误重定向到/登录页面
'use strict';
angular.module('testApp')
.factory('authentificationFactory', function($rootScope, $q, $window, $location, CONF) {
return {
request: function(config) {
config.headers = config.headers || {};
if ($window.sessionStorage.token) {
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
}
console.log(config);
$rootScope.lastRequest = config;
return config;
},
response: function(response) {
console.log($rootScope.lastRequest);
if (response.status === 401) {
if ($window.sessionStorage.refreshToken) {
//Save, request new token, send old response
//if it fails, go to login
$location.url('/login');
} else {
$location.url('/login');
}
}
return response || $q.when(response);
}
};
});
Run Code Online (Sandbox Code Playgroud)奖金问题(主要问题更重要):有2个移动应用程序也将连接到我的服务,当我从我的网络应用程序登录时,稍后我的移动应用程序,移动应用程序采取新的刷新令牌并且我的网络应用程序的刷新令牌不再有效.处理这个问题的最佳选择是什么?
谢谢你的时间,祝你好运
| 归档时间: |
|
| 查看次数: |
20161 次 |
| 最近记录: |