【问题标题】:Access POST Request body from Custom Authorizer Lambda Function从自定义授权方 Lambda 函数访问 POST 请求正文
【发布时间】:2018-05-04 04:04:48
【问题描述】:

我在 AWS 中配置了一个自定义授权器 lambda 函数,用于将 API 配置为具有 POST 资源的另一个 Lambda 函数。

授权方设置为 Request 类型,而不是 Token,因为我没有在 Authorization 标头中使用令牌,而是想要访问正在发布的表单数据。

当我检查我的授权函数的 event 参数的内容时,我看不到任何原始的 POST 请求正文(表单数据)。但是,如果我将资源更改为GET,我会按预期看到queryStringParameters 中的表单数据。

(注意下面当请求为 POST 时,queryStringParameters 始终为空对象)

当使用POST 方法时,是否可以从函数中的请求访问表单数据?

以下是使用 POST 时授权函数的 event 参数将包含的内容的示例:

{
  type: 'REQUEST',
  methodArn: 'arn:aws:execute-api:us-east-1:********:********/dev/POST/receive',
  resource: '/receive',
  path: '/sms/receive',
  httpMethod: 'POST',
  headers: {
    Accept: '*/*',
    'CloudFront-Viewer-Country': 'US',
    'CloudFront-Forwarded-Proto': 'https',
    'CloudFront-Is-Tablet-Viewer': 'false',
    'CloudFront-Is-Mobile-Viewer': 'false',
    'User-Agent': 'TwilioProxy/1.1',
    'X-Forwarded-Proto': 'https',
    'CloudFront-Is-SmartTV-Viewer': 'false',
    Host: 'api.myredactedcompany.io',
    'X-Forwarded-Port': '443',
    'X-Amzn-Trace-Id': 'Root=**************',
    Via: '1.1 ***************.cloudfront.net (CloudFront)',
    'Cache-Control': 'max-age=259200',
    'X-Twilio-Signature': '***************************',
    'X-Amz-Cf-Id': '****************************',
    'X-Forwarded-For': '[redacted IP addresses]',
    'Content-Length': '492',
    'CloudFront-Is-Desktop-Viewer': 'true',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  queryStringParameters: {},
  pathParameters: {},
  stageVariables: {},
  requestContext: {
    path: '/sms/receive',
    accountId: '************',
    resourceId: '*****',
    stage: 'dev',
    requestId: '5458adda-ce2c-11e7-ba08-b7e69bc7c01c',
    identity: {
      cognitoIdentityPoolId: null,
      accountId: null,
      cognitoIdentityId: null,
      caller: null,
      apiKey: '',
      sourceIp: '[redacted IP]',
      accessKey: null,
      cognitoAuthenticationType: null,
      cognitoAuthenticationProvider: null,
      userArn: null,
      userAgent: 'TwilioProxy/1.1',
      user: null
    },
    resourcePath: '/receive',
    httpMethod: 'POST',
    apiId: '*******'
  }
}

【问题讨论】:

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


    【解决方案1】:

    根据this answer 的类似问题,似乎没有将正文提供给自定义授权人。文档here 没有列出正文参数。我认为可能的想法是授权者应该依赖路由和标头,而不是进入应用程序级的正文数据。

    【讨论】:

    • 这有点傻,因为亚马逊自己的身份验证机制需要完整的正文内容。如果能够实现一个基于签名的授权器以获得更好的安全性,那就太好了。
    【解决方案2】:

    如果您需要实现基于签名的授权方,您可以使用 Lambda@Edge。它适用于云端,您可以拦截查看器请求、源请求、源响应和查看器响应中的事件。

    收到的字段: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#request-event-fields-request

    注意正文大小限制,如果超出大小限制,正文将被截断。 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-at-the-edge-body-size-limits-lambda-at-edge

    【讨论】:

      猜你喜欢
      • 2016-11-30
      • 2022-12-09
      • 1970-01-01
      • 2022-06-14
      • 2023-02-06
      • 2019-06-10
      • 2017-04-29
      • 2016-11-09
      • 2018-01-28
      相关资源
      最近更新 更多