繁体   English   中英

从 Postman 或 cURL 访问时从 AWS Cognito + API 网关获取 401 Unauthorized

[英]Getting 401 Unauthorized from AWS Cognito + API Gateway when accessing from Postman or cURL

我正在尝试使用 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 function。 这按预期工作:

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

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

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

我试过什么:

  • 我尝试添加“Content-Type: application/json”,但仍然收到 401。
  • 我尝试使用Authorization: Bearer <token> ,但仍然得到 401。
  • 此模板仅返回 access_token,但我的另一个堆栈也返回 id_token,两者都返回 401
  • 完整返回的 header 是:
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"}

JWT Authorizer 配置为:

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 中读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM