如何设置或编辑 Azure API 管理策略How to set or edit Azure API Management policies

策略定义是一个 XML 文档,用于描述一个入站和出站语句序列。The policy definition is an XML document that describes a sequence of inbound and outbound statements. 可以直接在定义窗口中编辑 XML。The XML can be edited directly in the definition window. 也可以从策略窗口右侧提供的列表中选择预定义策略。You can also select a predefined policy from the list that is provided to the right of the policy window. 适用于当前范围的语句已启用并已突出显示。The statements applicable to the current scope are enabled and highlighted. 单击启用的语句会在定义视图中的光标位置添加相应的 XML。Clicking an enabled statement adds the appropriate XML at the location of the cursor in the definition view.

有关策略的详细信息,请参阅 Azure API 管理中的策略For detailed information about policies, see Policies in Azure API Management.

设置或编辑策略Set or edit a policy

若要设置或编辑策略,请执行以下步骤:To set or edit a policy, follow the following steps:

  1. https://portal.azure.cn 中登录 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

  2. 浏览到自己的 APIM 实例。Browse to your APIM instance.

  3. 单击“API”选项卡。Click the APIs tab.

    编辑策略

  4. 选择以前导入的某个 API。Select one of the APIs that you previously imported.

  5. 选择“设计”选项卡。Select the Design tab.

  6. 选择要将该策略应用到的操作。Select an operation to which you want to apply the policy. 如果想要将策略应用到所有操作,请选择“所有操作”。If you want to apply the policy to all operations, select All operations.

  7. 在“入站处理”或“出站处理”部分中选择“</>(代码编辑器)”图标。Select the </> (code editor) icon in the Inbound processing or Outbound processing section.

  8. 将所需的策略代码粘贴到某个相应的块中。Paste the desired policy code into one of the appropriate blocks.

     <policies>
          <inbound>
              <base />
          </inbound>
          <backend>
              <base />
          </backend>
          <outbound>
              <base />
          </outbound>
          <on-error>
              <base />
          </on-error>
      </policies>
    

配置范围Configure scope

可在全局范围或者在“产品”、“API”或“操作”范围内配置策略。Policies can be configured globally or at the scope of a Product, API, or Operation. 若要开始配置策略,必须首先选择该策略将应用的范围。To begin configuring a policy, you must first select the scope at which the policy should apply.

策略范围按以下顺序计算:Policy scopes are evaluated in the following order:

  1. 全局范围Global scope
  2. 产品范围Product scope
  3. API 范围API scope
  4. 操作范围Operation scope

策略中的语句按 base 元素(如果存在)的位置计算。The statements within policies are evaluated according to the placement of the base element, if it is present. 全局策略没有父策略,因此在其中使用 <base> 元素无效。Global policy has no parent policy and using the <base> element in it has no effect.

若要在策略编辑器中查看当前范围的策略,请单击“重新计算所选范围的有效策略”。To see the policies in the current scope in the policy editor, click Recalculate effective policy for selected scope.

全局范围Global scope

全局范围是针对 APIM 实例中的所有 API 配置的。Global scope is configured for All APIs in your APIM instance.

  1. 登录到 Azure 门户,并导航到 APIM 实例。Sign in to the Azure portal and navigate to your APIM instance.

  2. 单击“所有 API”。Click All APIs.

    全局范围

  3. 单击三角形图标。Click the triangle icon.

  4. 选择“代码编辑器”。Select Code editor.

  5. 添加或编辑策略。Add or edit policies.

  6. 按“保存” 。Press Save.

    所做的更改会立即传播到 API 管理网关。The changes are propagated to the API Management gateway immediately.

产品范围Product scope

产品范围是针对选定的产品配置的。Product scope is configured for the selected product.

  1. 单击“产品”。Click Products.

    产品范围

  2. 选择要将策略应用到的产品。Select the product to which you want to apply policies.

  3. 单击“策略”。Click Policies.

  4. 添加或编辑策略。Add or edit policies.

  5. 按“保存” 。Press Save.

API 范围API scope

API 范围是针对选定 API 的所有操作配置的。API scope is configured for All Operations of the selected API.

  1. 选择要将策略应用到的 APISelect the API you want to apply policies to.

    API 范围

  2. 选择“所有操作”Select All operations

  3. 单击三角形图标。Click the triangle icon.

  4. 选择“代码编辑器”。Select Code editor.

  5. 添加或编辑策略。Add or edit policies.

  6. 按“保存” 。Press Save.

操作范围Operation scope

操作范围是针对选定的操作配置的。Operation scope is configured for the selected operation.

  1. 选择一个 APISelect an API.

  2. 选择要将策略应用到的操作。Select the operation you want to apply policies to.

    操作范围

  3. 单击三角形图标。Click the triangle icon.

  4. 选择“代码编辑器”。Select Code editor.

  5. 添加或编辑策略。Add or edit policies.

  6. 按“保存” 。Press Save.

后续步骤Next steps

请参阅以下相关主题:See the following related topics: