我一直在使用无服务器框架创建一个应用程序,AWS 作为我的云提供商。我有一些用于创建用户、列出用户等的 HTTP 路由。
我正在使用 HTTP v2:
httpApi:
authorizers:
authAuthorizer:
type: request
functionName: authAuthorizer
但是,我需要为我拥有的每个端点定义自定义授权者:
createUser:
handler: src/createUser.main
events:
- httpApi:
method: POST
path: /users
authorizer:
name: authAuthorizer
是否可以为整个网关设置自定义授权者,然后我只需指定我不想受到保护的路由?这样可以防止忘记添加重要路线授权而将其公开等错误。
我查遍了也没找到相关内容。
也许可以设置自定义授权人..
尝试在您的
provider.httpApi.authorizers
文件中的 serverless.yml
上配置授权者。然后,您可以通过在函数定义的 authorizer
部分中指定 events
属性来配置预计访问受限的端点。
例如,
/private
路由受到authAuthorizer
授权者的保护,因此只有授权用户才能访问它。
httpApi:
authorizers:
authAuthorizer:
type: request
functionName: authAuthorizer
routes:
- path: /public
method: GET
authorizer: NONE
function: publicFunction
- path: /private
method: GET
authorizer: authAuthorizer
function: privateFunction
更多详细信息请参见:https://www.serverless.com/framework/docs/providers/aws/events/http-api