使用amazon登录并访问资源,如S3,EC2等

时间:2016-09-03 09:05:26

标签: java amazon-web-services authentication amazon-cognito login-with-amazon

我有一个Web应用程序,可以让用户轻松管理AWS资源,就像使用GUI启动Ec2实例一样。不同的用户可以使用此Web应用程序。我已经实现了自己的用户管理。某些用户可能不希望为我的Web应用程序创建帐户并存储其凭据以向AWS资源发出请求。它们可能不安全。所以,我想使用以下流程构建一个机制。

  1. 用户使用amazon登录
  2. 登录我的网络应用程序
  3. 使用amazon登录将为我提供访问令牌
  4. 使用此令牌获取临时凭据
  5. 将这些凭据用于将来对AWs资源的请求
  6. 通过这种方式,我的网络应用程序中不会存储永久凭证。

    我正在使用java作为我的后端服务。那么有没有AWS APi这样做呢?

    我尝试过以下代码:

     val client = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials())
        val idRequest = new GetIdRequest();
       idRequest.setAccountId(account-id);
     idRequest.setIdentityPoolId("us-east-1:xxxx");
       // If you are authenticating your users through an identity provider
        // then you can set the Map of tokens in the request
     val logins = new HashMap[String, String]()
      providerTokens+=("www.amazon.com"->"Atza|IwE-xxxxxxxxxxxxxxxxxxx")
    
      idRequest.setLogins(logins);
    
      val idResp = client.getId(idRequest)
    
     val identityId = idResp.getIdentityId()
    
    
        // Create the request object
        val tokenRequest = new GetOpenIdTokenRequest();
        tokenRequest.setIdentityId("us-east-1:xxxxxx");
    
    //
        val tokenResp = client.getOpenIdToken(tokenRequest);
    //     get the OpenID token from the response
        val openIdToken = tokenResp.getToken()
    
    //     you can now create a set of temporary, limited-privilege credentials to access
    //     your AWS resources through the Security Token Service utilizing the OpenID
    //     token returned by the previous API call. The IAM Role ARN passed to this call
    //     will be applied to the temporary credentials returned
    
    
        val stsClient = new AWSSecurityTokenServiceClient(new AnonymousAWSCredentials());
        val stsReq = new AssumeRoleWithWebIdentityRequest();
        stsReq.setRoleArn("arn:aws:iam::account-id:role/Cognito_Auth_Role");
        stsReq.setWebIdentityToken(openIdToken);
        stsReq.setRoleSessionName("AppTestSession");
        val stsResp = stsClient.assumeRoleWithWebIdentity(stsReq);
        val stsCredentials = stsResp.getCredentials();
    

    我收到以下错误:

      

    禁止访问身份'us-east-1:xxxxxxxxxxxxxxxx'。 (服务:AmazonCognitoIdentity;状态代码:400;错误代码:NotAuthorizedException

    以下是IAM角色中的Cognito_Auth_Role角色策略:

    {
      "Version": "2012-10-17",
      "Statement":[{
          "Effect":"Allow",
          "Action":"cognito-sync:*",
          "Resource":["arn:aws:cognito-sync:us-east-1:xxxxxxx:identitypool/${cognito-identity.amazonaws.com:aud}/identity/${cognito-identity.amazonaws.com:sub}/*"]
          }]
      }
    

    以下是此角色的信任关系:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "",
          "Effect": "Allow",
          "Principal": {
            "Federated": "cognito-identity.amazonaws.com"
          },
          "Action": "sts:AssumeRoleWithWebIdentity",
          "Condition": {
            "StringEquals": {
              "cognito-identity.amazonaws.com:aud": "us-east-1:xxxxx"
            },
            "ForAnyValue:StringLike": {
              "cognito-identity.amazonaws.com:amr": "www.amazon.com"
            }
          }
        }
      ]
    }
    

    请指出我,这里有什么问题?

    由于

1 个答案:

答案 0 :(得分:0)

您可以使用AWS Cognito处理大部分管道,也可以连接您自己的OpenId Identity Provider或您订阅的提供商服务。

以下是身份和访问管理(IAM)的文档: https://aws.amazon.com/documentation/iam/

或者你可以让他们为Cognito做一些管道工作: http://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html

虽然它看起来只适用于移动设备,但他们有关于在浏览器中使用JavaScript SDK的文档: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-intro.html