148

我试图传达身份验证/安全方案需要设置如下标头:

Authorization: Bearer <token>

这是我基于招摇文档所拥有的:

securityDefinitions:
  APIKey:
    type: apiKey
    name: Authorization
    in: header
security:
  - APIKey: []
4

7 回答 7

172

也许这可以帮助:

swagger: '2.0'
info:
  version: 1.0.0
  title: Based on "Basic Auth Example"
  description: >
    An example for how to use Auth with Swagger.

host: basic-auth-server.herokuapp.com
schemes:
  - http
  - https
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
paths:
  /:
    get:
      security:
        - Bearer: []
      responses:
        '200':
          description: 'Will send `Authenticated`'
        '403': 
          description: 'You do not have necessary permissions for the resource'

您可以在此处复制并粘贴它:http: //editor.swagger.io/#/以查看结果。

swagger editor web 中还有几个示例,它们具有更复杂的安全配置,可以帮助您。

于 2015-10-07T14:56:55.793 回答
83

OpenAPI 3.0.0 中的承载认证

OpenAPI 3.0现在原生支持 Bearer/JWT 身份验证。它是这样定义的:

openapi: 3.0.0
...

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT  # optional, for documentation purposes only

security:
  - bearerAuth: []

这在 Swagger UI 3.4.0+ 和 Swagger Editor 3.1.12+ 中受支持(同样,仅适用于 OpenAPI 3.0 规范!)。

UI 将显示“授权”按钮,您可以单击该按钮并输入不记名令牌(只是令牌本身,没有“不记名”前缀)。之后,“试用”请求将与Authorization: Bearer xxxxxx标头一起发送。

以编程方式添加Authorization标题 (Swagger UI 3.x)

如果您使用 Swagger UI,并且由于某种原因需要以Authorization编程方式添加标头,而不是让用户单击“授权”并输入令牌,则可以使用requestInterceptor. 此解决方案适用于Swagger UI 3.x;UI 2.x 使用了不同的技术。

// index.html

const ui = SwaggerUIBundle({
  url: "http://your.server.com/swagger.json",
  ...

  requestInterceptor: (req) => {
    req.headers.Authorization = "Bearer xxxxxxx"
    return req
  }
})
于 2017-08-02T21:07:34.610 回答
20

使用 openapi 3.0.0 以 JSON 格式发布 2022 年的答案:

{
  "openapi": "3.0.0",
  ...
  "servers": [
    {
      "url": "/"
    }
  ],
  ...
  "paths": {
    "/skills": {
      "put": {
        "security": [
           {
              "bearerAuth": []
           }
        ],
       ...
  },


  "components": {        
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    }
  }
}
于 2020-07-15T03:20:57.590 回答
19

为什么“接受的答案”有效……但这对我来说还不够

这在规范中有效。至少swagger-tools(版本 0.10.1)将其验证为有效。

但是如果您使用其他工具,例如swagger-codegen(2.1.6 版),您会发现一些困难,即使生成的客户端包含身份验证定义,如下所示:

this.authentications = {
  'Bearer': {type: 'apiKey', 'in': 'header', name: 'Authorization'}
};

在调用方法(端点)之前,无法将令牌传递到标头中。查看此函数签名:

this.rootGet = function(callback) { ... }

这意味着,我只传递没有令牌的回调(在其他情况下是查询参数等),这会导致对服务器的请求构建不正确。

我的选择

不幸的是,它并不“漂亮”,但在我在 Swagger 上获得 JWT Tokens 支持之前它一直有效。

注意:正在讨论

因此,它像标准标头一样处理身份验证。在path对象上附加一个标头参数:

swagger: '2.0'
info:
  version: 1.0.0
  title: Based on "Basic Auth Example"
  description: >
    An example for how to use Auth with Swagger.

host: localhost
schemes:
  - http
  - https
paths:
  /:
    get:
      parameters:
        - 
          name: authorization
          in: header
          type: string
          required: true
      responses:
        '200':
          description: 'Will send `Authenticated`'
        '403': 
          description: 'You do not have necessary permissions for the resource'

这将在方法签名上生成一个带有新参数的客户端:

this.rootGet = function(authorization, callback) {
  // ...
  var headerParams = {
    'authorization': authorization
  };
  // ...
}

要以正确的方式使用此方法,只需传递“完整字符串”

// 'token' and 'cb' comes from elsewhere
var header = 'Bearer ' + token;
sdk.rootGet(header, cb);

并且有效。

于 2016-08-02T17:36:48.977 回答
1

通过使用 requestInterceptor,它对我有用:

const ui = SwaggerUIBundle({
  ...
  requestInterceptor: (req) => {
    req.headers.Authorization = "Bearer " + req.headers.Authorization;
    return req;
  },
  ...
});
于 2020-10-27T03:13:37.033 回答
0

从 laravel 7x ("openapi": "3.0.0") 解决这个问题,使用以下代码编辑您的 config\l5-swagger.php

'securityDefinitions' => [
                'securitySchemes' => [
                    'bearerAuth' => [ 
                        'type' => 'http',
                        'scheme' => 'bearer',
                        'bearerFormat' => 'JWT', 
                    ], 
                ],

然后您可以将其添加为端点的安全注释:

*security={
     *{
     *"bearerAuth": {}},
     *},
于 2021-07-15T22:18:09.817 回答
0

在我的案例中,我的 Hackie 解决此问题的方法是修改 echo-swagger 包中的 swagger.go 文件:

在文件的底部更新 window.onload 函数以包含一个正确格式化令牌的 requestInterceptor。

window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
  url: "{{.URL}}",
  dom_id: '#swagger-ui',
  validatorUrl: null,
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset
  ],
  plugins: [
    SwaggerUIBundle.plugins.DownloadUrl
  ,
  layout: "StandaloneLayout",
  requestInterceptor: (req) => {
    req.headers.Authorization = "Bearer " + req.headers.Authorization
  return req
  }
})

window.ui = ui

}

于 2020-07-22T19:01:23.700 回答