编程语言
首页 > 编程语言> > Amazon Cognito开发人员使用Java SDK验证身份

Amazon Cognito开发人员使用Java SDK验证身份

作者:互联网

我正在尝试使用开发人员验证的Cognito身份验证Java应用程序对AWS服务的身份.这在AWS移动SDK(documentation)中非常简单,但是我似乎在Java SDK中找不到等效的类.

我遇到的主要问题是Java SDK类(例如WebIdentityFederationSessionCredentialsProvider)要求客户端代码知道所承担角色的信息.通过移动SDK,它使用为联合身份配置的角色.那就是我希望做的,但是Java SDK似乎没有为此提供支持的类.

解决方法:

杰夫(Jeff)的最后评论将我引向了答案.谢谢杰夫!

String cognitoIdentityId = "your user's identity id";
String openIdToken = "open id token for the user created on backend";

Map<String,String> logins = new HashMap<>();
logins.put("cognito-identity.amazonaws.com", openIdToken);
GetCredentialsForIdentityRequest getCredentialsRequest =
    new GetCredentialsForIdentityRequest()
    .withIdentityId(cognitoIdentityId)
    .withLogins(logins);
AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient();
GetCredentialsForIdentityResult getCredentialsResult = cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
Credentials credentials = getCredentialsResult.getCredentials();
AWSSessionCredentials sessionCredentials = new BasicSessionCredentials(
    credentials.getAccessKeyId(),
    credentials.getSecretKey(),
    credentials.getSessionToken()
);

AmazonS3Client s3Client = new AmazonS3Client(sessionCredentials);
...

标签:amazon-web-services,amazon-cognito,java
来源: https://codeday.me/bug/20191118/2027867.html