无法在 swagger 文档中添加不记名令牌

Could not add bearer token in swagger docs

我需要在 swagger 编辑器中记录的每个 API 中添加不记名令牌。但根据我的代码,它没有按预期工作。我在下面解释我的代码。

swagger: "2.0"
info:
  description: "This is a sample API documentation of DCI project."
  version: "1.0.0"
  title: "Swagger API docs for DCI"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "**.**.**.**"
basePath: "/bpa/api/v1.0/workflow/process-definition/"
tags:
- name: "DCI"
  description: "API to start the workflow of DCI."
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
schemes:
- "https"
- "http"
paths:
  /DCI_PORT_MGMT/start:
    post:
      tags:
      - "Port Allocation"
      summary: "Add a new port"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Start the port allocation workflow."
        required: true
        schema:
          type: object
      responses:
        "405":
          description: "Invalid input"
      security:
      - petstore_auth:
        - "write:pets"
        - "read:pets"
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

这里我需要在每个API的header中添加bearer token。根据我的代码,当我执行 API 时,它不要求添加任何令牌。如果有人能帮助我,那将是很大的帮助。

根据 swagger 文档和 other answers,您只需要拥有安全定义,然后在一个或多个路径中使用它。例如

swagger: "2.0"
info:
  description: "This is a sample API documentation of DCI project."
  version: "1.0.0"
  title: "Swagger API docs for DCI"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "**.**.**.**"
basePath: "/bpa/api/v1.0/workflow/process-definition/"
tags:
- name: "DCI"
  description: "API to start the workflow of DCI."
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
schemes:
- "https"
- "http"
paths:
  /DCI_PORT_MGMT/start:
    post:
      security:
        - Bearer: []
      tags:
      - "Port Allocation"
      summary: "Add a new port"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Start the port allocation workflow."
        required: true
        schema:
          type: object
      responses:
        "405":
          description: "Invalid input"
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

如果您在 swagger 编辑器中进行测试,则有一个授权按钮(右上角)应该允许您为此 header 输入凭据。您可以测试请求/执行它,您会看到将添加授权 header。