本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
允許未經驗證的訪客使用 Amazon Cognito 存取您的應用程式
您可以使用 Amazon Cognito 身份驗證作為直接將 AWS Identity and Access Management (IAM) 與前端開發套件和直接 HTTPS 請求搭配使用的替代方法。
您可能基於以下原因想要使用這種身份驗證形式:
-
未經驗證的使用者 — 如果您擁有匿名使用者的網站,您可以使用 Amazon Cognito 身分集區。如需詳細資訊,請參閱(詳見)一節允許未經驗證的訪客使用 Amazon Cognito 存取您的應用程式。
-
您自己的身份驗證 — 如果您想要使用自己的身份驗證程序,或結合多種身份驗證方法,可以使用 Amazon Cognito 聯合身分。如需詳細資訊,請參閱 Amazon Cognito 開發人員指南中的聯合身分入門。
Amazon Cognito 為網頁和行動應用程式提供身份驗證、授權和使用者管理功能。您可以將 Amazon Cognito 未經驗證的身分集區與 Amazon 位置搭配使用,作為應用程式擷取臨時、速 AWS 度較低的登入資料的一種方式。
如需詳細資訊,請參閱 Amazon Cognito 開發人員指南中的使用者集區入門。
注意
對於行動開發人員,Amazon 位置提供適用於 iOS 和 Android 的行動驗證 SDK,請參閱下列 github 儲存庫以取得詳細資訊:
建立 Amazon Cognito 身分集區
您可以建立 Amazon Cognito 身分集區,以允許未經驗證的訪客透過 Amazon Cognito 主控台、或 Amazon Cognito API 存取 AWS CLI您的應用程式。
重要
您建立的集 AWS 區必須 AWS 帳戶 與您正在使用的 Amazon 定位服務資源位於相同的區域。
您可以透過下列動作使用與未驗證身分角色相關聯的 IAM 政策:
-
geo:GetMap*
-
geo:SearchPlaceIndex*
-
geo:GetPlace
-
geo:CalculateRoute*
-
geo:GetGeofence
-
geo:ListGeofences
-
geo:PutGeofence
-
geo:BatchDeleteGeofence
-
geo:BatchPutGeofence
-
geo:BatchEvaluateGeofences
-
geo:GetDevicePosition*
-
geo:ListDevicePositions
-
geo:BatchDeleteDevicePositionHistory
-
geo:BatchGetDevicePosition
-
geo:BatchUpdateDevicePosition
包括其他 Amazon 位置動作將不會產生任何作用,且未經驗證的身分將無法呼叫它們。
使用 Amazon Cognito 主控台建立身分集區
-
選擇 Manage Identity Pools (管理身分集區)。
-
選擇 [建立新身分集區],然後輸入身分集區的名稱。
-
從 [未驗證的身分可收合] 區段中,選擇 [啟用存取未驗證身分]。
-
選擇 Create Pool (建立集區)。
-
選擇要搭配身分集區使用的 IAM 角色。
-
展開檢視詳細資料。
-
在「未驗證的身分」下,輸入角色名稱。
-
展開「檢視原則文件」區段,然後選擇 「編輯」以新增原則。
-
新增您的政策以授與資源的存取權。
以下是「地圖」、「地點」、「追蹤器」和「路線」的政策範例。若要將範例用於您自己的政策,請取代
區域
和accountID
預留位置符號:注意
雖然未驗證身分集區的目的是在不安全的網際網路網站上曝光,但請注意,這些身分集區會被交換成標準、有時間限制 AWS 的認證。
適當範圍與未驗證身分集區相關聯的 IAM 角色非常重要。
-
選擇 [允許] 建立您的身分集區。
產生的身分集區遵循語法 <
region
>:<GUID
>.
例如:
us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef
如需 Amazon 位置特定的更多政策範例,請參閱Amazon 定 Location Service 的身分識別政策範例。
在中使用 Amazon Cognito 身份集區 JavaScript
下列範例會交換您為憑證建立的未驗證身分集區,然後用來擷取地圖資源ExampleMap
的樣式描述元。
const AWS = require("aws-sdk"); const credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: "<identity pool ID>" // for example, us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef }); const client = new AWS.Location({ credentials, region: AWS.config.region || "<region>" }); console.log(await client.getMapStyleDescriptor("
ExampleMap
").promise());
注意
從未驗證身分擷取的認證有效期為一小時。
以下是在認證過期之前自動更新的函數範例。
async function refreshCredentials() { await credentials.refreshPromise(); // schedule the next credential refresh when they're about to expire setTimeout(refreshCredentials, credentials.expireTime - new Date()); }
為了簡化這項工作,您可以使用 Amazon 位置JavaScript 驗證助手。這代替了獲取憑據並刷新它們。此範例使用第三 AWS 版的 JavaScript SDK。
import { LocationClient, GetMapStyleDescriptorCommand } from "@aws-sdk/client-location"; import { withIdentityPoolId } from "@aws/amazon-location-utilities-auth-helper"; const identityPoolId = "<identity pool ID>"; // for example, us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef // Create an authentication helper instance using credentials from Cognito const authHelper = await withIdentityPoolId(identityPoolId); const client = new LocationClient({ region: "<region>", // The region containing both the identity pool and tracker resource ...authHelper.getLocationClientConfig(), // Provides configuration required to make requests to Amazon Location }); const input = { MapName: "ExampleMap", }; const command = new GetMapStyleDescriptorCommand(input); console.log(await client.send(command));
後續步驟
-
若要修改您的角色,請前往 IAM 主控台
。 -
若要管理您的身分集區,請前往 Amazon Cognito 主控台
。