如何使用“授权:Bearer < token>' in a Swagger Spec

z0qdvdin  于 8个月前  发布在  其他
关注(0)|答案(7)|浏览(125)

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

Authorization: Bearer <token>

这是我根据swagger文档得出的结论:

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

watbbzwu1#

也许这可以帮助:

swagger: '2.0'
info:
  version: 1.0.0
  title: Bearer auth example
  description: >
    An example for how to use Bearer Auth with OpenAPI / Swagger 2.0.

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

您可以将其复制并粘贴到https://editor.swagger.io以查看结果。
在Swagger Editor网站上也有几个例子,它们具有更复杂的安全配置,可以帮助您。

**重要提示:**本例中,API消费者必须在token值中包含“Bearer”前缀。例如,当使用Swagger UI的“Authorize”对话框时,您需要输入Bearer your_token,而不仅仅是your_token

abithluo

abithluo2#

OpenAPI 3.x中的Bearer认证

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.x规范!).
UI将显示“授权”按钮,您可以单击并输入不记名令牌(仅令牌本身,没有“Bearer“前缀)。之后,“try it out”请求将与Authorization: Bearer xxxxxx报头一起发送。

以编程方式添加Authorization头(Swagger UI 3.x+)

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

// index.html

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

  requestInterceptor: (req) => {
    req.headers.Authorization = "Bearer xxxxxxx"
    return req
  }
})
fruv7luv

fruv7luv3#

使用openapi 3.0.0发布JSON格式的2023答案:

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

  "components": {        
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    }
  }
}
xkrw2x1b

xkrw2x1b4#

**为什么“接受的答案”工作.但对我来说还不够

这在规范中起作用。至少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令牌支持。
注:这一点在

所以,它像标准头一样处理身份验证。在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);

也能工作。

kcwpcxri

kcwpcxri5#

通过使用requestInterceptor,它为我工作:

const ui = SwaggerUIBundle({
  ...
  requestInterceptor: (req) => {
    req.headers.Authorization = "Bearer " + req.headers.Authorization;
    return req;
  },
  ...
});
trnvg8h3

trnvg8h36#

我的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

}

ff29svar

ff29svar7#

从laravel 7 x(“openapi”:“3.0.0”),使用以下代码编辑您的PHP\l5-swagger.php

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

然后你可以添加这个作为你的端点的安全注解:

*security={
     *{
     *"bearerAuth": {}},
     *},

相关问题