Swagger-php与Swagger-ui无法使用基本身份验证

时间:2018-05-07 17:56:13

标签: php swagger swagger-ui

我使用swagger-php生成的swagger-ui和swagger-json。我无法使用基本身份验证来使用我的终端。我可以从我的应用程序中获取swagger json文件,但不能使用暴露的端点。我不明白我的误解。如果有人能够在swagger 2.0中向我展示基本身份验证的完整示例? CORS启用并完全正常工作。 Swagger-ui在localhost:3000上使用nodejs运行,我的应用程序在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注释来使用basicAuth,(服务器端)

/**
 *  @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")
 *     )
 * )
 */

这是我的客户端代码:

 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 - 未经授权,因为未发送基本身份验证标头。

capture du 2018-05-07 16-44-38

1 个答案:

答案 0 :(得分:3)

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

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

(没有namescheme属性),并更改security中的@SWG\Get,如下所示:

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