【问题标题】:AWS Cognito GetCredentialsForIdentityAWS Cognito GetCredentialsForIdentity
【发布时间】:2017-11-01 15:33:51
【问题描述】:

我正在尝试使用以下博客文章中概述的方法从 Java 向 S3 验证 cognito 身份:

https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-authentication-part-4-enhanced-flow/

我已成功实现开发人员身份验证提供程序,并且可以为我的 cognito 身份检索有效的 OpenId 令牌。将文件上传/下载到 S3 时,该令牌适用于 iOS 和 Android SDK。不幸的是,我无法在我的 Java 应用程序中成功地向 S3 验证相同的身份。这是我正在使用的代码:

Map<String, String> logins = new HashMap();
logins.put("cognito-identity.amazonaws.com", cognitoOpenIdToken);
GetCredentialsForIdentityRequest getCredentialsRequest = 
  new GetCredentialsForIdentityRequest()
  .withIdentityId(cognitoIdentityId)
  .withLogins(logins);
AmazonCognitoIdentityClient cognitoIdentityClient = 
  new AmazonCognitoIdentityClient();
GetCredentialsForIdentityResult getCredentialsResult = 
  cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);

对 getCredentialsForIdentity 的调用失败并出现错误“com.amazonaws.AmazonClientException:无法从链中的任何提供商加载 AWS 凭证”。此方法被记录为不需要身份验证的公共 API。但是,我注意到,如果在系统变量中配置了有效的 IAM 凭证的环境中完成,调用会成功。它在不是这种情况的环境中失败。

我错过了一些简单的东西吗?

【问题讨论】:

    标签: java amazon-web-services amazon-cognito


    【解决方案1】:

    您是否尝试过以下列方式初始化 AmazonCognitoIdentityClient?

    新的 AmazonCognitoIdentityClient(new AnonymousAWSCredentials());

    否则,根据 AWS Java SDK 的设计,服务客户端会查找 AWS 凭证,因为同一客户端也可用于访问经过身份验证的 API。

    【讨论】:

      【解决方案2】:

      也许不是您需要成为另一个 IAM 用户...而是运行您的方法的用户在 sts:* 上“拒绝”(或您需要的其他一些操作)

      【讨论】:

      • 这是在 Lambda 上运行的吗?
      • 它没有在 Lambda 上运行。我的理解是调用此方法根本不需要 IAM 用户,它是一种公共方法(调用它不需要凭据)。
      • 同意,但您是否使用其他可能在 sts 上“拒绝”的凭证/IAM 用户?
      • 不,它在没有配置 IAM 凭证的环境中失败。
      猜你喜欢
      • 2016-08-07
      • 2022-12-20
      • 2018-12-16
      • 2021-06-23
      • 2019-03-04
      • 2016-05-02
      • 2016-08-28
      • 2018-07-30
      • 1970-01-01
      相关资源
      最近更新 更多