使用用户池进行身份验证 - Amazon Cognito
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用用户池进行身份验证

您的应用程序用户可以通过用户池直接登录,也可以通过第三方身份提供商(IdP)联合。用户池管理处理以下令牌的开销:从通过 Facebook、Google、亚马逊和 Apple 进行的社交登录返回的令牌以及从 OpenID Connect(OIDC)和 SAML IdP 返回的令牌。

在成功验证身份后,Amazon Cognito 会将用户池令牌返回到您的应用程序。您可以使用这些令牌向您的用户授予对您的服务器端资源或 Amazon API Gateway 的访问权限。或者,您可以用它们交换用于访问其他Amazon服务的Amazon凭证。


      身份验证概述

您的 Web 或移动应用程序的用户池令牌处理和管理在客户端上通过 Amazon Cognito SDK 进行。同样的,如果存在有效的(非过期的)刷新令牌,Mobile SDK for iOS 和 Mobile SDK for Android 会自动刷新您的 ID 令牌和访问令牌,而且 ID 令牌和访问令牌剩余有效时间至少有 5 分钟。有关 SDK 以及适用于 JavaScript、Android 和 iOS 的示例代码的信息,请参阅 Amazon Cognito 用户池 SDK

在您的应用程序用户成功登录后,Amazon Cognito 会创建会话并返回经过身份验证的用户的 ID 令牌、访问令牌和刷新令牌。

JavaScript
// Amazon Cognito creates a session which includes the id, access, and refresh tokens of an authenticated user. var authenticationData = { Username : 'username', Password : 'password', }; var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData); var poolData = { UserPoolId : 'us-east-1_ExaMPle', ClientId : '1example23456789' }; var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); var userData = { Username : 'username', Pool : userPool }; var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData); cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { var accessToken = result.getAccessToken().getJwtToken(); /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */ var idToken = result.idToken.jwtToken; }, onFailure: function(err) { alert(err); }, });
Android
// Session is an object of the type CognitoUserSession, and includes the id, access, and refresh tokens for a user. String idToken = session.getIdToken().getJWTToken(); String accessToken = session.getAccessToken().getJWT();
iOS - swift
// AWSCognitoIdentityUserSession includes id, access, and refresh tokens for a user. - (AWSTask<AWSCognitoIdentityUserSession *> *)getSession;
iOS - objective-C
// AWSCognitoIdentityUserSession includes the id, access, and refresh tokens for a user. [[user getSession:@"username" password:@"password" validationData:nil scopes:nil] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) { // success, task.result has user session return nil; }];