簡體   English   中英

Api Gateway Cognito 授權方:客戶端令牌適用於 AWS ui 但不適用於 Postman

[英]Api Gateway Cognito Authorizer: client token works on AWS ui but not on Postman

我使用無服務器框架設置了一些 POST api,然后編寫了一個原始的 NextJs 應用程序來嘗試使用 Cognito 進行身份驗證,然后使用受授權方保護的 api。

  1. 我驗證了 api,它在沒有授權者的情況下工作。 (在郵遞員中測試)
  2. 我正在向控制台打印從 cognito 收到的 access_token 和 id_token。
  3. id_token 在 aws 上通過了基於 UI 的 Authorizer 測試
  4. 我在前端應用程序和 Postman 上的請求都失敗了,盡管包括授權 header 和令牌(嘗試了兩個令牌)。
  5. 我還嘗試在 Aws UI 上手動啟用 CORS,但仍然沒有成功。

我也看過這篇文章 ( https://www.serverless.com/blog/cors-api-gateway-survival-guide/ ) 但沒有運氣,此時我不知道該怎么做。

我的 serverless.yml 的一部分:

custom:
  allowedHeaders:
    - Accept
    - Content-Type
    - Content-Length
    - Authorization
    - X-Amz-Date
    - X-Api-Key
    - X-Amz-Security-Token
    - X-Amz-User-Agent
    - app_user_id
    - app_user_name
...
createResource:
    handler: api/${name}.handler
    description: POST /${name}
    events:
      - http:
          path: api/${name}
          method: post
          authorizer:
            type: COGNITO_USER_POOLS
            authorizerId:
              Ref: ApiGatewayCognitoAuthorizer
            scopes:
              - email
          cors:
            origin: '*'
            headers: ${self:custom.allowedHeaders}
          reqValidatorName: ${name}RequestValidator
          integration: lambda
          request:
            template:
              application/json: ${file(api/resource/mappingTemplates/${name}RequestMappingTemplate.vm)}
            passThrough: WHEN_NO_MATCH
...
ApiGatewayCognitoAuthorizer:
      Type: AWS::ApiGateway::Authorizer
      Properties:
        Name: CognitoUserPool-${self:provider.environment.VERSION}
        Type: COGNITO_USER_POOLS
        IdentitySource: method.request.header.Authorization
        RestApiId:
          Ref: ApiGatewayRestApi
        ProviderARNs:
          - ${self:custom.settings.cognitoARN-${env:ENV_NAME}}

經過一系列嘗試,我解決了這個問題。 我發現這篇文章非常相似:stackoverflow.com/questions/46345005/… 對我有用的解決方案是將“openid”添加到 api 的 oauth 范圍,而不僅僅是“電子郵件”。 然后我在 Postman 和瀏覽器上嘗試了 access_token 而不是 id_token 和 hallelujah

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM