NodeJS OpenAPI -添加一个安全方案来要求对我的API进行授权

oalqel3c  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(153)

如何定义安全方案并将授权应用于我的端点?

{
    "openapi": "3.0.3",
    "info": {
        "description": "NodeJS API documentation of SSV",
        "version": "1.0.0",
        "title": "SSV APIs"
    },
    "components": {
        "securitySchemes": {
            "BearerAuth": {
                "name": "Authorization",
                "in": "header",
                "type": "apiKey",
                "scheme": "bearer",
                "bearerFormat": "JWT",
                "description": "Enter your bearer token in the format Bearer <token>"
            }
        }
     }
 }

个字符

qkf9rpyu

qkf9rpyu1#

你的描述有几点。
"swagger": "2.0"的结构与使用的结构完全不同。您应该定义"openapi": "3.0.3"以匹配文件内容。
在文件的根目录添加security数组将需要对定义的所有端点进行授权。

{
    "openapi": "3.0.3",
    "info": {
        "description": "NodeJS API documentation of SSV",
        "version": "1.0.0",
        "title": "SSV APIs"
    },
    "security": [
        {
            "BearerAuth": []
        }
    ],
    "paths": {
        "/thing": {
            "get": {
                "description": "Get all things",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "securitySchemes": {
            "BearerAuth": {
                "name": "Authorization",
                "in": "header",
                "type": "apiKey",
                "scheme": "bearer",
                "bearerFormat": "JWT",
                "description": "Enter your bearer token in the format Bearer <token>"
            }
        }
    }
}

字符串

或者,您可以在单个端点设置授权。

{
    "openapi": "3.0.3",
    "info": {
        "description": "NodeJS API documentation of SSV",
        "version": "1.0.0",
        "title": "SSV APIs"
    },
    "paths": {
        "/thing": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Get all things",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "securitySchemes": {
            "BearerAuth": {
                "name": "Authorization",
                "in": "header",
                "type": "apiKey",
                "scheme": "bearer",
                "bearerFormat": "JWT",
                "description": "Enter your bearer token in the format Bearer <token>"
            }
        }
    }
}

相关问题