【问题标题】:How to Add custom authorization in swagger UI?如何在 swagger UI 中添加自定义授权?
【发布时间】:2015-10-15 06:27:40
【问题描述】:

在我的一个 Rest API 调用中,我需要添加标题“Authorization: partner_id: timestamp signature”

其中公司名称是静态字符串,它可以被硬编码,其中 partner_id 是用户输入的查询参数的一部分,并且 签名计算为 sha256(secret,password).digest.encode('base64')

我现在如何以及在哪里可以实现此授权,因为我看到只有 api_key,并且在 swagger-ui 中允许基本授权。

【问题讨论】:

    标签: javascript authorization swagger swagger-ui swagger-2.0


    【解决方案1】:

    是的,您可以轻松创建自己的签名技术。来自swagger-js自述文件:

    var CustomRequestSigner = function(name) {
      this.name = name;
    };
    
    CustomRequestSigner.prototype.apply = function(obj, authorizations) {
      // do something real instead of this...
      var hashFunction = this._btoa;
      var hash = hashFunction(obj.url);
    
      obj.headers["signature"] = hash;
      return true;
    };
    

    并像这样将其添加到招摇中:

    client.clientAuthorizations
        .add("custom", new CustomRequestSigner("custom","special-key","query"));
    

    然后,任何标记有安全要求 custom 的操作都将应用此功能。

    【讨论】:

    • 如果未定义 client.clientAuthorizations 怎么办?我把它当作未定义的
    • 我不清楚这一切的去向?
    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 2021-08-21
    • 2018-06-17
    • 2018-10-04
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多