swagger 将标头硬编码到AWS API网关中

vhipe2zx  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(169)

我们有一个API网关,移动的应用程序请求在到达我们的核心API之前通过它进行代理,我们有一个Web应用程序直接到达核心API。
我们只想区分哪些请求来自移动的应用程序。我们不想更改和重新提交移动应用程序。因此,我想将标头硬编码到API网关中,例如"X-IS-MOBILE": "true"
我第一次尝试在参数中添加它:

swagger: "2.0"
info:
  version: "2019-07-22T10:33:53Z"
  title: "Mobile API Integration"
host: "mobile.domain.link"
basePath: "/v3"
schemes:
- "https"
paths:
  /app-info:
    post:
      operationId: "appInfo"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: header
        name: "X-IS-MOBILE"
        type: boolean
        default: true
      - in: "body"
        name: "AppInfoPayload"
        required: true
        schema:
          $ref: "#/definitions/AppInfoPayload"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/AppInfoView"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
        "400":
          description: "400 response"
          schema:
            $ref: "#/definitions/ApiError"
        "401":
          description: "401 response"
          schema:
            $ref: "#/definitions/ApiError"
        "500":
          description: "500 response"
          schema:
            $ref: "#/definitions/ApiError"
      x-amazon-apigateway-integration:
        httpMethod: "POST"
        uri: "https://api.domain.link/v1/app-info"
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http_proxy"
    options:
      produces:
      - "application/json"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
      x-amazon-apigateway-integration:
        httpMethod: "OPTIONS"
        uri: "https://api.domain.link/v1/app-info"
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http_proxy"

这不起作用。当我检查核心API日志时,标头中没有X-IS-MOBILE
然后我尝试在x-amazon-apigateway-integration中使用requestTemplates

swagger: "2.0"
info:
  version: "2019-07-22T10:33:53Z"
  title: "Mobile API Integration"
host: "mobile.domain.link"
basePath: "/v3"
schemes:
- "https"
paths:
  /app-info:
    post:
      operationId: "appInfo"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "AppInfoPayload"
        required: true
        schema:
          $ref: "#/definitions/AppInfoPayload"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/AppInfoView"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
        "400":
          description: "400 response"
          schema:
            $ref: "#/definitions/ApiError"
        "401":
          description: "401 response"
          schema:
            $ref: "#/definitions/ApiError"
        "500":
          description: "500 response"
          schema:
            $ref: "#/definitions/ApiError"
      x-amazon-apigateway-integration:
        httpMethod: "POST"
        uri: "https://api.domain.link/v1/app-info"
        requestTemplates:
          'application/json': |
            {
              "headers": {
                  "X-IS-MOBILE": "true"
              },
              "body": $input.json('$')
            }
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http_proxy"
    options:
      produces:
      - "application/json"
      responses:
        "200":
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Access-Control-Allow-Origin:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Headers:
              type: "string"
      x-amazon-apigateway-integration:
        httpMethod: "OPTIONS"
        uri: "https://api.domain.link/v1/app-info"
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http_proxy"

相关代码为:

requestTemplates:
          'application/json': |
            {
              "headers": {
                  "X-IS-MOBILE": "true"
              },
              "body": $input.json('$')
            }

同样,这也不起作用,核心API日志中没有X-IS-移动的。我做错了什么?

pftdvrlh

pftdvrlh1#

因此,在第一次尝试中,您将X-IS-MOBILE头添加到incomingrequestheader参数中以进行验证(即,如果需要但不存在,APIGW将使用400 Bad Request进行回复)。
在第二次尝试时,requestTemplates存在一些问题:
1.不适用于http_proxy集成类型,仅适用于http;
1.code 本身不正确,应为:

x-amazon-apigateway-integration:
        httpMethod: "POST"
        uri: "https://api.domain.link/v1/app-info"
        requestTemplates:
          'application/json': |
            #set($context.requestOverride.header.x-is-mobile = 'true')
            $input.json('$')
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http"

参见相关文档和示例here
但是,如果您想要/需要使用http_proxy集成类型,您仍然可以将标题Map添加到集成请求中,而使用requestParameters

x-amazon-apigateway-integration:
        httpMethod: "POST"
        uri: "https://api.domain.link/v1/app-info"
        requestParameters:
          integration.request.header.x-is-mobile: "'true'"
        responses:
          default:
            statusCode: "200"
        passthroughBehavior: "when_no_match"
        type: "http_proxy"

相关问题