练习 - 使用转换策略屏蔽 URL

已完成

如果托管的 API 在响应中包含其原始 URL,则客户端可以通过直接连接到 API 来绕过 API 管理策略。 可以通过屏蔽 API 响应中的原始 URL 来防止这种情况。

人口普查 API 示例以这种方式包含其 URL。 你希望确保所有请求都会通过(而不会绕过)API 管理。

在此单元中,你将了解如何屏蔽人口普查 API 正文中返回的某些数据。 使用转换策略来重写在响应正文中公开的 URL 数据。

屏蔽 URL

以下练习演示如何在 API 管理中应用屏蔽 URL 转换策略。

Screenshot of the Design tab with the Policies icon highlighted in the Outbound processing section.

  1. 使用在上一个练习中所用的帐户登录到 Azure 门户

  2. 在 Azure 门户菜单或“主页”上,选择“所有资源”,然后选择 API 管理实例。

  3. 在左侧菜单窗格的“API”下,选择“API”,然后在中间窗格中,选择“人口普查数据”。

  4. 在顶部菜单栏,选择“设计”选项卡,然后选择“所有操作”。

  5. 在“出站”处理部分,选择 </> 图标。 此时将显示 XML 策略编辑器。

  6. <outbound> 元素中,在之前添加的 <set-header-name> 元素下面添加以下元素:

    <redirect-content-urls />
    
  7. 选择“保存”。

测试新策略

我们可以使用 Azure 门户中的测试工具来检查策略的行为是否符合要求:

  1. 再次选择“人口普查数据”,然后在顶部菜单栏中,选择“测试”选项卡。

  2. 选择“GetLatestCensus”,然后选择“发送”。

    Screenshot of the test screen for the GetLatestCensus operation of the Census Data API. The Send button is highlighted.

现在应会看到屏蔽 URL 转换策略如何将响应正文中的链接更改为 API 管理终结点。 你应在 URL 中看到 API 管理实例的名称。

Screenshot of an HTTP response with the href value highlighted, showing that the URL has been masked.