-1

环境:

  • 我正在使用 typeahead/bloodhound 作为我的移动应用程序中的搜索字段(类固醇/cordova)
  • 从我的应用程序到 API 的每个请求都需要签名并将签名添加到 auth 标头

显然,在 ajax 设置中设置标头将不起作用,因为 Bloodhound 发送的每个请求都会不同并且需要不同的签名。

在我的第一个实现中,我使用beforeSendajax 设置来实现这一点。只需计算该函数中的签名并将其添加到请求标头中。

但是,这不是很安全,所以我决定将使用的秘密和签名计算放入 Cordova 自定义插件的本地代码中进行编译。不是防弹,而是合理的安全性。

由于 Cordova 插件是异步的,beforeSend在这种情况下变得毫无用处。该功能将在标头的签名和设置完成之前完成。

所以,总而言之,问题是:我怎样才能异步计算和设置那些带有 typeahead/bloodhound 的标头?

4

1 回答 1

1

好的,解决方案似乎是分叉和破解。首先修改以通过添加类似于的选项_getFromRemote来删除需要,除了它返回一个延迟对象beforeSendremote.headersremote.replace

if (this.remote.headers) {
    $.when(
        this.remote.headers(url, query, this.remote.ajax)
    ).done(function(headers) {
        that.remote.ajax.headers = headers;
        deferred.resolve(that.transport.get(url, that.remote.ajax, handleRemoteResponse));
     });
} else {
    deferred.resolve(this.transport.get(url, this.remote.ajax, handleRemoteResponse));
}

然后修改使用它来处理延迟的get函数

if (matches.length < this.limit && this.transport) {
    cacheHitPromise = this._getFromRemote(query, returnRemoteMatches);
    cacheHitPromise.done(function(hit) {
        if (!hit) {
            (matches.length > 0 || !this.transport) && cb && cb(matches);
        }
     });
}

现在我可以自由地使用异步本机代码来签名和设置请求身份验证标头:)

于 2014-05-23T07:41:11.250 回答