快速入門導覽課程:使用 Google Cloud 控制台保護傳送至服務的流量
本頁面說明如何在 API Gateway 上部署 API,確保後端服務的流量安全。
按照步驟使用 Google Cloud 控制台部署新 API,存取 Cloud Run functions 上的後端服務。本快速入門導覽課程也會說明如何使用 API 金鑰,保護後端免於未經授權的存取。
事前準備
前往 Google Cloud 控制台的「API Gateway」頁面。
API Gateway 需要啟用下列 Google 服務:
名稱 標題 apigateway.googleapis.comAPI Gateway API servicemanagement.googleapis.comService Management API servicecontrol.googleapis.comService Control API 如果您尚未為所選專案啟用這些服務,系統會提示您啟用。
確認專案已啟用計費功能。
部署 API 後端
API Gateway 位於已部署的後端服務前方,負責處理所有傳入的要求。在本快速入門導覽課程中,API Gateway 會將來電轉送至名為 helloGET 的 Cloud Run functions 後端,其中包含的函式如下:
/** * HTTP Cloud Function. * This function is exported by index.js, and is executed when * you make an HTTP request to the deployed function's endpoint. * * @param {Object} req Cloud Function request context. * More info: https://expressjs.com/en/api.html#req * @param {Object} res Cloud Function response context. * More info: https://expressjs.com/en/api.html#res */ exports.helloGET = (req, res) => { res.send('Hello World!'); };
按照「快速入門:使用 Google Cloud CLI」一文中的步驟,下載範例 Cloud Run 函式程式碼,並部署 Cloud Run 函式後端服務。
建立 API 設定
API Gateway 會使用 API 設定,將呼叫轉送至後端服務。您可以使用包含特殊擴充功能的 OpenAPI 規格,定義所選的 API Gateway 行為。如要進一步瞭解支援的 OpenAPI 擴充功能,請參閱下列說明:
本快速入門導覽課程的 OpenAPI 規格包含 Cloud Run 函式後端的轉送指示:
OpenAPI 2.0
# openapi-functions.yaml swagger: '2.0' info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 schemes: - https produces: - application/json paths: /hello: get: summary: Greet a user operationId: hello x-google-backend: address: https://GATEWAY_LOCATION -PROJECT_ID.cloudfunctions.net/helloGET responses: '200': description: A successful response schema: type: string
OpenAPI 3.x
# openapi-functions.yaml openapi: 3.0.4 info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 # Define reusable components in x-google-api-management x-google-api-management: backend: functions_backend: address: https://GATEWAY_LOCATION-PROJECT_ID.cloudfunctions.net/helloGET pathTranslation: APPEND_PATH_TO_ADDRESS protocol: "http/1.1" # Apply the backend configuration by referencing it by name. Set at the root so this applies to all operations unless overridden. x-google-backend: functions_backend paths: /hello: get: summary: Greet a user operationId: hello responses: '200': description: A successful response content: application/json: schema: type: string
使用這個 OpenAPI 規格定義 API:
從指令列建立名為
openapi-functions.yaml的新檔案。複製上例中的 OpenAPI 規格內容,並貼到新建立的檔案中。
編輯檔案,如下所示:
- 在
title欄位中,將 API_ID 替換成您的 API 名稱 (您會在下一個步驟中建立),並將 optional-string 替換成您選擇的簡要說明。這個欄位的值用於鑄造 API 金鑰,授予這個 API 的存取權。如需 API ID 命名規範,請參閱「API ID 規定」。 - 在
address欄位中,將 PROJECT_ID 替換為專案名稱,並將 GATEWAY_LOCATION 替換為部署閘道的 Google Cloud Google Cloud 。
- 在
建立閘道
現在您已準備好在 API Gateway 中建立及部署閘道。
在 Google Cloud 控制台中開啟 API Gateway 頁面。
點選「建立閘道」。
在「API」部分:
- 選擇建立新的 API,或從「選取 API」下拉式選單中選取現有 API。在本教學課程中,請選取「建立新的 API」。
- 輸入 API 的顯示名稱。
- 輸入 API 的 API ID。
- (選用) 使用「鍵:值」格式為 API 新增標籤。如要新增多個標籤,請按一下「新增標籤」並輸入其他值。
在「API 設定」部分:
- 選擇建立新的 API 設定,或從「選取設定」下拉式選單中選取現有的 API 設定。在本教學課程中,請選取「建立新的 API 設定」。
- 使用檔案瀏覽器上傳用於定義 API 的
openapi-functions.yaml。 - 輸入 API 設定的顯示名稱。
從下拉式清單中選取服務帳戶。您選取的服務帳戶會做為 API Gateway 的身分。
(選用) 使用「鍵:值」格式,將標籤新增至 API 設定。如要新增多個標籤,請按一下「新增標籤」並輸入其他值。
在「閘道詳細資料」部分:
- 輸入閘道的顯示名稱。系統會自動產生閘道網址。
- 從下拉式選單中選取閘道位置。
- (選用) 使用「鍵:值」格式為閘道新增標籤。如要新增多個標籤,請按一下「新增標籤」並輸入其他值。
點選「建立閘道」。
這會在新建的閘道上部署 API 設定。在閘道上部署 API 設定時,會定義 API 用戶端可用來存取 API 的外部網址。
這項作業可能需要幾分鐘才能完成。如要查看建立和部署程序的狀態,請點按主導覽列中的「通知」圖示,顯示狀態通知,如下圖所示:

