【问题标题】:Swagger-php with Swagger-ui not working with Basic Authentication带有 Swagger-ui 的 Swagger-php 无法使用基本身份验证
【发布时间】: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 只需要securityDefinitiontype="basic" 并且不应具有其他属性,因为它们适用于其他身份验证类型。可以试试看有没有用?
  • 我试过了,没有任何变化。我也尝试输入“security=[{"basicAuth"}],但没有任何变化。

标签: php swagger swagger-ui


【解决方案1】:

注释看起来不正确。将@SWG\SecurityScheme 更改为:

/**
 *  @SWG\SecurityScheme(
  *      securityDefinition="basicAuth",
  *      type="basic"
  *  )
  */

(不带namescheme 属性),并将@SWG\Get 中的security 更改如下:

/**
 * @SWG\Get(
 *     ...
 *     security = {{"basicAuth": {}}},
 *     ...

【讨论】:

  • 它有效,但我必须强调在 swagger-php 文档中非常模棱两可的奇怪数组类型。这只是告诉 swg\get classe 中属性 «security» 的 «var array»。
猜你喜欢
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2017-06-26
  • 2019-11-02
  • 2021-07-20
相关资源
最近更新 更多