phi*_*sch 5 amazon-web-services aws-cloudformation swagger aws-api-gateway
我尝试使用Swagger / OpenAPI定义我的AWS Api Gateway基础架构。到目前为止,一切都工作正常,但是我无法在端点上使用API密钥。
我的Swagger文件看起来像这样(缩短):
---
swagger: 2.0
basePath: /dev
info:
title: My API
description: Proof of concept
schemes:
- https
securityDefinitions:
api_key:
type: apiKey
name: X-Api-Key
in: header
paths:
/example-path:
options:
consumes:
- application/json
produces:
- application/json
x-amazon-apigateway-integration:
type: mock
requestTemplates:
application/json: |
{
"statusCode" : 200
}
responses:
"default":
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'GET,HEAD,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
responseTemplates:
application/json: |
{}
responses:
200:
description: Default response for CORS method
headers:
Access-Control-Allow-Headers:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Origin:
type: "string"
get:
security:
- api_key: []
x-amazon-apigateway-integration:
# Further definition of the endpoint, calling Lambda etc...
Run Code Online (Sandbox Code Playgroud)
链接到CloudFormation模板内部的Swagger文件已成功处理。但是,当我在AWS Web Console中打开终端节点时,要求API Key的标志仍为false。
有什么建议么?谢谢。
要启用所需的 API 密钥,您需要将其添加"x-amazon-apigateway-api-key-source" : "HEADER"
到安全方案块中。
看一个例子:
"components" : {
"securitySchemes" : {
"api-key" : {
"type" : "apiKey",
"name" : "x-api-key",
"in" : "header",
"x-amazon-apigateway-api-key-source" : "HEADER"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个使用代理请求的示例。你的 JSON 应该是这样的:openapi3
{
"openapi": "3.0.3",
"info": {
"title": "User Portal",
"description": "API focused in User Portal.",
"version": "v1"
},
"paths": {
"users/{proxy+}": {
"options": {
"x-amazon-apigateway-integration": {
"httpMethod": "OPTIONS",
"payloadFormatVersion": "1.0",
"type": "MOCK"
}
},
"x-amazon-apigateway-any-method": {
"produces":[ "application/json"],
"parameters": [
{
"name": "proxy",
"in": "path",
"required": "true",
"type": "string"
}
],
"responses": {},
"security": [
{
"api-key": []
}
],
"x-amazon-apigateway-integration": {
"uri":"https://test.com.br/users/{proxy}",
"httpMethod":"ANY",
"type": "HTTP_PROXY"
}
}
}
},
"components" : {
"securitySchemes" : {
"api-key" : {
"type" : "apiKey",
"name" : "x-api-key",
"in" : "header",
"x-amazon-apigateway-api-key-source" : "HEADER"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 openapi2 中,您可以将其添加到您的 yml 中。
swagger: 2.0
basePath: /dev
info:
title: My API
description: Proof of concept
schemes:
- https
securityDefinitions:
api_key:
type: apiKey
name: X-Api-Key
in: header
x-amazon-apigateway-api-key-source: HEADER
Run Code Online (Sandbox Code Playgroud)
如果您在使用 api 与 openapi 集成时遇到问题,可以查看这篇文章:使用 OpenAPI 的 API Gateway 扩展
归档时间: |
|
查看次数: |
2692 次 |
最近记录: |