amazon-web-services - 如何在使用 Amazon Cognito 进行身份验证时公开访问 AWS AppSync GraphQL API?

标签 amazon-web-services amazon-iam aws-amplify aws-appsync

目前我正在使用 Amazon CognitoAWS Amplify 项目中进行身份验证,因此只有登录用户才能访问该 API。 但我希望公开访问一些 api 调用。

我该怎么做?

最佳答案

我刚刚解决了这个完全相同的问题。这就是我所做的:

  1. 通过运行 amplify update auth 更新您的 API 并选择 IAM 作为您的用户处理程序(其他一切都使用默认设置)

  2. 登录到您的 AWS 控制台 -> Appsync 并修改对 IAM(而不是 Cognito 池)的访问权限

  3. 转到 IAM 控制台并为 AUTH 和 UNAUTH 用户创建 IAM 策略(通过键入您的 Appsync 应用的名称在列表中搜索它们)

找到 AUTH 用户并附上以下政策(用您的信息更新它):

授权用户

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "appsync:GraphQL",
            "Resource": [
                "arn:aws:appsync:<AWS region>:<AWS account ID>:apis/<app sync endpoint ID>/*"
            ]
        }
    ]
}

找到未经授权的用户并附上以下政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "appsync:GraphQL",
            "Resource": [
                "arn:aws:appsync:<AWS region>:<AWS account ID>:apis/<app sync endpoint ID>/types/Query/fields/<your Query name>",
        "arn:aws:appsync:<AWS region>:<AWS account ID>:apis/<app sync endpoint ID>/types/Query/fields/<your Query name>",
        "arn:aws:appsync:<AWS region>:<AWS account ID>:apis/<app sync endpoint ID>/types/Query/fields/<your Query name>"

            ]
        }
    ]
}
  1. 现在没有记录的事情(人们从 Cognito Pools 过渡到 IAM)你需要导入 {AUTH_TYPE}

从“aws-appsync”导入 AWSAppSyncClient,{AUTH_TYPE};

并使用它在 AppSync 初始化中加载凭据

const client = new AWSAppSyncClient(
  {
    disableOffline: true,
    url: aws_config.aws_appsync_graphqlEndpoint,
    region: aws_config.aws_cognito_region,
    auth: {
      // IAM
      type: AUTH_TYPE.AWS_IAM,
      credentials: () => Auth.currentCredentials(),
    });

希望这对您有所帮助。

关于amazon-web-services - 如何在使用 Amazon Cognito 进行身份验证时公开访问 AWS AppSync GraphQL API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55593963/

相关文章:

scala - 将 aws 凭据配置文件与 spark scala 应用程序一起使用

python - 给出基本的 boto create_qualification_type 示例

amazon-web-services - 无服务器错误 : The security token included in the request is invalid

reactjs - 使用来自 React Native webView 的 Web 应用程序自动进行身份验证

cookies - AWS Cognito cookie 存储

amazon-web-services - 仅当实体尚不存在时,我可以在 CloudFormation 中设置属性吗?

swift - DynamoDB Swift 表扫描映射类

amazon-iam - 有没有办法为不活动的 aws 用户设置警报

amazon-web-services - 以编程方式检查信任关系

amazon-web-services - AWS Amplify React GET 请求错误 - 缺少身份验证 token