【问题标题】:Getting 401 Unauthorized from AWS Cognito + API Gateway when accessing from Postman or cURL从 Postman 或 cURL 访问时从 AWS Cognito + API Gateway 获取 401 Unauthorized
【发布时间】:2022-12-30 00:09:04
【问题描述】:

我正在尝试使用 AWS Cognito 提供的令牌通过 Postman 或 cURL 访问 URL,但我没有这样做。

我使用下面的 CloudFormation 模板创建了一个带有 JWT 身份验证的 API。

https://github.com/awsdocs/amazon-api-gateway-developer-guide/blob/main/cloudformation-templates/HTTP/http-with-jwt-auth.yaml

登录后,我可以使用返回的 URL 和 access_token 访问 lambda 函数。这按预期工作:

http://<api_url>/?access_token=<token>

但是当我尝试使用标头中的 access_token 从 Postman 或 cURL 访问它时,它输出 401。我期望获得访问权限。

$ curl -v -X GET <url> -H "Authorization: <token>"
{"message":"Unauthorized"}

我试过什么:

  • 我尝试添加“Content-Type: application/json”,但仍然收到 401。
  • 我已经尝试使用Authorization: Bearer &lt;token&gt;,但仍然得到 401。
  • 这个模板只返回access_token,但我有的另一个堆栈也返回id_token,两者都返回401
  • 的 完整的返回标头是:
HTTP/2 401
date: Thu, 03 Mar 2022 20:12:58 GMT
content-type: application/json
content-length: 26
www-authenticate: Bearer
apigw-requestid: ObIjqhmPIAMEJtA=
* Connection #0 to host <url> left intact
{"message":"Unauthorized"}

【问题讨论】:

    标签: amazon-web-services authentication aws-api-gateway amazon-cognito


    【解决方案1】:

    JWT 授权方配置为:

    JWTAuthorizer:
        Type: AWS::ApiGatewayV2::Authorizer
        Properties: 
          ApiId: !Ref MyAPI
          AuthorizerType: JWT
          IdentitySource: 
            - '$request.querystring.access_token'
          JwtConfiguration: 
            Audience: 
            - !Ref AppClient
            Issuer: !Sub https://cognito-idp.${AWS::Region}.amazonaws.com/${UserPool}
          Name: test-jwt-authorizer
    

    IdentitySource 必须是“$request.header.Authorization”才能从 header.Authorization 中读取。

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 2019-05-11
      • 2017-06-27
      • 2018-03-02
      • 1970-01-01
      • 2019-03-16
      • 2020-11-20
      • 2022-10-03
      相关资源
      最近更新 更多