我试图传达认证/安全方案需要设置标题,如下所示:
Authorization: Bearer <token>
这就是我所基于的swagger documentation:
securityDefinitions:
APIKey:
type: apiKey
name: Authorization
in: header
security:
- APIKey: []
也许这可以帮助:
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'
你可以在这里复制并粘贴它:qazxsw poi查看结果。
swagger编辑器网站中还有几个示例,其中包含更复杂的安全配置,可以为您提供帮助。
http://editor.swagger.io/#/现在支持Bearer / JWT身份验证。它的定义如下:
OpenAPI 3.0
这在Swagger UI 3.4.0+和Swagger Editor 3.1.12+中得到支持(同样,仅适用于OpenAPI 3.0规范!)。
UI将显示“授权”按钮,您可以单击该按钮并输入承载令牌(只是令牌本身,不带“承载”前缀)。之后,“试用”请求将与openapi: 3.0.0
...
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT # optional, for documentation purposes only
security:
- bearerAuth: []
标头一起发送。
Authorization: Bearer xxxxxx
header programmatically (Swagger UI 3.x)如果您使用Swagger UI,并且出于某种原因需要以编程方式添加Authorization
标头而不是让用户单击“授权”并输入标记,则可以使用Authorization
。此解决方案适用于Swagger UI 3.x; UI 2.x使用了不同的技术。
requestInterceptor
为什么“接受的答案”有效......但这对我来说还不够
这适用于规范。至少// index.html
const ui = SwaggerUIBundle({
url: "http://your.server.com/swagger.json",
...
requestInterceptor: (req) => {
req.headers.Authorization = "Bearer xxxxxxx"
return req
}
})
(版本0.10.1)验证它是有效的。
但是如果你使用swagger-tools
(版本2.1.6)之类的其他工具,你会发现一些困难,即使生成的客户端包含Authentication定义,如下所示:
swagger-codegen
在调用方法(端点)之前,无法将令牌传递到标头中。查看此函数签名:
this.authentications = {
'Bearer': {type: 'apiKey', 'in': 'header', name: 'Authorization'}
};
这意味着,我只在没有令牌的情况下传递回调(在其他情况下是查询参数等),这导致对服务器的请求的不正确构建。
我的选择
不幸的是,它并不“漂亮”,但它一直有效,直到我在Swagger上获得JWT Tokens支持。
注意:正在讨论中
this.rootGet = function(callback) { ... }
因此,它像标准头一样处理身份验证。在qazxsw poi对象上附加一个头参数:
Extensibility of security definitions? #460
这将生成一个带有方法签名的新参数的客户端:
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'
并且工作。