如何在自定义授权方 AWS lambda 函数中访问 http headers
How to access http headers in custom authorizer AWS lambda function
从 API 网关,我使用 python 中的 Lambda 函数为我的 API 创建了一个 custom authorizer。 API 网关使用我配置的 header(method.request.header.Authorization
)移交传入的身份验证令牌。但是,我还需要我的 lambda 函数中原始 http 请求的其他 headers。我如何访问它们?我没有在 event
object 上看到 headers 输入到我的 lambda 函数。
请注意,这不是 的副本。问题是关于自定义授权方 lambda 函数的。我没有看到任何将传入的 http headers 传递给授权方 lambda 函数的配置选项。
根据 AWS Documentation,API 网关使用以下输入调用自定义授权器。根据以下内容,我认为我的问题是不可能的。但想检查是否有解决方法。
{
"type":"TOKEN",
"authorizationToken":"",
"methodArn":"arn:aws:execute-api:<regionId>:<accountId>:<apiId>/<stage>/<method>/<resourcePath>"
}
现在可以通过使用 'Request' 类型的 Authoriser 代替 Token
完整的细节在这里:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
基本上,所有 headers 都在事件 object 中传递以请求授权
即 headers object 事件
"headers": {
"X-wibble": "111",
"X-wobble": "222",
"x-amzn-ssl-client-hello": "*Deleted*",
"Via": "1.1 .cloudfront.net (CloudFront)",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Forwarded-Proto": "https",
"X-Forwarded-For": "*Deleted*",
"CloudFront-Viewer-Country": "GB",
"Accept": "*/*",
"User-Agent": "curl/7.55.1",
"X-Amzn-Trace-Id": "Root=*Deleted*",
"Host": "*Deleted*.execute-api.eu-west-1.amazonaws.com",
"X-Forwarded-Proto": "https",
"X-Amz-Cf-Id": "*Deleted*",
"CloudFront-Is-Tablet-Viewer": "false",
"X-Forwarded-Port": "443",
"CloudFront-Is-Mobile-Viewer": "false"
}
这是一个 SAM 模板:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
Authorizers:
MyAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: !GetAtt AuthLambda.Arn
Identity:
Headers:
- X-API-KEY
- X-API-ID
有几种方法可以做到。
您可以定义 SAM 模板(API 网关),在 headers 下,您可以定义多个 headers,您可以在应用程序中检索它们。
在请求中,可以获取多个customheaders
"headers": {
"Access-Control-Allow-Origin": {
"type": "string",
"description": "URI that may access the resource"
},
"Access-Control-Allow-Methods": {
"type": "string",
"description": "Method or methods allowed when accessing the resource"
},
"Access-Control-Allow-Headers": {
"type": "string",
"description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request."
}
}
关注 link 会有所帮助
从 API 网关,我使用 python 中的 Lambda 函数为我的 API 创建了一个 custom authorizer。 API 网关使用我配置的 header(method.request.header.Authorization
)移交传入的身份验证令牌。但是,我还需要我的 lambda 函数中原始 http 请求的其他 headers。我如何访问它们?我没有在 event
object 上看到 headers 输入到我的 lambda 函数。
请注意,这不是
根据 AWS Documentation,API 网关使用以下输入调用自定义授权器。根据以下内容,我认为我的问题是不可能的。但想检查是否有解决方法。
{ "type":"TOKEN", "authorizationToken":"", "methodArn":"arn:aws:execute-api:<regionId>:<accountId>:<apiId>/<stage>/<method>/<resourcePath>" }
现在可以通过使用 'Request' 类型的 Authoriser 代替 Token
完整的细节在这里: https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
基本上,所有 headers 都在事件 object 中传递以请求授权
即 headers object 事件
"headers": {
"X-wibble": "111",
"X-wobble": "222",
"x-amzn-ssl-client-hello": "*Deleted*",
"Via": "1.1 .cloudfront.net (CloudFront)",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Forwarded-Proto": "https",
"X-Forwarded-For": "*Deleted*",
"CloudFront-Viewer-Country": "GB",
"Accept": "*/*",
"User-Agent": "curl/7.55.1",
"X-Amzn-Trace-Id": "Root=*Deleted*",
"Host": "*Deleted*.execute-api.eu-west-1.amazonaws.com",
"X-Forwarded-Proto": "https",
"X-Amz-Cf-Id": "*Deleted*",
"CloudFront-Is-Tablet-Viewer": "false",
"X-Forwarded-Port": "443",
"CloudFront-Is-Mobile-Viewer": "false"
}
这是一个 SAM 模板:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
Authorizers:
MyAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: !GetAtt AuthLambda.Arn
Identity:
Headers:
- X-API-KEY
- X-API-ID
有几种方法可以做到。
您可以定义 SAM 模板(API 网关),在 headers 下,您可以定义多个 headers,您可以在应用程序中检索它们。
在请求中,可以获取多个customheaders
"headers": { "Access-Control-Allow-Origin": { "type": "string", "description": "URI that may access the resource" }, "Access-Control-Allow-Methods": { "type": "string", "description": "Method or methods allowed when accessing the resource" }, "Access-Control-Allow-Headers": { "type": "string", "description": "Used in response to a preflight request to indicate which HTTP headers can be used when making the request." } }
关注 link 会有所帮助