标记方法匿名:swagger version 3.0.2

Mark a method anonymous: swagger version 3.0.2

我正在使用 Swagger-ui version 3.0.2,我在本地托管了它并向其提供了我的 Json 文件,API 它可以很好地打开文档并列出 [=20] 中的所有方法=] 文件,在我将基本身份验证放入其中后,我在 .JSON 文件中进行了更改,但是有一些方法我想标记为匿名。

{
    "swagger": "2.0",
    "info": {
        "description": "description",
        "version": "1.0",
        "title": "API"
    },
    "host": "localhost",
    "schemes": [
        "http"
    ],
    "securityDefinitions": {
        "anonymous_auth": {
            "type": ""
        },
        "basic_auth": {
            "type": "basic",
            "name": "basic_auth",
            "description": "Basic Authentication"
        },
        "token": {
            "type": "apiKey",
            "description": "API Token Authentication",
            "name": "apikey",
            "in": "header"
        }
    },
    "security": [
        {
            "basic_auth": [ ]
        },
        {
            "token": [ ]
        }
    ],
    "paths": {
        //somthing
    },
    "definitions": {  
        //something    
    }
}

通过以这种方式使用 security 属性,它将保护完整的文件,但我有一些方法应该是匿名的。

要删除全局 security,请将空 security 数组添加到操作中:

"paths": {
  "/something:": {
    "get": {
      "security": [],
      ...
    }
  }
}


此外,您的规范无效:

  1. 移除anonymous_auth.

  2. basic_auth 中删除 name - name 仅用于 apiKey 安全方案以指定 header 的名称或将包含 API 键的查询参数。