为Swagger-UI添加基本授权

Zac*_*ert 16 basic-authentication swagger swagger-ui

我目前部署了一个招摇项目,但我无法添加一些基本授权.当你点击"试试看!"时是当前的.按钮您需要登录帐户才能访问结果.我有一个帐户,我想每次有人试图访问api时自动使用.Bellow是我项目的index.html:

    <!DOCTYPE html>
<html>
<head>
  <title>Swagger UI</title>
  <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
  <link href='css/screen.css' media='print' rel='stylesheet' type='text/css'/>
  <script src='lib/jquery-1.8.3.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-1.0.rc.1.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='lib/swagger.js' type='text/javascript'></script>
  <script src='lib/swagger-ui.js' type='text/javascript'></script>
  <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>

  <script type="text/javascript">
    $(function () {
        window.swaggerUi = new SwaggerUi({
                discoveryUrl:"https://localhost:8080/AssistAPI/api-docs.json",
                apiKey:"",
                dom_id:"swagger-ui-container",
                supportHeaderParams: true,
                supportedSubmitMethods: ['get', 'post', 'put'],
                onComplete: function(swaggerApi, swaggerUi){
                    if(console) {
                        console.log("Loaded SwaggerUI")
                        console.log(swaggerApi);
                        console.log(swaggerUi);
                    }
                  $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
                },
                onFailure: function(data) {
                    if(console) {
                        console.log("Unable to Load SwaggerUI");
                        console.log(data);
                    }
                },
                docExpansion: "none"
            });
            window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "XXXX"header"));
            //window.authorizations.add("key", new ApiKeyAuthorization("username", "rmanda", "header"));
            window.swaggerUi.load();
        });
  </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'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
      <div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
      <div class='input'><a id="explore" href="#">Explore</a></div>
    </form>
  </div>
</div>

<div id="message-bar" class="swagger-ui-wrap">&nbsp;</div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我试图确定信息应该去哪里以允许基本授权.我知道它涉及以下几行代码,但是有人可以更准确地向我解释事情的确切方向.我已经认识到GitHub上swagger的文档不是很清楚或有用

window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "XXXX"header"));
window.authorizations.add("key", new ApiKeyAuthorization("username", "password", "header"));
Run Code Online (Sandbox Code Playgroud)

Hel*_*len 12

Swagger UI 3.x

在 Swagger UI 3.13.0+ 中,您可以使用该preauthorizeBasic方法预先填写“试用”调用的基本身份验证用户名和密码。

假设您的 API 定义包括基本身份验证的安全方案:

swagger: '2.0'
...
securityDefinitions:
  basicAuth:
    type: basic

security:
  - basicAuth: []
Run Code Online (Sandbox Code Playgroud)

您可以为 Basic auth 指定默认用户名和密码,如下所示:

// index.html

const ui = SwaggerUIBundle({
  url: "https://my.api.com/swagger.yaml",
  ...
  onComplete: function() {
    // "basicAuth" is the key name of the security scheme in securityDefinitions
    ui.preauthorizeBasic("basicAuth", "username", "password");
  }
})
Run Code Online (Sandbox Code Playgroud)

“试用”将使用指定的用户名和密码,如果您在 Swagger UI 中单击“授权”按钮,您将看到在 UI 中预先填写了用户名和掩码密码。


此答案还包含 Swagger UI 3.1.6—3.12.1 的解决方案,该解决方案没有该preauthorizeBasic功能。


ipe*_*ffo 6

基本身份验证标头的正确设置是:

Authorization: Basic username:password
Run Code Online (Sandbox Code Playgroud)

String username:password需要使用RFC2045-MIME编码,这是Base64的一种变体.在此处查看更多详细信息:https://en.wikipedia.org/wiki/Basic_access_authentication#Client_side

然后,要配置此标头,您应该:

考虑到用户名:密码的Base64编码是dXNlcm5hbWU6cGFzc3dvcmQ=

swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", "header"));
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多相关信息:https: //github.com/swagger-api/swagger-ui