如何跳过HTTP请求的angularjs拦截器?

Beh*_*ooz 4 interceptor angularjs angularjs-http

我有一个angularjs应用程序,其中我有一个拦截器,它将授权令牌添加到每个请求的标头中.

但是,我需要使用的应用程序中的某个地方以及拦截器对其进行破坏的外部API,因为它添加了此外部API提供程序无法接受的此授​​权标头.我怎样才能使angularjs HTTP跳过拦截器,只针对这一个具体情况?

拦截器代码如下:

app.factory('authInterceptorService', ['$q', '$injector', '$location', 'localStorageService', function ($q, $injector, $location, localStorageService) {
    var authInterceptorServiceFactory = {};
    var $http;

    var _request = function (config) {

        config.headers = config.headers || {};

        var authData = localStorageService.get('authorizationData');
        if (authData) {
            //console.log("token: " + authData.token.substring(0, 10));
            //console.log("user: " + authData.userName);
            config.headers.Authorization = 'Bearer ' + authData.token;
        }
        return config;
    }

    var _responseError = function (rejection) {
        var deferred = $q.defer();
        if (rejection.status === 401) {
            var authService = $injector.get('authService');
            authService.refreshToken().then(function (response) {
                _retryHttpRequest(rejection.config, deferred);
            }, function () {
                authService.logOut();
                $location.path('/login');
                deferred.reject(rejection);
            });
        } else {
            deferred.reject(rejection);
        }
        return deferred.promise;
    }

    var _retryHttpRequest = function (config, deferred) {
        console.log('retrying');
        $http = $http || $injector.get('$http');
        $http(config).then(function (response) {
            deferred.resolve(response);
            //console.log("success:" +response);
        }, function (response) {
            deferred.reject(response);
            //console.log("error:" + response);
        });
    }

    authInterceptorServiceFactory.request = _request;
    authInterceptorServiceFactory.responseError = _responseError;

    return authInterceptorServiceFactory;
}]);
Run Code Online (Sandbox Code Playgroud)

San*_*h B 9

简单

$http.get("url" , {noAuth : true}).then(success(),error());
Run Code Online (Sandbox Code Playgroud)

在拦截器中

  var _request = function (config) {

    config.headers = config.headers || {};

    var authData = localStorageService.get('authorizationData');
    if (authData && !config.noAuth) {
        //console.log("token: " + authData.token.substring(0, 10));
        //console.log("user: " + authData.userName);
        config.headers.Authorization = 'Bearer ' + authData.token;
    }
    return config;
}
Run Code Online (Sandbox Code Playgroud)


Cur*_*eek 6

写得像这样: -

   var _request = function (config) {
    if (config.url.indexOf('yourExternalApiUrl') > -1) {
         return config;
    } else {
         config.headers = config.headers || {};
         var authData = localStorageService.get('authorizationData');
         if (authData) {
             //console.log("token: " + authData.token.substring(0, 10));
             //console.log("user: " + authData.userName);
             config.headers.Authorization = 'Bearer ' + authData.token;
         }
    return config;
    }        
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅:http://www.codemosquitoes.com/2016/06/using-angularjs-interceptors-with-http.html


Phi*_*hil 5

简单的。改变这一行

if (authData) {
Run Code Online (Sandbox Code Playgroud)

if (authData && !config.headers.hasOwnProperty('Authorization')) {
Run Code Online (Sandbox Code Playgroud)

对于您不希望应用标头的任何请求,请使用

$http({
    headers { Authorization: null },
    // and the rest
})
Run Code Online (Sandbox Code Playgroud)