

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

# OAuth 2.0/JWT 授权方示例 AWS SAM
<a name="serverless-controlling-access-to-apis-oauth2-authorizer"></a>

 JWTs [作为 [OpenID Connect (OIDC) 和](https://openid.net/specs/openid-connect-core-1_0.html) 2.0 框架的一部分，你可以控制对你的 APIs 使用的访问权限。OAuth ](https://oauth.net/2/)为此，您需要使用 [HttpApiAuth](sam-property-httpapi-httpapiauth.md) 数据类型。

以下是 OAuth 2.0/JWT 授权方的示例 AWS SAM 模板部分：

```
Resources:
  MyApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Auth:
        Authorizers:
          MyOauth2Authorizer:
            AuthorizationScopes:
              - scope
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              audience:
                - audience1
                - audience2
              issuer: "https://www.example.com/v1/connect/oidc"
        DefaultAuthorizer: MyOauth2Authorizer
      StageName: Prod
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./src
      Events:
        GetRoot:
          Properties:
            ApiId: MyApi
            Method: get
            Path: /
            PayloadFormatVersion: "2.0"
          Type: HttpApi
      Handler: index.handler
      Runtime: nodejs12.x
```

*有关 OAuth 2.0/JWT 授权方的更多信息，请参阅《API Gateway 开发者指南》[中的 “ APIs 使用 JWT 授权者控制 HTTP 访问权限](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html)”。*