AWS APIGateway CloudFormation指定方法所需的Api密钥?

时间:2019-01-08 01:59:56

标签: amazon-web-services amazon-cloudformation aws-api-gateway

我具有下面的CloudFormation模板,该模板可创建我的API网关(由Lambda支持)。我想启用API密钥,这是其中一种或多种方法的要求。我已经成功创建了API密钥,使用计划以及两者之间的关联,但是无法弄清楚如何为某些方法实际启用“ requires API Key”属性。 AWS的文档将“ ApiKeyRequired ”属性指定为 AWS :: ApiGateway :: Method 组件的一部分,但是我的CF模板没有或没有使用此组件?考虑到我以前从未需要过它,我不确定如何使用它?

我的模板如下:

 <ul id="sidebar" class="nav flex-column" onmouseenter="HoverOpenSideBar()" onmouseleave="HoverCloseSideBar()">

5 个答案:

答案 0 :(得分:1)

我认为在每个路径下添加public class Main extends Application { private LineChart<Number,Number> lineChart; @Override public void start(Stage stage) { createLinechart(); Button newWindowButton = new Button("Create window"); newWindowButton.setOnAction(event -> createWindow()); Scene scene = new Scene(newWindowButton); stage.setScene(scene); stage.show(); } private void createWindow(){ VBox vBox = new VBox(); vBox.getChildren().add(lineChart); Button stageCloseButton = new Button("stage.close();"); Button platformExitButton = new Button("Platform.exit()"); Button systemExitButton = new Button("System.exit(0)"); vBox.getChildren().addAll(stageCloseButton,platformExitButton,systemExitButton); Scene scene = new Scene(vBox); Stage stage = new Stage(); stage.setScene(scene); stage.show(); stageCloseButton.setOnAction(event -> stage.close()); platformExitButton.setOnAction(event -> Platform.exit()); systemExitButton.setOnAction(event -> System.exit(0)); } private void createLinechart(){ final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); xAxis.setLabel("Number of Month"); //creating the chart lineChart = new LineChart<>(xAxis, yAxis); lineChart.setTitle("Stock Monitoring, 2010"); //defining a series XYChart.Series series = new XYChart.Series(); series.setName("My portfolio"); //populating the series with data series.getData().add(new XYChart.Data(1, 23)); series.getData().add(new XYChart.Data(2, 14)); series.getData().add(new XYChart.Data(3, 15)); series.getData().add(new XYChart.Data(4, 24)); series.getData().add(new XYChart.Data(5, 34)); series.getData().add(new XYChart.Data(6, 36)); series.getData().add(new XYChart.Data(7, 22)); series.getData().add(new XYChart.Data(8, 45)); series.getData().add(new XYChart.Data(9, 43)); series.getData().add(new XYChart.Data(10, 17)); series.getData().add(new XYChart.Data(11, 29)); series.getData().add(new XYChart.Data(12, 25)); lineChart.getData().add(series); } public static void main(String[] args) { launch(args); } } ,然后在stage.close()下添加security

securityDefinitions

答案 1 :(得分:0)

我遇到了同样的问题,并通过使用以下命令放弃了在AWS :: ApiGateway :: RestApi中使用Body属性,从而解决了该问题:

 "ServerlessRestApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "DependsOn": "AspNetCoreFunction",
        "Properties": {
           "Description":"My Api Gateway",
            "ApiKeySourceType" : "HEADER",      
            "EndpointConfiguration" : {  "Types" : [ "REGIONAL" ]}
        }
    },

然后,我创建了一个代理资源。对于您的情况,您将为每个路径创建一个资源。在我有“ {proxy +}”的地方,您将有“ / list / tables”。

"ProxyResource": {
    "Type": "AWS::ApiGateway::Resource",
    "Properties": {
        "RestApiId": {
            "Ref": "ServerlessRestApi"
        },
        "ParentId": {
            "Fn::GetAtt": [
                "ServerlessRestApi",
                "RootResourceId"
            ]
        },
        "PathPart": "{proxy+}"
    }
},

最后,我能够定义一个AWS :: ApiGateway :: Method然后强制使用API​​密钥:

"CoreApiPostMethod":
  {
    "Type": "AWS::ApiGateway::Method",
     "DependsOn" : ["AspNetCoreFunction", "ServerlessRestApi"],
    "Properties":
    {
     "AuthorizationType" :"NONE",
      "OperationName" : "My API Post Request",

     "ApiKeyRequired" : true,
            "ResourceId": { "Ref": "ProxyResource"  },
    "RestApiId": {
      "Ref": "ServerlessRestApi"
    },
     "HttpMethod" : "POST",
      "Integration" : {  
       "ConnectionType" :  "INTERNET",
          "IntegrationHttpMethod" : "POST",
       "Type" : "AWS_PROXY",
        "Uri" : {
                          "Fn::Sub":"arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AspNetCoreFunction.Arn}/invocations"
                }
      }        
    }

  },

,然后对其他HTTP方法采用相同的模式。它比原始配置更冗长,但确实可以让您更好地控制方法配置。

答案 2 :(得分:0)

迟到的聚会。

status of 403

"x-amazon-apigateway-api-key-source" : "HEADER",

"securityDefinitions": {
    "<SOME_NAME>": {
        "type": "apiKey",
        "name": "x-api-key",
        "in": "header"
    }
 }

所以可能的可行解决方案是

"security" : [{
    "<SOME_NAME>" : []
}]

答案 3 :(得分:0)

"security" : [{
                "myKey" : []
            }],

"myKey": {
            "type": "apiKey",
            "name": "x-api-key",
            "in": "header"
        },

在body中添加安全元素,并在securityDefinitions中添加myKey元素对我来说都是有效的。

答案 4 :(得分:0)

完整指南here。本指南提供了用于为任何API网关方法启用API密钥的基本设置。

使用AWS::Serverless::Api定义您的API。它支持Auth属性,该属性具有一个名为ApiKeyRequired的属性。将此设置为true。

应该遵循上述指南中的代码片段。

AuthApiGateway:
    Type: AWS::Serverless::Api
    Properties:
       StageName: Prod
       Auth:
           ApiKeyRequired: 'true' # This makes passing ApiKey mandatory
       DefinitionBody:
           swagger: '2.0'
           info: ...