【发布时间】:2014-06-15 12:14:26
【问题描述】:
我正在使用 AngularJS 构建一个带有与服务 (JAVA) 通信的 SPA。
当用户发送他的用户名/密码时,服务会同时发回:访问令牌和刷新令牌。我正在尝试处理:如果我收到状态为 401 的响应,请发回刷新令牌,然后再次发送您的最后一个请求。我试图通过包含 $http 来做到这一点,但是 Angular 不允许我将它包含在这个拦截器中。有没有办法用我收到的这个响应参数重新创建原始请求?
类似:
- 我得到 401
- 保存我的请求
- 如果我有刷新令牌,请发送该刷新令牌
- 成功后重新发送我的请求
-
错误重定向到 /login 页面
'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); } }; });
奖励问题(主要问题更重要):有 2 个移动应用程序也将连接到我的服务,当我从我的网络应用程序登录时,稍后从我的移动应用程序登录,移动应用程序需要新的刷新令牌和我的网络应用程序的刷新令牌不再有效。处理这个问题的最佳选择是什么?
感谢您的宝贵时间, 最好的问候
【问题讨论】:
-
嗨@dyslexisDcuk,你在这方面取得了任何成功,请与我分享。谢谢
标签: javascript angularjs authentication access-token http-status-code-401