首页 文章

如何让Swagger将API密钥作为http而不是URL发送

提问于
浏览
21

我正在使用servicestack的招摇,但我从我的/资源URL获得401未经授权的错误,因为它需要API密钥 .

除非我弄错了,according to the documentation我应该从我的html页面初始化Swagger时将supportHeaderParams设置为true以及JSON参数中的apiKeyName和apiKey值 .

我当时希望在http请求标头中看到我的API密钥,但它仍然被附加到URL而不是标头集合中 .

以下是在我的HTML页面中初始化Swagger的代码:

window.swaggerUi = new SwaggerUi({
            discoveryUrl: "http://pathtomyservice.com/resources",
                headers: { "testheader" : "123" },
                apiKey: "123",
                apiKeyName: "Api-Key",
                dom_id:"swagger-ui-container",
                supportHeaderParams: true,
                supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
                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"
            });

不幸的是我根本没有 Headers ,没有'Api-Key'或'testheader' .

3 回答

  • 20

    我认为这可能是一个招摇ui的错误 .

    作为一种解决方法,我在swagger index.html文件中添加了以下内容 .

    $(function () {
       $.ajaxSetup({
           beforeSend: function (jqXHR, settings) {
               jqXHR.setRequestHeader("YourApiKeyHeader", $("#input_apiKey").val());
           }
       });
    });
    

    希望这可以帮助,

  • 1

    在swagger-ui 2.0或更高版本中,这是微不足道的:

    https://github.com/wordnik/swagger-ui#header-parameters

    // add a new ApiKeyAuthorization when the api-key changes in the ui.
    $('#input_apiKey').change(function() {
      var key = $('#input_apiKey')[0].value;
      if(key && key.trim() != "") {
        window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "header"));
      }
    })
    

    这也更具可扩展性,并支持自定义身份验证机制 .

  • 26

    你可以试试这个

    (function () {
        $(function () {
            var basicAuthUI =
                    '<div class="input"><input placeholder="username" id="input_username" name="username" type="text" size="10"/></div>' +
                    '<div class="input"><input placeholder="password" id="input_password" name="password" type="password" size="10"/></div>';
            $(basicAuthUI).insertBefore('#api_selector div.input:last-child');
            $("#input_apiKey").hide();
    
            $('#input_username').change(addAuthorization);
            $('#input_password').change(addAuthorization);
        });
    
        function addAuthorization() {
            SwaggerApi.supportHeaderParams = true;
            SwaggerApi.headers = {"authentication": "test"};
            var username = $('#input_username').val();
            var password = $('#input_password').val();
            if (username && username.trim() != "" && password && password.trim() != "") {
                var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
                window.swaggerUi.api.clientAuthorizations.add("basicAuth", basicAuth);
                console.log("authorization added: username = " + username + ", password = " + password);
            }
        }
    })();
    

相关问题