Ran*_*s1r 7 asp.net oauth-2.0 jwt angularjs refresh-token
我一直试图让我的刷新令牌工作一段时间,我希望我很接近.我的令牌刷新并触发后续200调用导致401的任何调用,但我的页面上的数据不刷新.
访问令牌到期时,会发生以下情况:
在401之后,GetListofCompanyNames使用正确更新的访问令牌返回200,其中包含名称列表.但是,我的下拉列表不会刷新.
我的拦截器:
app.factory('authInterceptorService',['$q', '$location', 'localStorageService', '$injector', function($q, $location, localStorageService, $injector) {
return {
request: function(config) {
config.headers = config.headers || {};
var authData = localStorageService.get('authorizationData');
if (authData) {
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
},
responseError: function(rejection) {
//var promise = $q.reject(rejection);
var authService = $injector.get('authService');
if (rejection.status === 401) {
// refresh the token
authService.refreshToken().then(function() {
// retry the request
var $http = $injector.get('$http');
return $http(rejection.config);
});
}
if (rejection.status === 400) {
authService.logOut();
$location.path('/login');
}
return $q.reject(rejection);
}
};
}
]);
Run Code Online (Sandbox Code Playgroud)
关于401拒绝的回复声明在这里看起来很可疑,但我不确定要用什么来代替它.因此我的问题是:当我拨打新电话时,如何让我的页面刷新它的数据?
更新:
这让我过去了,当200返回并且我可以得到一个下拉菜单刷新,但我在页面上丢失了任何状态(例如选择下拉列表),如下所示.
authService.refreshToken().then(function() {
var $state = $injector.get('$state');
$state.reload();
});
Run Code Online (Sandbox Code Playgroud)
回到绘图板!
我的最终解决方案:
app.factory('authInterceptorService', ['$q', '$location', 'localStorageService', '$injector', function($q, $location, localStorageService, $injector) {
var $http;
var retryHttpRequest = function(config, deferred) {
$http = $http || $injector.get('$http');
$http(config).then(function(response) {
deferred.resolve(response);
},
function(response) {
deferred.reject(response);
});
}
return {
request: function(config) {
config.headers = config.headers || {};
var authData = localStorageService.get('authorizationData');
if (authData) {
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
},
responseError: function(rejection) {
var deferred = $q.defer();
if (rejection.status === 401) {
var authService = $injector.get('authService');
authService.refreshToken().then(function() {
retryHttpRequest(rejection.config, deferred);
},
function () {
authService.logOut();
$location.path('/login');
deferred.reject(rejection);
});
} else {
deferred.reject(rejection);
}
return deferred.promise;
}
};
}
]);
Run Code Online (Sandbox Code Playgroud)
它透明地处理所有请求并在必要时刷新它们。当刷新令牌过期时,它会注销用户,并通过正确拒绝错误将错误传递给控制器。然而,它似乎不适用于多个飞行请求,当我在系统中得到它的用例时,我会研究一下。