相关疑难解决方法(0)

AngularJS - 处理刷新令牌?

我正在与AngularJS建立一个与服务(JAVA)通信的SPA.

当用户发送他的用户名/密码时,服务会发回两个:访问令牌和刷新令牌.我正在尝试处理:如果我得到状态401的响应,则发送回刷新令牌,然后再次发送您的最后一个请求.我尝试使用包含$ http来做到这一点,但angular不允许我将它包含在这个拦截器中.有没有办法用我收到的响应参数重新创建原始请求?

就像是:

  1. 我得到了401
  2. 保存我的请求
  3. 如果我有刷新令牌发送该刷新令牌
  4. 成功后重新发送我的请求
  5. 在错误重定向到/登录页面

    '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 …
    Run Code Online (Sandbox Code Playgroud)

javascript authentication http-status-code-401 access-token angularjs

22
推荐指数
2
解决办法
2万
查看次数