Amazon Cognoto开发者使用Java SDK验证身份

Amazon Cognoto开发者使用Java SDK验证身份,java,amazon-web-services,amazon-cognito,Java,Amazon Web Services,Amazon Cognito,我正在尝试使用开发人员认证的Cognito标识对AWS服务的java应用程序进行认证。这在AWS mobile SDK()中非常简单,但在Java SDK中似乎找不到等效的类 我面临的主要问题是,Java SDK类(如WebIdentityFederationSessionRedentialsProvider)要求客户端代码了解所承担角色的arn。对于移动SDK,它使用为联邦身份配置的角色。这是我更愿意做的,但Java SDK似乎没有支持这一点的类。如果这是您想要走的路线,您可以在IAM控制台中

我正在尝试使用开发人员认证的Cognito标识对AWS服务的java应用程序进行认证。这在AWS mobile SDK()中非常简单,但在Java SDK中似乎找不到等效的类


我面临的主要问题是,Java SDK类(如WebIdentityFederationSessionRedentialsProvider)要求客户端代码了解所承担角色的arn。对于移动SDK,它使用为联邦身份配置的角色。这是我更愿意做的,但Java SDK似乎没有支持这一点的类。

如果这是您想要走的路线,您可以在IAM控制台中找到这个角色,名为Cognito_(Auth | Unauth)u DefaultRole。这些是Cognito生成并链接到您的池的内容,您可以从中获得ARN


可能会有所帮助。SDK用于与Cognito通信以获取凭据的所有API都在JavaSDK中公开,您只需要使用自己的后端来获取令牌本身。一旦你有了它,你就可以像平常一样在其他提供商那里设置登录,一切都会正常工作。

杰夫的最后一句话让我找到了答案。谢谢杰夫

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);
...
String cognitoIdentityId=“您用户的身份id”;
String openIdToken=“为在后端创建的用户打开id令牌”;
Map logins=new HashMap();
logins.put(“cognito identity.amazonaws.com”,openIdToken);
GetCredentialsForIdentity请求getCredentialsRequest=
新GetCredentialsForIdentityRequest()
.withIdentityId(cognitoIdentityId)
.具有登录名(登录名);
AmazonCongnitoIdentityClient cognitoIdentityClient=新的AmazonCongnitoIdentityClient();
GetCredentialsForIdentityResult getCredentialsResult=cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
凭证凭证=getCredentialsResult.getCredentials();
AWSSessionCredentials sessionCredentials=新的基本会话凭据(
credentials.getAccessKeyId(),
credentials.getSecretKey(),
credentials.getSessionToken()
);
AmazonS3Client s3Client=新的AmazonS3Client(sessionCredentials);
...

对不起,那篇博文没用。它使用AssumeRoleWithWebIdentity,这要求您提前了解角色的知识。我知道您可以通过AWS控制台获得ARN,但我需要以编程方式确定它,而无需java客户端直接访问AWS帐户(java客户端没有AWS的访问密钥)。后端确实提供了进行身份验证所需的访问令牌。您能否澄清为什么需要以编程方式确定它?控制台的功能正是移动SDK中服务的功能,因此,如果您愿意这样做,它将使这成为可能。在引入简化流程之前,客户端SDK所做的是使用dev输入字符串并根据登录状态进行选择,这是您可以做的。在任何情况下,博客主要是展示如何使用凭证提供者以外的一切。在我的情况下,客户端应用程序只知道4件事:AWS帐户id、身份池id、该池中用户身份的id以及该身份的OpenId令牌。通过这4件事,我可以在移动SDK中对用户进行身份验证,但在Java SDK中似乎找不到同样的方法。在JavaSDK中,我还需要角色的ARN由Cognito标识承担。对,这就是我建议的原因。抱歉,我仍然不清楚为什么不能将IAM控制台中的角色arn放入代码中?这就是移动SDK直到最近所做的。因为ARN将根据登录到应用程序的用户而有所不同。用户可能在不同的身份池中此链接很有帮助->