我正在使用Restangular在單個頁面中處理我的令牌/標頭認證Angular Web應用程序。 使用addFullRequestInterceptor,我爲每個傳出REST API調用設置正確的標頭,使用個人密鑰來加密數據。刪除或覆蓋Restangular中作用域配置的請求攔截器
Restangular
.setBaseUrl(CONSTANTS.API_URL)
.setRequestSuffix('.json')
.setDefaultHeaders({'X-MyApp-ApiKey': CONSTANTS.API_KEY})
.addFullRequestInterceptor(requestInterceptor)
.addErrorInterceptor(errorInterceptor);
function requestInterceptor(element, operation, route, url, headers, params, httpConfig) {
var timeStamp = Helpers.generateTimestamp(),
//Condensed code for illustration purposes
authSign = Helpers.generateAuthenticationHash(hashIngredients, key, token),
allHeaders = angular.extend(headers, {
'X-MyApp-Timestamp': timeStamp,
'Authentication': authSign
});
return {
headers: allHeaders
}
}
很好用。我需要一個例外:對於尚未登錄的新訪問者,通過REST請求一個通用密鑰/令牌對。此密鑰/令牌對用於登錄身份驗證調用的標頭中。 因此,對於此調用,我創建了一個單獨的Restangular子配置。在這個配置中,我想覆蓋requestInterceptor。但是這似乎被忽略了(即原始攔截器仍被稱爲)。無論我傳遞null還是返回空對象的函數都沒關係。
var specialRestInst = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.addFullRequestInterceptor(function() {return {}});
}),
timeStamp = Helpers.generateTimestamp(),
header = {'X-MyApp-Timestamp': timeStamp};
specialRestInst.one('initialise').get({id: 'app'}, header)
因此,如Restangular所記錄的,withConfig會採用基本配置並對其進行擴展。我想知道如何removeFullRequestInterceptor(這個函數不存在),覆蓋它,或類似的東西。
這是我猜測的一個選項。我已經設法通過檢測'route'參數是什麼來解決這個問題。但是如果可以做出子配置,我預計攔截器可以被覆蓋。所以我希望有人知道如何(或知道爲什麼不可能)。 – Micros