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

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

问题描述

对于我们的AWS API端点,我们使用AWS_IAM授权,并希望通过Swagger UI进行调用. 要成功拨打电话,必须有2个标题"Authorization"和"x-amz-date".要形成授权",我们使用遵循AWS文档中的步骤. 我们必须在每次调用中更改"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).

谢谢.

推荐答案

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;
  }
}

并将其应用于创建时的招摇实例:

and apply it to your swagger instance on creation:

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

您可以在此处在request对象中设置标头(请注意,这不是 standard 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天全站免登陆