Angularjs HTTP侦听器:对基于令牌的身份验证使用url参数而不是标头

Angularjs HTTP侦听器:对基于令牌的身份验证使用url参数而不是标头,angularjs,passport.js,angular-http,angular-http-interceptors,Angularjs,Passport.js,Angular Http,Angular Http Interceptors,我使用AngularJS中的后端api编写了以下基于令牌的身份验证代码: $httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) { return { 'request': function (config) { config.headers = config.hea

我使用AngularJS中的后端api编写了以下基于令牌的身份验证代码:

$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
        return {
            'request': function (config) {
                config.headers = config.headers || {};
                if ($localStorage.token) {
                    config.headers.Authorization = 'Bearer ' + $localStorage.token;
                }
                return config;
            },
            'responseError': function(response) {
                if(response.status === 401 || response.status === 403) {
                    $location.path('/signin');
                }
                return $q.reject(response);
            }
        };
    }]);
这将为我的令牌设置“授权”头


如何修改代码以使用URL参数(名为“access_token”)而不是标头?

您需要使用config对象的参数params–{Object.}

params–{Object.}
–url后将变为?key1=value1&key2=value2的字符串或对象的映射。如果该值不是字符串,则它将被JSONified


是的,我已经试过了。然而,它有一些“副作用”。使用此解决方案会导致我的rootscope出现一些问题(我设置了$rootscope.msg=Object…但是,使用您的解决方案时,我无法在html文件中输出$rootscope.msg.message。如果没有您的解决方案,这将很好)。
$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
        return {
            'request': function (config) {
                //config.headers = config.headers || {};
                 config.params = config.params || {};
                if ($localStorage.token) {
                   config.params.access_token = $localStorage.token;                        
                   //config.headers.Authorization = 'Bearer ' + $localStorage.token;
                }
                return config;
            },
            'responseError': function(response) {
                if(response.status === 401 || response.status === 403) {
                    $location.path('/signin');
                }
                return $q.reject(response);
            }
        };
    }]);