无服务器自定义授权器结果未定义
Serverless Custom Authorizer results to undefined
我按照在线教程和示例学习如何为我的 lambda 函数实现自定义授权方。不幸的是,我无法访问函数内的授权方上下文。执行 console.log(event.requestContext.authorizer);
只会导致 undefined
。我错过了什么吗?这是一些片段。
# 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'}),
};
}
提前致谢!
在 myAuthorizer
处理函数中,使用 callback
风格而不是 async/await
风格。
export async function handler(
event: AWSLambda.CustomAuthorizerEvent,
context: AWSLambda.Context
): Promise<void> { // change to void
const user = { id: 1, email: 'user@test.com' };
// some logic...
context.succeed({ // instead of return policy object
principalId: user.id,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: '*',
Effect: 'Allow',
Resource: '*',
},
],
},
context: { user },
});
}
我按照在线教程和示例学习如何为我的 lambda 函数实现自定义授权方。不幸的是,我无法访问函数内的授权方上下文。执行 console.log(event.requestContext.authorizer);
只会导致 undefined
。我错过了什么吗?这是一些片段。
# 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'}),
};
}
提前致谢!
在 myAuthorizer
处理函数中,使用 callback
风格而不是 async/await
风格。
export async function handler(
event: AWSLambda.CustomAuthorizerEvent,
context: AWSLambda.Context
): Promise<void> { // change to void
const user = { id: 1, email: 'user@test.com' };
// some logic...
context.succeed({ // instead of return policy object
principalId: user.id,
policyDocument: {
Version: '2012-10-17',
Statement: [
{
Action: '*',
Effect: 'Allow',
Resource: '*',
},
],
},
context: { user },
});
}