SAP UI5 应用读取 CSRF token 的 HTTP head 请求逻辑解析
SAP UI5 應(yīng)用在發(fā)送 OData batch 請求之前,會通過下列的_createBatchRequest方法構(gòu)造 batch 請求對象:
var oBatchRequest = that._createBatchRequest(aReadRequests);該請求對象的 data 字段里,包含 batch 具體的 payload:
- Invoices?KaTeX parse error: Expected 'EOF', got '&' at position 7: skip=0&?top=100&$orderby=ShipperName%20asc
- Invoices/$count
該請求頭部字段 Accept 為 multipart/mixed:
然后通過 oWrappedBatchRequestHandle.oRequestHandle = that._submitBatchRequest(oBatchRequest, aBatchGroup, fnSuccess, fnError) 方法進(jìn)行提交。
token handling 標(biāo)志位為 true,且方法不為 POST,因此在執(zhí)行 batch 操作之前,先要獲取 CSRF token:
進(jìn)入函數(shù)refreshSecurityToken.
構(gòu)造發(fā)起 token 請求的 request 對象:
url 為:https://services.odata.org/V2/Northwind/Northwind.svc/
首先嘗試 head 請求,如果報錯,再切換成 get 請求:
// Initially try method "HEAD", error handler falls back to "GET" unless the flag forbids HEAD requestif (this.bDisableHeadRequestForToken) {mTokenRequest.request = requestToken("GET", handleGetError);} else {mTokenRequest.request = requestToken("HEAD", handleHeadError);}請求 token 的 HTTP 請求的 Content-type 設(shè)置邏輯,和標(biāo)志位 bJson 有關(guān):
request 對象:
最重要的頭部字段 x-csrf-token, 值被填充成 fetch:
function requestToken(sRequestType, fnError) {// trigger a read to the service url to fetch the tokenoRequest = that._createRequest(sUrl, "", sRequestType,that._getHeaders(undefined, true), null, null, !!bAsync);oRequest.headers["x-csrf-token"] = "Fetch";return that._request(oRequest, handleSuccess, fnError, undefined, undefined, that.getServiceMetadata());}執(zhí)行了 head 請求后,響應(yīng)的狀態(tài)碼是 200,但是 responseText 字段值是空的。
還是進(jìn)入 success callback:
使用 handler 讀取 token 請求的 response,這個 handler 支持的 content-type 類型:application/atomsvc+xml;q=0.8, application/json;odata=fullmetadata;q=0.7, application/json;q=0.5, /;q=0.1
這里因?yàn)?response.body 是空的,因此進(jìn)入不了 dispatchHandler的處理邏輯:
然后進(jìn)入 refreshToken 的 callback:
當(dāng)然是拿不到 token 的:
進(jìn)入 else 分支:
清除所有相關(guān)的 token 標(biāo)志位:
ODataModel.prototype.resetSecurityToken = function() {delete this.oSharedServiceData.securityToken;delete this.oHeaders["x-csrf-token"];delete this.pSecurityToken;};resolve 一個空的 token 給 callback:
這個 head 請求的響應(yīng)碼為 200,但是響應(yīng)頭部沒有附帶 csrf token:
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的SAP UI5 应用读取 CSRF token 的 HTTP head 请求逻辑解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leaflet图层管理,图层组
- 下一篇: java在线工具