Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除或覆盖Restanglar中作用域配置的请求侦听器_Javascript_Angularjs_Restangular - Fatal编程技术网

Javascript 删除或覆盖Restanglar中作用域配置的请求侦听器

Javascript 删除或覆盖Restanglar中作用域配置的请求侦听器,javascript,angularjs,restangular,Javascript,Angularjs,Restangular,我正在使用Restangular在单页web应用程序中处理令牌/头身份验证。 使用addFullRequestInterceptor,我使用个人密钥加密数据,为每个传出的RESTAPI调用设置正确的头 Restangular .setBaseUrl(CONSTANTS.API_URL) .setRequestSuffix('.json') .setDefaultHeaders({'X-MyApp-ApiKey': CONSTANTS.API_KEY

我正在使用Restangular在单页web应用程序中处理令牌/头身份验证。 使用addFullRequestInterceptor,我使用个人密钥加密数据,为每个传出的RESTAPI调用设置正确的头

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(此函数不存在)、重写它或类似的东西。

我会采取不同的方法,并尝试将标志传递给拦截器。如果该标志存在,则不包括
authSign
。您可以使用HttpConfig的
来执行此操作。最好排除特殊情况,而不要总是告诉拦截器包含
authSign

所以你会像这样更新拦截器

function requestInterceptor(element, operation, route, url, headers, params, httpConfig) {
    var timeStamp = Helpers.generateTimestamp();
    var allHeaders = {'X-MyApp-Timestamp': timeStamp};
    if(!httpConfig.excludeAuth) {
        //Condensed code for illustration purposes
        var authSign = Helpers.generateAuthenticationHash(hashIngredients, key, token);
        allHeaders['Authentication'] = authSign;
    }
    return angular.extend(headers, allHeaders);
}
specialRestInst.withHttpConfig({excludeAuth: true}).get({id: 'app'});
当您需要排除
authSign
时,您可以像这样使用restanglar

function requestInterceptor(element, operation, route, url, headers, params, httpConfig) {
    var timeStamp = Helpers.generateTimestamp();
    var allHeaders = {'X-MyApp-Timestamp': timeStamp};
    if(!httpConfig.excludeAuth) {
        //Condensed code for illustration purposes
        var authSign = Helpers.generateAuthenticationHash(hashIngredients, key, token);
        allHeaders['Authentication'] = authSign;
    }
    return angular.extend(headers, allHeaders);
}
specialRestInst.withHttpConfig({excludeAuth: true}).get({id: 'app'});
只要尚未使用,您就应该能够向http配置添加所需的任何值


我不确定这是否会像预期的那样起作用,但我不明白为什么它不会起作用。

我想这是一个选择。我已经通过检测
route
-参数来解决这个问题。但如果有可能进行子配置,我希望拦截器可以被覆盖。所以我希望有人知道怎么做(或者知道为什么不可能)。