是的,可以使用AWS API Gateway和AWS Lambda钩子验证JWT令牌。下面是一些示例代码:
"authorizer": {
"type": "TOKEN",
"authorizerUri": "arn:aws:apigateway::lambda:path/2015-03-31/functions/arn:aws:lambda:::function:/invocations",
"authorizerResultTtlInSeconds": 0
}
const jwt = require('jsonwebtoken');
exports.handler = async (event) => {
const token = event.headers.Authorization;
// Verify token using jsonwebtoken library
const decodedToken = jwt.verify(token, process.env.JWT_SECRET);
// If token is invalid, return error
if (!decodedToken) {
return {
statusCode: 401,
body: 'Unauthorized'
}
}
// If token is valid, add user information to event object and call original function
event.user = decodedToken;
return originalFunction(event);
};
// Original Lambda function
const originalFunction = async (event) => {
// Your business logic here
};
exports.handler = middlewareFunction;