如何在请求之前在 Swagger 中计算 AWS 签名 V4 [英] How to calculate AWS signature V4 in Swagger before request

查看:18
本文介绍了如何在请求之前在 Swagger 中计算 AWS 签名 V4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们的 AWS API 终端节点,我们使用 AWS_IAM 授权并希望从 Swagger UI 进行调用.要成功调用,必须有 2 个标头授权"和x-amz-date".为了形成授权",我们使用 遵循 aws doc 中的步骤.我们必须在每次调用时更改x-amz-date"以通过授权.问题是:如何在 Swagger 中编写脚本来签署请求,每次 请求发送到 aws 时运行该脚本?(我们知道如何在加载 Swagger 页面之前一次性指定两个标题,但是这个过程应该在每次调用之前重新运行).

For our AWS API Endpoints we use AWS_IAM authorization and want to make a call from Swagger UI. To make a successful call there must be 2 headers 'Authorization' and 'x-amz-date'. To form 'Authorization' we use following steps from aws doc. We must to change 'x-amz-date' with every call to go through authorization. The question is: How to write script in Swagger to sign request, which run every time before request send to aws? (We know how to specify both headers one time before loading Swagger page, but this process should be re-run before every call).

提前致谢.

推荐答案

swagger-js 添加 requestInterceptor 来做到这一点.swagger-ui 项目在后台使用 swagger-js.

There is built-in support in swagger-js to add requestInterceptors to do just this. The swagger-ui project uses swagger-js under the hood.

像这样简单地创建一个请求拦截器:

Simply create a request interceptor like such:

requestInterceptor: {
  apply: function (request) {
    // modify the request object here
    return request;
  }
}

并在创建时将其应用于您的 swagger 实例:

and apply it to your swagger instance on creation:

window.swaggerUi = new SwaggerUi({
  url: url,
  dom_id: "swagger-ui-container",
  requestInterceptor: requestInterceptor,

在这里您可以在 request 对象中设置标头(注意,这不是 标准 javascript http 请求对象,请检查它以获取详细信息).但是您确实可以访问此处的所有标头,因此您可以根据需要计算和注入它们.

Here you can set headers in the request object (note, this is not the standard javascript http request object, inspect it for details). But you do have access to all headers here, so you can calculate and inject them as needed.

这篇关于如何在请求之前在 Swagger 中计算 AWS 签名 V4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