我正在尋找類似的解決方案,將OAuth令牌應用到我的Ext.data.proxy.Rest代理,並找不到任何有關它的信息。非常震驚,沒有什麼關於如何做到這一點。讓我覺得我正在以錯誤的方式去做。無論如何,繼承人我想出了什麼來實現這一目標。
覆蓋Ext.data.proxy.Ajax
並保留一個靜態變量,因此它適用於所有代理。然後將set authHeader與當前的標題列表合併,以便您仍可以自定義每個代理。
Ext.override(Ext.data.proxy.Ajax, {
statics: {
authHeader: undefined,
},
/**
* @cfg {Object} headers
* Any headers to add to the Ajax request. Defaults to undefined.
*/
doRequest: function(operation, callback, scope) {
var writer = this.getWriter(),
request = this.buildRequest(operation, callback, scope);
if (operation.allowWrite()) {
request = writer.write(request);
}
console.log(this.headers);
Ext.apply(request, {
headers : Ext.apply(this.headers, Ext.data.proxy.Ajax.authHeader) || Ext.data.proxy.Ajax.authHeader,
timeout : this.timeout,
scope : this,
callback : this.createRequestCallback(request, operation, callback, scope),
method : this.getMethod(request),
disableCaching: false // explicitly set it to false, ServerProxy handles caching
});
Ext.Ajax.request(request);
return request;
}
});
然後我在本地存儲中查找我的令牌,如果它在全局設置它,否則提示輸入登錄名。
MyApp.model.AuthToken.load(1, {
callback: function(record) {
// If we don't have anything in local storage for the user, show the login box.
if (record.get('access_token') == '') {
var window = Ext.create('MyApp.view.Login');
window.show();
} else {
Ext.data.proxy.Ajax.authHeader = { 'Authorization': 'Bearer ' + record.get('access_token') };
}
}
});
應該有一些額外的邏輯在那裏處理令牌過期和刷新請求,但你明白了!