完成後,您可以在閘道到達網頁上查看閘道詳細資料。
請記下閘道網址。您將在下一個步驟中用到這個資料夾的位置,測試部署作業。
測試 API 部署作業
現在,您可以使用部署閘道時產生的網址來傳送要求至 API。
在瀏覽器中輸入下列網址,其中:
- GATEWAY_URL 指定已部署的閘道網址。
hello是 API 設定中指定的路徑。
https://GATEWAY_URL/hello
例如:
https://my-gateway-a12bcd345e67f89g0h.uc.gateway.dev/hello
瀏覽器應會顯示 Hello World! 訊息。
您已成功建立及部署 API Gateway!
使用 API 金鑰確保存取安全
如要安全地存取 API 後端,請產生與專案相關聯的 API 金鑰,並授予該金鑰呼叫 API 的存取權。詳情請參閱「使用 API 金鑰限制 API 存取權」。
如果您還沒有與本快速入門導覽課程所用 Google Cloud 專案相關聯的 API 金鑰,請按照「建立 API 金鑰」一文中的步驟新增金鑰。
如要使用 API 金鑰確保閘道存取安全,請按照下列步驟操作:
- 為服務啟用 API 金鑰支援功能:
- 在 Google Cloud 控制台中,依序前往「APIs & Services」(API 與服務) >「Library」(程式庫)。
- 在搜尋列中,輸入剛建立的 API 的代管服務名稱。您可以在 API 登陸頁面的「Managed Service」(受管理服務) 欄中找到 API 的這個值。例如:
my-api-123abc456def1.apigateway.my-project.cloud.goog
- 在服務的到達網頁上,按一下「啟用」。
- 修改用來建立 API 設定的 OpenAPI 規格,加入對所有流量強制執行 API 金鑰驗證安全性政策的指令。新增
security類型和securitySchemes,如下所示:OpenAPI 2.0
# openapi2-functions.yaml swagger: '2.0' info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 schemes: - https produces: - application/json paths: /hello: get: summary: Greet a user operationId: hello x-google-backend: address: https://GATEWAY_LOCATION-PROJECT_ID.cloudfunctions.net/helloGET security: - api_key: [] responses: '200': description: A successful response schema: type: string securityDefinitions: # This section configures basic authentication with an API key. api_key: type: "apiKey" name: "key" in: "query"
當要求存取規格中定義的所有路徑時,
securityDefinition會將您的 API 設定為要求以名為key的查詢參數形式傳遞 API 金鑰。OpenAPI 3.x
# openapi-functions.yaml openapi: 3.0.4 info: title: API_ID optional-string description: Sample API on API Gateway with a Google Cloud Functions backend version: 1.0.0 # Define reusable components in x-google-api-management x-google-api-management: backend: functions_backend: address: https://GATEWAY_LOCATION-PROJECT_ID.cloudfunctions.net/helloGET pathTranslation: APPEND_PATH_TO_ADDRESS protocol: "http/1.1" # Apply the backend configuration by referencing it by name. Set at the root so this applies to all operations unless overridden. x-google-backend: functions_backend components: # This section configures basic authentication with an API key. securitySchemes: google_api_key: type: apiKey name: x-api-key in: header security: - google_api_key: [] paths: /hello: get: summary: Greet a user operationId: hello responses: '200': description: A successful response content: application/json: schema: type: string
當要求存取規格中定義的所有路徑時,
securitySchemes會將您的 API 設定為要求以名為key的查詢參數形式傳遞 API 金鑰。 - 建立新的 API 設定並部署至現有閘道:
- 前往「閘道」到達網頁。
- 從清單中選取閘道,即可查看詳細資料。
- 按一下「編輯」,開啟閘道設定窗格。
- 在「API 設定」部分:
- 從下拉式選單中選取「建立新的 API 設定」。
- 使用檔案瀏覽器上傳修改後的 OpenAPI 規格。
- 輸入新 API 設定的顯示名稱。
- 從下拉式清單中選取服務帳戶。您選取的服務帳戶會做為 API Gateway 的身分。
- (選用) 使用鍵/值格式,將標籤新增至 API 設定。如要新增多個標籤,請按一下「新增標籤」,然後輸入其他值。
- 按一下「Update」。
測試 API 金鑰
建立並部署修改後的 API 之後,請嘗試提出要求。
在瀏覽器中輸入下列網址,其中:
- GATEWAY_URL 指定已部署的閘道網址。
hello是 API 設定中指定的路徑。
https://GATEWAY_URL/hello
這應該會產生下列錯誤:
UNAUTHENTICATED:Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.
現在,請在瀏覽器中輸入下列網址,其中:
- GATEWAY_URL 指定已部署的閘道網址。
hello是 API 設定中指定的路徑。- API_KEY 指定您在「使用 API 金鑰確保存取安全」中建立的 API 金鑰。
https://GATEWAY_URL/hello?key=API_KEY
現在瀏覽器中應該會顯示 Hello World!。
恭喜!您已成功使用 API Gateway 保護 API 後端。現在您可以產生其他 API 金鑰,開始加入新的 API 用戶端。
追蹤 API 活動
如要查看 API 活動圖表,請前往Google Cloud 控制台的「API Gateway」頁面。按一下 API,即可在「Overview」(總覽) 頁面中查看活動圖表。要求可能需要一些時間才能反映在圖表中。
在「Logs Explorer」頁面中查看您的 API 要求記錄。您可以在Google Cloud 控制台的「API Gateway」頁面中,找到「Logs Explorer」頁面的連結。
前往 API Gateway 頁面後,請按照下列步驟操作:
- 選取要查看的 API。
- 按一下 [Details] (詳細資料) 分頁標籤。
- 按一下「記錄」下方的連結。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取這個快速入門導覽課程所用資源的費用,您可以:
或者,您也可以刪除 Google Cloud 本教學課程中使用的專案。
後續步驟
- 進一步瞭解 API Gateway
- 逐步完成「設定開發環境」一文的說明