3

我有一个基本的身份验证安全 API,但在填写身份验证凭据后,它不适用于请求标头。我在 swagger 编辑器上看到“ERROR Server not found or an error occurred”,在提琴手上看到“401 Unauthorized”。

用户名和密码:odata 和 qtkr47PTM3pmzLyEHNrW4DXhhgyjMfM3CKUZfXdn0tk=

这是我招摇的 json

{
"swagger": "2.0",
"info": {
    "version": "1.0.0",
    "title": "Basic Auth Example",
    "description": "An example for how to use Basic Auth with Swagger.\nServer code is available [here](http://navm3.cloudapp.net:90/nav/odata). It's running on NAVM3.\n\n**You can use below User Name and Password for test.**\n* User Name: `ODATA`\n* Password: `qtkr47PTM3pmzLyEHNrW4DXhhgyjMfM3CKUZfXdn0tk=`\n"
},
"host": "navm3.cloudapp.net:90",
"basePath": "/nav/odata",
"schemes": [
    "http"
],
"securityDefinitions": {
    "basicAuth": {
        "type": "basic",
        "description": "HTTP Basic Authentication. Works over `HTTP` and `HTTPS`"
    }
},
"paths": {
    "/": {
        "get": {
            "security": [
                {
                    "basicAuth": []
                }
            ],
            "responses": {
                "200": {
                    "description": "Will send `Authenticated` if authentication is succesful, otherwise it will send `Unauthorized`"
                }
            }
        }
    }
}

}

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

1

我有同样的问题,但问题出在 nodeJS 后端。

如果您使用的是 NodeJS,则可能是 CORS 的问题。您应该使用 Express 在 NodeJS 中启用 CORS,一切都会正常工作。

要在 nodeJS 中启用 CORS,您可以在路由之前添加以下代码。

var app = express();

app.use(function(req, res, next) {

    res.setHeader('Access-Control-Allow-Origin', '*');

    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, UPDATE, DELETE, OPTIONS');

    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');

    next();

});
于 2016-06-21T13:16:25.147 回答
0

尝试更换以下部分:

"securityDefinitions": {
    "basicAuth": {
        "type": "basic",
        "description": "HTTP Basic Authentication. Works over `HTTP` and `HTTPS`"
    }
},

与以下

"securityDefinitions": {
    "basicAuth": {
        "type": "http",
        "scheme": "basic"
    }
},

其余部分保持原样是参考https://swagger.io/docs/specification/authentication/basic-authentication/

于 2021-12-15T09:44:02.073 回答