问题描述
我正在使用OpenAPI 3.0创建API文档。
我有如下定义的安全方案。
components:
securitySchemes:
BasicAuth:
type: http
scheme: basic
BearerAuth:
type: http
scheme: bearer
我定义了两个服务器URL。
servers:
- url: https://api.example.com
description: Production server (uses live data)
- url: https://sandbox-api.example.com
description: Sandbox server (uses test data)
我想仅对沙箱服务器应用BasicAuth安全性,而仅对生产服务器应用BearerAuth。 我该怎么办?
解决方法
OpenAPI无法更改每台服务器的安全性。您可以执行以下操作:
- 为沙盒服务器和生产服务器提供单独的API定义。
或
- 在
security
中指定两种身份验证方法,并使用description
来说明哪个服务器使用哪种身份验证方法。
# Either Basic or Bearer auth
security:
- BasicAuth: []
- BearerAuth: []
components:
securitySchemes:
BasicAuth:
type: http
scheme: basic
description: Use Basic authentication with the **Sandbox** server.
BearerAuth:
type: http
scheme: bearer
description: Use Bearer authentication with the **Production** server.