API 管理原則範例API Management policy samples

原則是系統的強大功能,可讓發行者透過設定來變更 API 的行為。Policies are a powerful capability of the system that allows the publisher to change the behavior of the API through configuration. 原則是陳述式的集合,會因 API 的要求或回應循序執行。Policies are a collection of statements that are executed sequentially on the request or response of an API. 下表包含範例的連結,並提供每個範例的簡短描述。The following table includes links to samples and gives a brief description of each sample.

輸入原則Inbound policies
新增 Forwarded 標頭以允許後端 API 建構適當的 URLAdd a Forwarded header to allow the backend API to construct proper URLs 示範如何在輸入要求中新增 Forwarded 標頭,以允許後端 API 建構適當的 URL。Demonstrates how to add a Forwarded header in the inbound request to allow the backend API to construct proper URLs.
新增包含相互關聯識別碼的標頭Add a header containing a correlation id 示範如何將包含相互關聯識別碼的標頭新增至輸入要求。Demonstrates how to add a header containing a correlation ID to the inbound request.
將功能新增至後端服務並快取回應Add capabilities to a backend service and cache the response 示範如何將功能新增至後端服務。Shows how to add capabilities to a backend service. 例如,在天氣預報 API 中接受地點的名稱,而不是緯度和經度。For example, accept a name of the place instead of latitude and longitude in a weather forecast API.
根據 JWT 宣告授權存取Authorize access based on JWT claims 示範如何在以 JWT 宣告為基礎的 API 上授權存取特定的 HTTP 方法。Shows how to authorize access to specific HTTP methods on an API based on JWT claims.
使用外部授權者授權要求Authorize requests using external authorizer 示範如何使用外部授權者來保護 API 存取。Shows how to use external authorizer for securing API access.
使用 Google OAuth 權杖授權存取Authorize access using Google OAuth token 示範如何使用 Google 作為 OAuth 權杖提供者來授權存取您的端點。Shows how to authorize access to your endpoints using Google as an OAuth token provider.
產生共用存取簽章並將要求轉送到 Azure 儲存體Generate Shared Access Signature and forward request to Azure storage 示範如何使用運算式來產生共用存取簽章,並使用 rewrite-uri 原則來將要求轉送到 Azure 儲存體。Shows how to generate Shared Access Signature using expressions and forward the request to Azure storage with rewrite-uri policy.
從 AAD 取得 OAuth2 存取權杖並將它轉送到後端Get OAuth2 access token from AAD and forward it to the backend 提供使用 OAuth2 在閘道與後端之間進行授權的範例。Provides and example of using OAuth2 for authorization between the gateway and a backend. 此範例示範如何從 AAD 取得存取權杖並將它轉送到後端。It shows how to obtain an access token from AAD and forward it to the backend.
使用傳送要求原則從 SAP 閘道取得 X-CSRF 權杖Get X-CSRF token from SAP gateway using send request policy 示範如何實作許多 API 所使用的 X-CSRF 模式。Shows how to implement X-CSRF pattern used by many APIs. 這是 SAP 閘道特有的範例。This example is specific to SAP Gateway.
根據要求主體的大小進行要求的路由傳送Route the request based on the size of its body 示範如何根據要求主體的大小進行要求的路由傳送。Demonstrates how to route requests based on the size of their bodies.
將要求內容資訊傳送到後端服務Send request context information to the backend service 示範如何將一些內容資訊傳送到後端服務以進行記錄或處理。Shows how to send some context information to the backend service for logging or processing.
設定回應快取持續時間Set response cache duration 示範如何使用後端所傳送之 Cache-Control 標頭中的 maxAge 值來設定回應快取持續時間。Demonstrates how to set response cache duration using maxAge value in Cache-Control header sent by the backend.
輸出原則Outbound policies
篩選回應內容Filter response content 示範如何根據與要求相關聯的產品,從回應承載篩選資料元素。Demonstrates how to filter data elements from the response payload based on the product associated with the request.
錯誤時的原則On-error policies
將錯誤記錄到 StackifyLog errors to Stackify 示範如何新增錯誤記錄原則,以將錯誤傳送到 Stackify 進行記錄。Shows how to add an error logging policy to send errors to Stackify for logging.