2015-09-22 136 views
3

我想在swagger-ui中添加基本授權,通過「試用」按鈕來測試我的WS。我嘗試從swagger documentation,和這個主題Adding Basic Authorization for Swagger-UI,但沒有人工作。請求中未添加標頭,但在curl命令中,存在基本授權標頭。如果我複製粘貼這個捲曲命令,一切都好。swagger-ui:添加授權標頭不起作用

我的swagger-ui版本是2.1.2。我從github下載,螞蟻直接把dist文件夾放在我服務器的靜態文件夾中。

這裏我的index.html:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Sensor API documentation</title> 
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" /> 
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" /> 
<link href='css/typography.css' media='screen' rel='stylesheet' type='text/css' /> 
<link href='css/reset.css' media='screen' rel='stylesheet' type='text/css' /> 
<link href='css/screen.css' media='screen' rel='stylesheet' type='text/css' /> 
<link href='css/reset.css' media='print' rel='stylesheet' type='text/css' /> 
<link href='css/print.css' media='print' rel='stylesheet' type='text/css' /> 
<script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script> 
<script src='lib/jquery.slideto.min.js' type='text/javascript'></script> 
<script src='lib/jquery.wiggle.min.js' type='text/javascript'></script> 
<script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script> 
<script src='lib/handlebars-2.0.0.js' type='text/javascript'></script> 
<script src='lib/underscore-min.js' type='text/javascript'></script> 
<script src='lib/backbone-min.js' type='text/javascript'></script> 
<script src='swagger-ui.js' type='text/javascript'></script> 
<script src='lib/highlight.7.3.pack.js' type='text/javascript'></script> 
<script src='lib/marked.js' type='text/javascript'></script> 
<script src='lib/swagger-oauth.js' type='text/javascript'></script> 

<!-- Some basic translations --> 
<!-- <script src='lang/translator.js' type='text/javascript'></script> --> 
<!-- <script src='lang/ru.js' type='text/javascript'></script> --> 
<!-- <script src='lang/en.js' type='text/javascript'></script> --> 

<script type="text/javascript"> 
    $(function() { 

     // Pre load translate... 
     if (window.SwaggerTranslator) { 
      window.SwaggerTranslator.translate(); 
     } 
     window.swaggerUi = new SwaggerUi({ 
      url : "/swagger/json/SensorAPI.json", 
      validatorUrl : "", 
      dom_id : "swagger-ui-container", 
      supportedSubmitMethods : [ 'get', 'post', 'put', 'delete', 'patch' ], 
      onComplete : function(swaggerApi, swaggerUi) { 

       if (window.SwaggerTranslator) { 
        window.SwaggerTranslator.translate(); 
       } 

       $('pre code').each(function(i, e) { 
        hljs.highlightBlock(e) 
       }); 
       addAuthorization(); 

      }, 
      onFailure : function(data) { 
       log("Unable to Load SwaggerUI"); 
      }, 
      docExpansion : "none", 
      apisSorter : "alpha", 
      showRequestHeaders : false 
     }); 

     function addAuthorization() { 
      var username = $('#input_username').val(); 
      var password = $('#input_password').val(); 
      if (username && username.trim() != "" && password && password.trim() != "") { 
       var basicAuth = new SwaggerClient.PasswordAuthorization(username, password); 
       window.swaggerUi.api.clientAuthorizations.add('Basic', basicAuth); 
       log("authorization added: username = " + username + ", password = " + password); 
      } else { 
       window.authorizations.remove(); 
      } 
     } 
     $('#input_username').change(addAuthorization); 
     $('#input_password').change(addAuthorization); 
     // pre-populate on the page using demo account 
     $('#input_username').val("demo"); 
     $('#input_password').val("demo"); 

     window.swaggerUi.load(); 

     function log() { 
      if ('console' in window) { 
       console.log.apply(console, arguments); 
      } 
     } 
    }); 
</script> 
</head> 

<body class="swagger-section"> 
    <div id='header'> 
     <div class="swagger-ui-wrap"> 
      <a id="logo" href="http://swagger.io">swagger</a> 
      <form id='api_selector'> 
       <div class='input'> 
        Username: <input placeholder="username" id="input_username" name="username" type="text" size="10" /> 
       </div> 
       <div class='input'> 
        Password: <input placeholder="password" id="input_password" name="password" type="password" size="10" /> 
       </div> 
      </form> 
     </div> 
    </div> 

    <div id="message-bar" class="swagger-ui-wrap" data-sw-translate>&nbsp;</div> 
    <div id="swagger-ui-container" class="swagger-ui-wrap"></div> 
</body> 
</html> 

請告訴我,什麼是錯的。謝謝

回答

3

我發現:問題來自我的json文件。我定義了許多「securityDefinitions」,每個用戶的角色

..., 
"securityDefinitions":{ 
    "administrator":{ 
     "type":"basic", 
     "description":"The administrator." 
    }, 
    "supervisor":{ 
     "type":"basic", 
     "description":"A supervisor." 
    }, 
    "customer":{ 
     "type":"basic", 
     "description":"A customer." 
    } 
}, 
... 

一個和每個WS,我指出哪些角色授權。例如:

..., 
"get":{ 
    "security":[ 
     { 
      "administrator":[ 

      ] 
     }, 
     { 
      "supervisor":[ 

      ] 
     } 
    ], 
    ... 
}, 
... 

通過刪除WS中的安全部分,所有工作都正常。

在招搖編輯器,我們需要保衛科測試WS,但是在招搖的用戶界面,這部分產生一個錯誤:-(

+0

同樣的問題,你我在這裏報告的問題:https://開頭github上。 COM /招搖-API /招搖的UI /問題/ 1835 – hiveship