

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

# HttpApiFunctionAuth
<a name="sam-property-function-httpapifunctionauth"></a>

在事件级别配置授权。

为特定 API\$1路径\$1方法配置身份验证

## 语法
<a name="sam-property-function-httpapifunctionauth-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-function-httpapifunctionauth-syntax.yaml"></a>

```
  [AuthorizationScopes](#sam-function-httpapifunctionauth-authorizationscopes): List
  [Authorizer](#sam-function-httpapifunctionauth-authorizer): String
```

## Properties
<a name="sam-property-function-httpapifunctionauth-properties"></a>

 `AuthorizationScopes`   <a name="sam-function-httpapifunctionauth-authorizationscopes"></a>
适用于此 API、路径和方法的授权范围。  
此处列出的范围将覆盖`DefaultAuthorizer`应用的任何范围（如果存在）。  
*类型*：列表  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Authorizer`   <a name="sam-function-httpapifunctionauth-authorizer"></a>
特定函数的`Authorizer`。要使用 IAM 授权，请在模板的 `Globals` 部分指定 `AWS_IAM`，并为 `EnableIamAuthorizer` 指定 `true`。  
如果您已在 API 中指定了全局授权方并想公开特定函数，请通过将 `Authorizer` 设置为 `NONE` 来覆盖。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-function-httpapifunctionauth--examples"></a>

### 函数身份验证
<a name="sam-property-function-httpapifunctionauth--examples--function-auth"></a>

在函数级别指定授权

#### YAML
<a name="sam-property-function-httpapifunctionauth--examples--function-auth--yaml"></a>

```
Auth:
  Authorizer: OpenIdAuth
  AuthorizationScopes:
    - scope1
    - scope2
```

### IAM 授权
<a name="sam-property-function-httpapifunctionauth--examples--iam-authorization"></a>

在事件级别指定 IAM 授权。要在事件级别使用 `AWS_IAM` 授权，还必须在模板的 `Globals` 部分为 `EnableIamAuthorizer` 指定 `true`。有关更多信息，请参阅 [模板的 “全局” 部分 AWS SAM](sam-specification-template-anatomy-globals.md)。

#### YAML
<a name="sam-property-function-httpapifunctionauth--examples--iam-authorization--yaml"></a>

```
Globals:
  HttpApi:
    Auth:
      EnableIamAuthorizer: true

Resources:
  HttpApiFunctionWithIamAuth:
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ApiEvent:
          Type: HttpApi
          Properties:
            Path: /iam-auth
            Method: GET
            Auth:
              Authorizer: AWS_IAM
      Handler: index.handler
      InlineCode: |
        def handler(event, context):
          return {'body': 'HttpApiFunctionWithIamAuth', 'statusCode': 200}
      Runtime: python3.9
```