我正在尝试找到一种方法来允许在 AWS 的自定义授权方中使用访客模式。
基本上,我想要实现的是以下场景:
- 如果请求中没有
Authorization标头,则触发响应一些数据的 lambda 函数 - 如果有
Authorization标头,那么我的自定义授权者应该检查 JWT 令牌和AlloworDeny。custom-authorizer如果返回则触发 lambdaAllow
我看到我可以实现其中一个但不能同时实现,即我可以打开可以正常工作的端点(authorizer完全删除),或者我可以authorizer再次正常工作。
custom-authorizer然而,当没有Authorization标题时,我看不到绕过的方法。
无服务器中的示例配置:
functions:
hello:
handler: handler.hello
events:
- http:
path: /hello
method: get
private: true
authorizer:
identitySource: method.request.header.Authorization # Can this be optional?
name: custom-authorizer
custom-authorizer:
handler: authorizer.handler
从我的测试中,我可以确认一旦authorization存在并且Authorization请求中没有标头,那么我custom-authorizer根本不会被触发,API 网关会立即响应401 Unauthorized.
请注意,在我的访客模式下,我不想获得自定义 API 网关响应(这是可能的并且有效)。我想触发 lambda 函数,就像根本没有授权一样。
我得出结论这是不可能的,唯一的解决方法是删除authorization然后在 lambda 中执行一些自定义代码。
有什么建议吗?