【发布时间】:2018-05-08 04:03:45
【问题描述】:
我使用 swagger-ui 和 swagger-json 由 swagger-php 生成。我无法进行基本身份验证来使用我的端点。我可以从我的应用程序中获取 swagger json 文件,但不能使用暴露的端点。我不明白我误解了什么。如果有人可以在 swagger 2.0 中向我展示基本身份验证的完整示例? CORS 已启用并完全正常工作。 Swagger-ui 使用 nodejs 在 localhost:3000 上运行,我的应用程序在 localhost:80 上使用 nginx 运行 php。
我使用的是兼容 swagger 2.0 的 swagger-ui-dist 3.14.1(swagger-php 是 2.0)
3.14.1 | 2018-05-04 | 2.0, 3.0 | tag v3.14.1
我在我的控制器中使用这些 SWG cmets 来使用 basicAuth,(服务器端)
/**
* @SWG\SecurityScheme(
* securityDefinition="basicAuth",
* name="authorization",
* type="basic",
* scheme="http"
* )
*/
而这个cmets
/**
* @SWG\Get(
* description="Get all catalog",
* path="/ott/catalogs",
* produces={"application/json"},
* security = {"basicAuth"},
* @SWG\Response(response="200", description="Get all catalogs"),
* @SWG\Response(response="401",description="You are not authorized")
* )
* )
*/
这是我的客户端代码:
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://ott/ott/tools/swagger",
host: 'ott',
basePath: 'ott/',
schemes: 'http',
enableCORS: true,
dom_id: '#swagger-ui',
deepLinking: true,
validatorUrl:null,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
requestInterceptor: (req) => {
if (req.loadSpec) {
let hash = btoa("*******" + ":" + "********");
req.headers.Authorization = "Basic " + hash;
}
return req
},
onComplete : () => {
ui.preauthorizeBasic("basicAuth","*******","*******")
}
});
window.ui = ui
当我点击锁时,我的控制台中出现第一个错误,然后当我尝试获取我的目录时,我得到一个 401 - 未授权,因为未发送基本身份验证标头。
【问题讨论】:
-
我认为基本身份验证的
@SWG\SecurityScheme只需要securityDefinition和type="basic"并且不应具有其他属性,因为它们适用于其他身份验证类型。可以试试看有没有用? -
我试过了,没有任何变化。我也尝试输入“security=[{"basicAuth"}],但没有任何变化。
标签: php swagger swagger-ui