【发布时间】:2016-01-13 22:33:09
【问题描述】:
我正在使用 Restangular 在单页 Angular Web 应用程序中处理我的令牌/标头身份验证。 使用 addFullRequestInterceptor,我为每个传出的 REST API 调用设置正确的标头,使用个人密钥加密数据。
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 采用了基本配置并对其进行了扩展。我想知道如何删除FullRequestInterceptor(这个函数不存在),覆盖它,或者类似的东西。
【问题讨论】:
标签: javascript angularjs restangular