首页 > 解决方案 > OpenApi 3.0:覆盖全局安全性

问题描述

在 OpenAPI 3 中,是否可以在全局级别定义 SecurityScheme,然后在某些端点覆盖它以不使用安全性(对于公共可访问端点)?

例如(取自https://swagger.io/docs/specification/authentication/bearer-authentication/

openapi: 3.0.0
...
# 1) Define the security scheme type (HTTP bearer)
components:
  securitySchemes:
    bearerAuth:            # arbitrary name for the security scheme
      type: http
      scheme: bearer
      bearerFormat: JWT    # optional, arbitrary value for documentation purposes
# 2) Apply the security globally to all operations
security:
  - bearerAuth: []         # use the same name as above

然后使给定的端点可公开访问(不受保护)

paths:
  /unprotected/path:
    get:
      security: []

还是应该以其他方式完成?

更新这个问题被标记为重复,但另一个问题处理关于 Swagger 2.x 并且由于语法不同,我认为这个问题和答案应该保留。

标签: securityopenapi

解决方案


您确实可以覆盖 OA3 中路径库的安全性,如下所示:

paths:
  /unprotected/path:
    get:
      security: []

推荐阅读