Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
如何使用 Amazon Cognito 身份池授予用户访问 AWS 服务的权限?
我想使用 Amazon Cognito 用户群体授予用户访问 AWS 资源的权限。
解决方法
您可以将 Amazon Cognito 身份池与 Amazon Cognito 用户群体集成,以颁发用于访问 AWS 资源的临时凭证。通过使用用户群体配置身份池来设置身份验证提供商。通过用户群体对用户进行身份验证后,使用用户身份令牌通过身份验证流程从身份池检索临时凭证。有基本身份验证流程和用于检索临时凭证的增强型身份验证流程。
**重要提示:**在以下 AWS 命令行界面 (AWS CLI) 命令示例中,将所有示例字符串的实例替换为您的值。(例如,将“example_identity_pool_id”替换为您的身份池 ID,或将“abcdef”替换为您的 AWS 区域的身份 ID。)
基本流程
**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请确保您运行的是最新版本的 AWS CLI。
请执行以下步骤,使用基本身份验证流程检索临时 AWS 凭证:
1. 在旧的 Amazon Cognito 控制台中,选择 Manage Identity Pools(管理身份池)。选择您正在更新的 Amazon Cognito 身份池的名称,然后选择 Edit identity pool(编辑身份池)。在 Authentication flow settings(身份验证流程设置)下,选择 Allow Basic (Classic) Flow(允许基本(经典)流程),然后保存更改。
2. 要在身份池中生成或检索用户身份,请调用 GetId API。
get-id 命令示例:
aws cognito-identity get-id --identity-pool-id "example_identity_pool_id" --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id=example_cognito_user_id_token
输出示例:
{ "IdentityId": "region:abcdef" }
3. 要检索开放 ID 令牌,请使用步骤 2 的结果调用 GetOpenIdToken API。
get-open-id-token 命令示例:
aws cognito-identity get-open-id-token --identity-id "region:abcdef" --logins cognito-idp.example_region.amazonaws.com/example_user_pool_id=example_cognito_user_id_token
输出示例:
{ "IdentityId": "region:abcdef", "Token": "HIJKLMN" }
4. 要检索经过身份验证的角色的临时凭证,请使用步骤 3 中的令牌调用 AssumeRoleWithWebIdentity API。
assume-role-with-web-identity 命令示例:
aws sts assume-role-with-web-identity --role-arn "example_auth_role_arn_of_the_identity_pool" --web-identity-token "HIJKLMN" --role-session-name "example_session_name"
输出示例:
{ "Credentials": { AccessKeyId": "xxxxxxxxxxxx", SecretAccessKey": "xxxxxxxxxxxx", SessionToken": "xxxxxxxxxxxx", Expiration": "" }, "SubjectFromWebIdentityToken": "", "AssumedRoleUser": { "AssumedRoleId": "", "Arn": "" }, "Provider": "cognito-identity.amazonaws.com", "Audience": "" }
增强型流程
请执行以下步骤,使用增强型身份验证流程检索临时 AWS 凭证:
1. 要在 Amazon Cognito 身份池中生成或检索用户身份,请调用 GetId API。
get-id 命令示例:
aws cognito-identity get-id --identity-pool-id "example_identity_pool_id" --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id=example_cognito_user_id_token
输出示例:
{ "IdentityId": "region:abcdef" }
2. 要从身份池检索临时凭证,请调用 GetCredentialsForIdentity API。
get-credentials-for-identity 命令示例:
aws cognito-identity get-credentials-for-identity --identity-id region:abcdef --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id = example_cognito_user_id_token
输出示例:
{ "IdentityId": "region:abcdef", "Credentials": { "AccessKeyId": "xxxxxxxxxx", "SecretKey": "xxxxxxxxxx", "SessionToken": "xxxxxxxxxx", "Expiration": "" } }
要了解有关基于角色的复杂访问控制和基于属性的访问控制的更多信息,请参阅使用 Amazon Cognito 身份池进行精细访问控制。
相关信息
- 语言
- 中文 (简体)
