

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

您可以在 [OpenID Connect (OIDC)](https://openid.net/specs/openid-connect-core-1_0.html) 和 OAuth 2.0 架構中使用 JWTs控制對 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 授權方的詳細資訊，請參閱《 [ APIs Gateway 開發人員指南》中的使用 JWT 授權方控制 HTTP API 的存取](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html)。 **