amazon-web-services - AWS APIGateway CloudFormation 指定方法所需的 API 密钥?
问题描述
我有以下 CloudFormation 模板,它创建了我的 API 网关(由 Lambda 支持)。我想启用 API 密钥作为其中一种或多种方法的要求。我已成功创建 API 密钥、使用计划和两者之间的关联,但无法弄清楚如何为某些方法实际启用“需要 API 密钥”属性。AWS 的文档将“ ApiKeyRequired ”属性指定为AWS::ApiGateway::Method组件的一部分,但我的 CF 模板没有或没有使用此组件?考虑到我以前从未需要它,我不确定如何使用它?
我的模板如下:
"ServerlessRestApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Description":"This is a placeholder for the description of this web api",
"ApiKeySourceType":"HEADER",
"Body": {
"info": {
"version": "1.0",
"title": {
"Ref": "AWS::StackName"
}
},
"paths": {
"/list/tables": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableList.Arn}/invocations"
}
},
"security": [
{
"api_key": []
}
],
"responses": {}
}
},
"/list/columns/{tableid}": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetColumnList.Arn}/invocations"
}
},
"responses": {}
}
},
"datagw/general/table/get/{tableid}": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableResponse.Arn}/invocations"
}
},
"responses": {}
}
},
"/": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Get.Arn}/invocations"
}
},
"responses": {}
}
},
"/tables/{tableid}/{columnid}": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetTableBasic.Arn}/invocations"
}
},
"responses": {}
}
},
"securityDefinitions": {
"type": "api_key",
"name": "x-api-key",
"in": "header"
}
},
"swagger": "2.0"
}
}
},
解决方案
我认为在每条路径下添加security
然后在下添加会起作用。securityDefinitions
paths
"paths": {
"/list/tables": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"type": "aws_proxy",
"uri": {
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-
03-31/functions/${GetTableList.Arn}/invocations"
}
},
"security": [
{
"api_key": []
}
]
}
}
},
"securityDefinitions": {
"type": "api_key",
"name": "x-api-key",
"in": "header"
}
推荐阅读
- c# - XAML 二进制格式 (XBF) 生成器在使用 Visual Studio 的 UWP 中报告语法错误“0x09c6”
- typescript - ESlint import/no-unresolved with Babel plugin root import
- microsoft-graph-api - Microsoft Graph 用户上次修改字段
- javascript - 使用 React 生命周期方法为子组件中的每个用户发出 POST 请求并更新数据库
- database - 聚合查询的计数不正确
- java - 为 Tab 和移动设备设计 Android App UI
- php - 未加载 Mysql 扩展
- azure - 使用自定义属性在 Azure AD B2C 中存储有关用户的其他信息
- javascript - 如何在本机反应中通过其索引号从状态更改颜色
- python - 每次用户单击时如何用颜色填充矩形?游戏