简体   繁体   English

AWS 自定义 Api 网关响应

[英]AWS Custom Api Gateway Response

I am currently implementing the custom authorizer for my AWS lambdas using this template https://github.com/auth0-samples/jwt-rsa-aws-custom-authorizer/blob/master/lib.js .我目前正在使用此模板https://github.com/auth0-samples/jwt-rsa-aws-custom-authorizer/blob/master/lib.js为我的 AWS lambda 实现自定义授权方。

The thing is that I want to display a custom message whenever the API Gateway response is one of the following: ACCESS_DENIED , EXPIRED_TOKEN or UNAUTHORIZED ( https://docs.aws.amazon.com/apigateway/latest/developerguide/supported-gateway-response-types.html ).问题是,只要 API 网关响应为以下之一,我想显示自定义消息: ACCESS_DENIEDEXPIRED_TOKENUNAUTHORIZEDhttps://docs.aws.amazonguide.com/apigateway-latest/developersupported/apigateway-响应类型.html )。 I assigned the context property with a custom message when generating the policy and now I need to edit my serverless.ts config file to handle these responses.我在生成策略时为上下文属性分配了一条自定义消息,现在我需要编辑我的 serverless.ts 配置文件来处理这些响应。 Previously I used the .yml file and it looked like that以前我使用.yml文件,它看起来像这样

    DenyFailureGatewayResponse:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseTemplates:
          application/json: |
            {
              "name":"$context.authorizer.name",
              "message":"$context.authorizer.message"
            }
        ResponseType: ACCESS_DENIED
        RestApiId:
          Ref: "ApiGatewayRestApi"
        StatusCode: "403"

But now I switched to the .ts file.但现在我切换到.ts文件。 How can I write similar code using typescript?如何使用 typescript 编写类似的代码?

You can write like this:你可以这样写:

"responseParameters": {
  "method.response.header.X-Powered-By": "Serverless", // a string
  "method.response.header.Warning": "integration.response.body", // the whole response
  "method.response.header.Location": "integration.response.body.some.key" // a pseudo JSON-path
},


    custom: {
    ....
      },
      resources:{
    Resources: {
      GatewayResponse: {
        Type: 'AWS::ApiGateway::GatewayResponse',
        Properties: {
          ResponseParameters:{
            "gatewayresponse.header.Access-Control-Allow-Origin": "'*'",
            "gatewayresponse.header.Access-Control-Allow-Headers": "'*'",
          },
          ResponseType: "ACCESS_DENIED",
          RestApiId:{
            Ref: 'ApiGatewayRestApi'
          }
        },
    }, 
   },
 },

ref: https://www.serverless.com/plugins/serverless-offline参考: https://www.serverless.com/plugins/serverless-offline

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

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