通过无服务器设置API网关的请求验证器

时间:2019-03-14 06:49:13

标签: swagger aws-api-gateway serverless-framework serverless aws-serverless

我想通过无服务器设置API网关的请求验证器。我为请求验证程序尝试了两种不同的设置。但是,这两种方法都失败了。我已经总结了我所做的事情,所以请告诉我是否有问题。

  1. 我用swagger(OAS3.0)编写API规范。因此,我尝试使用OAS扩展实现请求验证程序的设置。在使用sls deployswagger.yaml下面的serverless.yml之后,x-amazon-apigateway-request-validators中描述的所有验证模式均未添加到“请求验证器”选项中。 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-request-validator.html

enter image description here

swagger.yaml在下面:

openapi: 3.0.0

info:
  description: xxx
  version: '0.1'
  title: xxx API
x-amazon-apigateway-request-validators:
  body-only:
    validateRequestBody: true,
    validateRequestParameters: false
  except-body:
    validateRequestBody: false,
    validateRequestParameters: true
  all:
    validateRequestBody: true,
    validateRequestParameters: true
tags:
  - name: auth
    description: xxx
paths:
  /login:
    post:
      tags:
        - auth
      summary: xxx
      description: ''
      x-amazon-apigateway-request-validator: all
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
        '400':
          description: fail
          content:
            application/json
        '401':
          description: fail
          content:
            application/json
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AuthRequest'
        required: true
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
        uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:xxx-api-dev-login/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws_proxy"

我的serverless.yml在下面:

resources:
  Resources:
    RestApi :
      Type : AWS::ApiGateway::RestApi
      Properties :
        Body : ${file(./swagger.yaml)}
    LoginApiToInvokeLambda:
      Type: AWS::Lambda::Permission
      DependsOn: LoginLambdaFunction
      Properties:
        FunctionName: xxx-ext-api-dev-login
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
  1. 我尝试使用AWS::ApiGateway::RequestValidator实现请求验证程序的设置。在使用上方sls deploy和下方swagger.yaml进行serverless.yml之后,all的{​​{1}}中描述的RequestValidatorAll被添加到了请求验证器选项中。但是,请求验证程序的默认值仍为NONE。 enter image description here
severless.yml

0 个答案:

没有答案