从Postman或cURL访问时,从AWS Cognito+API网关获得401未经授权



我正试图使用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"}

我试过什么:

  • 我已经尝试添加"内容类型:application/json",但仍然得到401
  • 我试过使用Authorization: Bearer <token>,但仍然获得401
  • 这个模板只返回access_token,但我的另一个堆栈也返回id_token
  • 完整的返回标头为:
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授权程序配置为:

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",才能从标头中读取。批准

最新更新