Swagger-php Swagger-ui 不使用基本身份验证
Swagger-php with Swagger-ui not working with Basic Authentication
我使用 swagger-ui 和 swagger-json 生成的 swagger-php。我无法执行基本身份验证来使用我的端点。我可以从我的应用程序中获取 swagger json 文件,但不能使用暴露的端点。我不明白我在误会什么。如果有人可以向我展示 swagger 2.0 中基本身份验证的完整示例?
CORS 已启用且完全正常工作。 Swagger-ui 是 运行 on localhost:3000 with nodejs 我的应用程序是 运行 php with nginx on localhost:80.
我使用与 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 评论来使用 basicAuth,(server-side)
/**
* @SWG\SecurityScheme(
* securityDefinition="basicAuth",
* name="authorization",
* type="basic",
* scheme="http"
* )
*/
和这条评论
/**
* @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")
* )
* )
*/
这是我的 client-side 代码:
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 - 未授权,因为基本身份验证 header 未发送。
注释看起来不对。将 @SWG\SecurityScheme
更改为:
/**
* @SWG\SecurityScheme(
* securityDefinition="basicAuth",
* type="basic"
* )
*/
(不带name
和scheme
属性),将@SWG\Get
中的security
改为:
/**
* @SWG\Get(
* ...
* security = {{"basicAuth": {}}},
* ...
我使用 swagger-ui 和 swagger-json 生成的 swagger-php。我无法执行基本身份验证来使用我的端点。我可以从我的应用程序中获取 swagger json 文件,但不能使用暴露的端点。我不明白我在误会什么。如果有人可以向我展示 swagger 2.0 中基本身份验证的完整示例? CORS 已启用且完全正常工作。 Swagger-ui 是 运行 on localhost:3000 with nodejs 我的应用程序是 运行 php with nginx on localhost:80.
我使用与 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 评论来使用 basicAuth,(server-side)
/**
* @SWG\SecurityScheme(
* securityDefinition="basicAuth",
* name="authorization",
* type="basic",
* scheme="http"
* )
*/
和这条评论
/**
* @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")
* )
* )
*/
这是我的 client-side 代码:
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 - 未授权,因为基本身份验证 header 未发送。
注释看起来不对。将 @SWG\SecurityScheme
更改为:
/**
* @SWG\SecurityScheme(
* securityDefinition="basicAuth",
* type="basic"
* )
*/
(不带name
和scheme
属性),将@SWG\Get
中的security
改为:
/**
* @SWG\Get(
* ...
* security = {{"basicAuth": {}}},
* ...