2016-06-06 20 views
3

Ich habe eine angularjs-App, in der ich einen Interceptor habe, der das Autorisierungs-Token zum Header jeder Anfrage hinzufügt.Wie angularjs Interceptor für eine HTTP-Anfrage zu überspringen?

Aber irgendwo in der Anwendung muss ich verwenden und externe API, wo der Interceptor es ruiniert, weil es diese Authorization-Header, die nicht von diesem externen API-Provider akzeptiert wird. Wie kann ich angularjs HTTP den Interceptor überspringen lassen, nur in diesem einen speziellen Fall?

Der Code Interceptor ist unten:

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; 
}]); 
+0

add Logik Hinzufügen von Auth-Header überspringen basierend auf 'config.url' – harishr

+1

Ich glaube, Sie nehmen sollten eine Beute in diesem Beitrag: http://stackoverflow.com/questions/23696202/set-defaults-header-on-angularjs-but-dont-use-it-on-one-specific-request –

Antwort

3

Einfach. Ändern Sie diese Zeile

if (authData) { 

zu

if (authData && !config.headers.hasOwnProperty('Authorization')) { 

Und für jede Anfrage, wo man nicht den Header wollen angewendet, verwenden

$http({ 
    headers { Authorization: null }, 
    // and the rest 
}) 
7

Leicht

$http.get("url" , {noAuth : true}).then(success(),error()); 

Im Interceptor

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; 
} 
6

schreiben wie folgt aus: -

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; 
    }   
} 

Für weitere Informationen können Sie sehen: http://www.codemosquitoes.com/2016/06/using-angularjs-interceptors-with-http.html