【发布时间】:2020-05-28 16:56:45
【问题描述】:
我按照在线教程和示例了解如何为我的 lambda 函数实现自定义授权器。不幸的是,我在访问我的函数中的授权者上下文时遇到了麻烦。执行console.log(event.requestContext.authorizer); 只会导致undefined。我错过了什么吗?这是一些sn-p。
# serverless.yml
...
myAuthorizer:
handler: src/functions/myAuthorizer/index.handler
someFunction:
handler: src/functions/someFunction/index.handler
events:
- http:
method: GET
path: /hello-world
authorizer:
name: myAuthorizer
identitySource: method.request.header.Authorization
// myAuthorizer.ts
export async function handler(
event: AWSLambda.CustomAuthorizerEvent,
context: AWSLambda.Context
): Promise<AWSLambda.AuthResponse> {
const user = {id: 1, email: 'user@test.com'};
// some logic...
return {
principalId: user.id,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: '*',
Effect: 'Allow',
Resource: '*',
},
],
},
context: { user },
};
}
// someFunction.ts
export async function handler(
event: AWSLambda.APIGatewayEvent,
context: AWSLambda.Context
): Promise<AWSLambda.APIGatewayProxyResult> {
console.log(event.requestContext.authorizer); // <-- this is undefined
console.log(Object.keys(event.requestContext)); // <-- no "authorizer" property
return {
statusCode: 200,
body: JSON.stringify({hello: 'world'}),
};
}
提前致谢!
【问题讨论】:
标签: javascript aws-lambda serverless-framework aws-serverless