选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

Lambda 授权方示例 AWS SAM

聚焦模式
Lambda 授权方示例 AWS SAM - AWS Serverless Application Model

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

AWS::Serverless::Api 资源类型支持两种类型的 Lambda 授权方:TOKEN 授权方和 REQUEST 授权方。AWS::Serverless::HttpApi 资源类型仅支持 REQUEST 授权方。下面是每个类型的示例。

Lambda TOKEN 授权方示例 (AWS::Serverless::Api)

您可以 APIs 通过在模板中定义 Lambda TOKEN 授权机构来控制对您的访问权限。 AWS SAM 为此,您需要使用 ApiAuth 数据类型。

以下是 Lambda TOKEN 授权方的示例 AWS SAM 模板部分:

注意

在以下示例中,SAM FunctionRole 是隐式生成的。

Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaTokenAuthorizer Authorizers: MyLambdaTokenAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x

有关 Lambda 授权方的更多信息,请参阅《API Gateway 开发人员指南》中的使用 API Gateway Lambda 授权方

Lambda REQUEST 授权方示例 (AWS::Serverless::Api)

您可以 APIs 通过在模板中定义 Lambda REQUEST 授权机构来控制对您的访问权限。 AWS SAM 为此,您需要使用 ApiAuth 数据类型。

以下是 Lambda REQUEST 授权方的示例 AWS SAM 模板部分:

Resources: MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionPayloadType: REQUEST FunctionArn: !GetAtt MyAuthFunction.Arn Identity: QueryStrings: - auth MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: Api Properties: RestApiId: !Ref MyApi Path: / Method: get MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x

有关 Lambda 授权方的更多信息,请参阅《API Gateway 开发人员指南》中的使用 API Gateway Lambda 授权方

Lambda 授权方示例 (AWS::Serverless::HttpApi)

您可以 APIs 通过在模板中定义 Lambda 授权机构来控制对 HTTP 的访问。 AWS SAM 为此,您需要使用 HttpApiAuth 数据类型。

以下是 Lambda 授权方的示例 AWS SAM 模板部分:

Resources: MyApi: Type: AWS::Serverless::HttpApi Properties: StageName: Prod Auth: DefaultAuthorizer: MyLambdaRequestAuthorizer Authorizers: MyLambdaRequestAuthorizer: FunctionArn: !GetAtt MyAuthFunction.Arn FunctionInvokeRole: !GetAtt MyAuthFunctionRole.Arn Identity: Headers: - Authorization AuthorizerPayloadFormatVersion: 2.0 EnableSimpleResponses: true MyFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: index.handler Runtime: nodejs12.x Events: GetRoot: Type: HttpApi Properties: ApiId: !Ref MyApi Path: / Method: get PayloadFormatVersion: "2.0" MyAuthFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: authorizer.handler Runtime: nodejs12.x
隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。