作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行 API-M,它调用另一个系统 (SAP) 来调用 OData 服务。 OData 服务将返回带有 URL 的响应。我不想公开 API-M 上的内部 URL,但这些策略不起作用。
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/Protocols/SAPData">
<edmx:Reference Uri="http://xxxxx.sap-internal.company.cloud/sap/opu/odata/iwfnd/catalogservice;v=2/Vocabularies(TechnicalName='%2FIWBEP%2FVOC_AGGREGATION',Version='0001',SAP__Origin='LOCAL')/$value" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
我想屏蔽http://xxxxx.sap-internal.company.cloud至http://xxxxapim.azure-api.net
基本上,我正在使用这两种策略。我还尝试过单独使用这两种策略之一
<outbound>
<base />
<find-and-replace from="xxxxx.sap-internal.company.cloud" to="xxxxapim" />
<redirect-content-urls />
</outbound>
在跟踪中,我看到了政策。
我不确定为什么它没有正确执行该策略。还有人遇到同样的事情吗?
我尝试使用 API-M 策略进行转换,但无法正常工作。
最佳答案
即使我尝试过,您使用的策略也是正确的。我有另一种方法,使用自定义脚本策略将内部 URL 替换为 OData 服务响应中的屏蔽 URL:
<outbound>
<base />
<set-body template="liquid">
@{
var response = context.Response.Body.As<string>();
var modifiedResponse = response.Replace("http://xxxxx.sap-internal.company.cloud", "http://xxxxapim.azure-api.net");
return modifiedResponse;
}
</set-body>
</outbound>
template="liquid"
允许您在 @{}
block 内使用脚本。该脚本检索响应正文,使用 Replace
方法将内部 URL 替换为屏蔽 URL,然后返回修改后的响应。我有一个示例 API 可以在 APIM 中注册,并尝试了您在上面给出的方法来屏蔽 Azure API 管理中某个前服务返回的内部 URL。
我使用函数应用程序进行了重定向,并尝试屏蔽暴露的内部 URL。
#r "Microsoft.AspNetCore.Http"
using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
// Call the OData service and retrieve the response
// ...
// Create a new response message
var response = new HttpResponseMessage(HttpStatusCode.OK);
// Modify the response using policies
response.Headers.Add("New-Header", "Value");
// Replace the internal URL in the response body with the masked URL
var responseBody = await response.Content.ReadAsStringAsync();
responseBody = responseBody.Replace("http://demo.sap-internal.company.cloud", "http://demoapim.azure-api.net");
response.Content = new StringContent(responseBody, Encoding.UTF8, "application/json");
// Return the modified response
return response;
}
输出:
替换 URL 的响应。
关于Azure API 管理查找和替换不适用于 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76608960/
我是一名优秀的程序员,十分优秀!