SAP UI5 应用读取 CSRF token 的 HTTP head 请求逻辑解析

举报
汪子熙 发表于 2022/10/03 10:26:12 2022/10/03
【摘要】 SAP UI5 应用在发送 OData batch 请求之前,会通过下列的_createBatchRequest方法构造 batch 请求对象:var oBatchRequest = that._createBatchRequest(aReadRequests);该请求对象的 data 字段里,包含 batch 具体的 payload:Invoices?skip=0&top=100&$ord...

SAP UI5 应用在发送 OData batch 请求之前,会通过下列的_createBatchRequest方法构造 batch 请求对象:

var oBatchRequest = that._createBatchRequest(aReadRequests);

该请求对象的 data 字段里,包含 batch 具体的 payload:

  • Invoices?skip=0&top=100&$orderby=ShipperName%20asc
  • Invoices/$count

该请求头部字段 Accept 为 multipart/mixed:

然后通过 oWrappedBatchRequestHandle.oRequestHandle = that._submitBatchRequest(oBatchRequest, aBatchGroup, fnSuccess, fnError) 方法进行提交。

token handling 标志位为 true,且方法不为 POST,因此在执行 batch 操作之前,先要获取 CSRF token:

进入函数refreshSecurityToken.

构造发起 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 request
		if (this.bDisableHeadRequestForToken) {
			mTokenRequest.request = requestToken("GET", handleGetError);
		} else {
			mTokenRequest.request = requestToken("HEAD", handleHeadError);
		}

请求 token 的 HTTP 请求的 Content-type 设置逻辑,和标志位 bJson 有关:

request 对象:

最重要的头部字段 x-csrf-token, 值被填充成 fetch:

function requestToken(sRequestType, fnError) {
			// trigger a read to the service url to fetch the token
			oRequest = 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());
		}

执行了 head 请求后,响应的状态码是 200,但是 responseText 字段值是空的。

还是进入 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

这里因为 response.body 是空的,因此进入不了 dispatchHandler的处理逻辑:

然后进入 refreshToken 的 callback:

当然是拿不到 token 的:

进入 else 分支:

清除所有相关的 token 标志位:

ODataModel.prototype.resetSecurityToken = function() {
		delete this.oSharedServiceData.securityToken;
		delete this.oHeaders["x-csrf-token"];
		delete this.pSecurityToken;
	};

resolve 一个空的 token 给 callback:

这个 head 请求的响应码为 200,但是响应头部没有附带 csrf token:

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。