簡體   English   中英

刷新令牌的工作方式以及上次失敗的http請求如何再次調用,從而給了401…?

[英]How refresh token works and how last failed http request called again which gave 401…?

我正在使用angularjs使用OAuth2 現在,我無法使用OAuth進行身份驗證,因為我無法重新發送最后一個401 API。 任何想法。

我正在使用 oauth2存儲庫。

Controller.js

app.controller('validate', ['$scope', '$rootScope', '$location', 'fullname', '$http', '$timeout', '$cookies', 'OAuth', function ($scope, $rootScope, $location, fullname, $http, $timeout, $cookies, OAuth) {
OAuth.getAccessToken($scope.user).then( function successCallBack(response){
            $scope.response = response;
            if($scope.response.status == 200){
                console.log($scope.response.data);
                $scope.accessToken      = $scope.response.data.access_token;
                $scope.refreshToken     = $scope.response.data.refresh_token;

                localStorage.setItem("accessToken", $scope.accessToken);
                localStorage.setItem("refreshToken", $scope.refreshToken);

                var userId = response.headers('userid');
                console.log(userId);
                $cookies.put("userId", userId);
                window.location.href = 'user_profile.php';
            }
        }, function errorCallBack(response){
            console.log(response);
        });
}]);

app.js

app.config(['OAuthProvider', function(OAuthProvider) {
OAuthProvider.configure({
  baseUrl: 'http://testzone.xxxxxx.net/api/LoginTest/Login/web/',
  clientId: '123456789',
  clientSecret: 'otszh9nonaosok88gsswc8k4w8ww04s',
  grantPath: 'api/oauth2/token',
  revokePath: 'api/oauth2/revoke'
});
}]);

app.run(['$rootScope', '$window', 'OAuth', '$cookies', '$timeout', function($rootScope, $window, OAuth, $cookies, $timeout) {
$rootScope.$on('oauth:error', function(event, rejection) {

  // Ignore `invalid_grant` error - should be catched on `LoginController`.
  if ('invalid_token' === rejection.data.error || 'invalid_grant' === rejection.data.error || 'invalid_request' === rejection.data.error || 'invalid_client' === rejection.data.error || 'unauthorized_client' === rejection.data.error || 'unsupported_grant_type' === rejection.data.error) {
        $cookies.remove('userId');
        $timeout(function(){
            window.location.href = 'index.php';
        },200);
  }

  // Refresh token when a `invalid_token` error occurs.
  if ('expired_token' === rejection.data.error) {
      console.log(rejection);

      OAuth.getRefreshToken();

  }

  console.log(rejection);
  console.log(rejection.data.error);
  console.log(rejection.data.error_description);

  // Redirect to `/login` with the `error_reason`.
  //return $window.location.href = 'index.php';

});
}]);

謝謝

分析錯誤響應時,您可以執行以下操作:

if (rejection.status === 401) {
    var authService = $injector.get('oAuthService');
    var authData = ipCookie(oAuthConstants.oAuthCookieName);
    var $http = $http || $injector.get('$http');
    var deferred = $q.defer();

    if (authData) {
        authService.refreshToken().then(function () {
            //this repeats the request with the original parameters
            return deferred.resolve($http(rejection.config));
        });
    }

    return deferred.promise;
}
else if (rejection.status === 403) {
    var toaster = $injector.get('toaster');
    toaster.pop('error', "Access Denied", "You are not authorized to do this request.");
}
else {
    return $q.reject(rejection);
}

重復上一個401 API調用的關鍵是:

return deferred.resolve($http(rejection.config));

希望對您有所幫助。

  • refresh token是一種特殊的JWT ,用於隨時獲取更新的id_token

  • 刷新令牌攜帶獲取新訪問令牌所需的信息。 換句話說,無論何時需要訪問令牌來訪問特定資源,客戶端都可以使用刷新令牌來獲取認證服務器發布的新訪問令牌。

以及如何在angular js中使用它,請參閱此鏈接, 單擊此處將指導您如何做。


看到這個相關項目,您可以從中獲得啟發。 github代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM