【问题标题】:AWS API Gateway: User anonymous is not authorized to execute APIAWS API Gateway:用户匿名无权执行 API
【发布时间】:2019-03-31 15:36:09
【问题描述】:

尝试发布到我在 API 网关中创建的 API:

{
    "Message": "User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:us-west-2:***********:jrr7u1ekrr/v0/POST/user"
}

如何更新 CloudFormation 中的策略以公开发布 POST 端点?我用AWS::ApiGateway::RestApi 资源类型声明API。

API 策略属性是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "execute-api:/*/POST/user"
        }
    ]
} 

【问题讨论】:

  • 仅供参考,如果您看到此内容,您需要更新 API 网关上的资源策略
  • 谢谢!这是我的问题。现在修好了。再次感谢

标签: amazon-web-services amazon-cloudformation aws-api-gateway


【解决方案1】:

问题可能出在方法声明部分。您需要在 AWS::ApiGateway::Method 声明中将 authorizationType 设置为 NONE

【讨论】:

  • 感谢 roxxypoxxy,我确实将方法 auth 设置为 none: `` "Type": "AWS::ApiGateway::Method", "Properties": { ... "AuthorizationType": “无”,```
  • 现在我看到了您编辑的帖子,您可以尝试按照此处docs.aws.amazon.com/apigateway/latest/developerguide/… 记录的方式调整资源。您拥有的资源声明似乎不遵循文档中提到的模式
  • 你错了。此错误消息与方法授权无关。
【解决方案2】:

让我不解的是:“如果 API 之前已在 API Gateway 控制台中部署,您需要重新部署它才能使资源策略生效。”

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-resource-policies-create-attach.html

【讨论】:

  • 重新部署后,我的 API 可用。这个解决方案对我有用。此答案应标记为正确答案。
  • 另外,在部署按钮被按下后,它似乎可能需要几秒钟才能生效。
  • aws 应该在有更改配置选项的地方提供部署按钮,这样用户就不会花费数小时来弄清楚为什么某些东西不工作
【解决方案3】:

策略更改后,您需要重新部署应用程序以传播更改。 重新部署 - 1.转到API网关。 2. 转到资源。 3. 单击操作下拉菜单。单击部署 API。

【讨论】:

    【解决方案4】:

    "Resource": "execute-api:/*/POST/user"
    

    设置您的Account ID 而不是*

    然后重新部署。

    氪,

    【讨论】:

    • 我工作的另一个团队使用通配符 进行了类似的设置,并且适用于他们。像这样 - "Resource": !Join ["", ["execute-api:/", !Ref EnvironmentSuffix, "/"]],
    【解决方案5】:

    即使您的 OPTIONS 方法的 Authorization 设置为 NONE,它也会检查资源策略(如果有的话)。

    您可以通过设置以下 API 网关资源策略来公开您的 OPTIONS 方法。

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "AWS": "*"
                },
                "Action": "execute-api:Invoke",
                "Resource": "arn:aws:execute-api:{REGION}:{AWS_ACCOUNT}:{YOUR_API_ID}/{YOUR_API_STAGE}/OPTIONS/*"
            }
        ]
    }
    

    检查How API Gateway Resource Policies Affect Authorization Workflow

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-23
      • 2019-06-21
      • 2020-04-04
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多