gpt4 book ai didi

amazon-web-services - AWS API Gateway 返回带有 x-amzn-ErrorType :AccessDeniedException header 的 403

转载 作者:行者123 更新时间:2023-12-03 18:15:10 26 4
gpt4 key购买 nike

上下文

我有一个使用 AWS 三重奏构建的无服务器 Web 应用程序:API Gateway + Lambda + DynamoDB。
应用程序的第一页发送 2 个 http 请求以获取数据并组合响应以打印结果。

            / APIGateway /projects      => Lambda (DynamoDB request)   \
web app => { } => display data
\ APIGateway /organizations => Lambda (Github API request) /

问题

我第一次要求页面时一切正常。但是,如果我尝试刷新此页面,则会在 403 请求中收到 /organizations 错误。从那里,如果我等待大约 3-5 分钟并再次刷新页面,我会正确地看到所有数据并且我的页面完美显示。有时, GET /organizations 失败,有时,是 GET /projects

为什么我必须等待才能正确刷新页面?我该如何解决?

附加信息

当我遇到这个 403 错误时,...
  • 我收到 200 OKOPTIONS /organizations 请求的响应
  • 我在 cloudwatch 日志中根本看不到 GET /organizations 请求(有 OPTIONS 请求,但没有 GET )
  • 响应头是:
    Connection:keep-alive
    Content-Length:60
    Content-Type:application/json
    Date:Mon, 12 Dec 2016 11:31:29 GMT
    Via:1.1 af2dd53407c5eae7ddf5c44e27a5dd1b.cloudfront.net (CloudFront)
    X-Amz-Cf-Id:Kl1JS1ZCCJuGAmydXv3qDoq3E-rFZrlZbs8Bgvw8RSrb8xsjEyAJNQ==
    x-amzn-ErrorType:AccessDeniedException
    x-amzn-RequestId:8689b86c-c05e-11e6-b851-7ff70d9a3125
    X-Cache:Error from cloudfront

    (我不知道为什么我有 x-amzn-ErrorType:AccessDeniedExceptionX-Cache:Error from cloudfront )
  • 我的 chrome 控制台中有一个 CORS 错误:
    XMLHttpRequest cannot load https://********.execute-api.eu-central-1.amazonaws.com/prod/organizations. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.

    (...即使启用了 CORS 并且 Access-Control-Allow-Origin:* 存在于 OPTIONS 响应 header 中)

  • 谢谢你的帮助!

    最佳答案

    此问题的根源是自定义授权方为特定资源生成自定义策略。由于此策略已缓存,因此当第二个请求到达时,它与先前生成的请求不匹配并返回错误。

    解决方法:禁用缓存

  • 在 API Gateway 服务上打开 AWS 控制台,
  • 单击您的 API,
  • 在左侧 Pane 中选择 Authorizers 并选择您的自定义授权方

  • Result TTL in seconds 中,输入 0 并单击 更新 。现在,您必须部署 API 以发布更改:
  • 单击左 Pane 中的 Ressources 菜单,然后在 Actions 下拉菜单中单击 Deploy API
  • 选择舞台并点击 部署

  • 解决方案:改进您的策略
    def generatePolicy(principalId, context):
    return {
    'principalId': principalId,
    'policyDocument': {
    'Version': '2012-10-17',
    'Statement': [{
    'Action': 'execute-api:Invoke',
    'Effect': 'Allow',
    'Resource': 'arn:aws:execute-api:eu-central-1:123456789012:abcdefghij/prod/*'
    }]
    },
    'context': context
    }

    您可以使用数组在 资源 属性中更精确:
    'Resource': [
    'arn:aws:execute-api:eu-central-1:123456789012:abcdefghij/prod/GET/projects',
    'arn:aws:execute-api:eu-central-1:123456789012:abcdefghij/prod/GET/user',
    ...
    ]

    您可以允许资源超集和拒绝特定资源。

    Have a look on @Jeremiah 's link : https://forums.aws.amazon.com/thread.jspa?threadID=225934&tstart=0

    关于amazon-web-services - AWS API Gateway 返回带有 x-amzn-ErrorType :AccessDeniedException header 的 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100859/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com