【问题标题】:How to get api gateway key name in aws lambda function如何在 aws lambda 函数中获取 api 网关密钥名称
【发布时间】:2019-08-29 12:39:28
【问题描述】:

我在 "Api-gateway" 中创建 api 并在方法执行设置中将 "API Key Required" 设置为 true,但在 lambda 函数中,我仅从请求标头中获取 "apiKeyId"。有什么办法也可以得到apiKeyName

【问题讨论】:

标签: amazon-web-services aws-lambda aws-api-gateway


【解决方案1】:

简而言之,ApiKey 名称在执行的 lambda 中不可用。您只能使用 SDK 查询所有键,然后使用代码手动过滤。

附带说明,您还可以在自定义授权方中执行此操作,并将名称映射到调用上下文。这样,您只需编写一次代码,所有 lambda 表达式都会将参数作为上下文变量。这个实现的另一个好处是,自定义授权的结果被缓存了。

nodejs implementation of custom authorizer with apikey name mapper

【讨论】:

    【解决方案2】:

    【讨论】:

    • 有没有办法让 apiKeyName 在 lamda 函数的请求头中可用?例如在 event.requestContext.identity 我得到了价值信息。 "identity": { "cognitoIdentityPoolId": null, "cognitoIdentityId": null, "apiKey": "iqUQOowmC7", "cognitoAuthenticationType": null, "userArn": null, "apiKeyId": "r3k", "userAgent": "PostmanRuntime/7.6.0", } 我也需要 apiKeyName。
    【解决方案3】:

    有点晚了,但这里有一种获取 API 密钥详细信息的方法,因为其他人说有必要获取它们。

    import { APIGateway } from 'aws-sdk'
    // Add the below to your handler
    const apiKey = event.requestContext.identity.apiKey 
    const apiKeyDetails = await new APIGateway().getApiKey({ apiKey }).promise()
    

    现在可以获取 api 键名、标签、描述等。

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2017-07-26
      • 2018-05-18
      • 2022-08-19
      • 2018-09-04
      • 2018-03-15
      • 1970-01-01
      • 2017-05-14
      相关资源
      最近更新 更多