【问题标题】:cdk http api cors configuration not workingcdk http api cors 配置不起作用
【发布时间】:2022-01-15 13:07:13
【问题描述】:

我有这段代码可以创建一个 http api,但是 cors 配置无法正常工作,即使它在部署后在控制台中正确显示。我仍然可以从邮递员发送请求并在 api 后面执行 lambda,考虑到这种 cors 配置,这不应该发生。

const someApiDomain = new apigateway.DomainName(this, 'mydomain', {
  domainName: 'api.example.com',
  endpointType: apigateway.EndpointType.REGIONAL,
  certificate: someCert
})

const httpApi = new apigateway.HttpApi(this, 'my-api', {
  apiName: `my-api`,
  createDefaultStage: true,
  disableExecuteApiEndpoint: true,
  defaultDomainMapping: {
    domainName: someApiDomain
  },
  corsPreflight: {
    allowHeaders: [
      'Content-Type',
      'X-Amz-Date'
    ],
    allowMethods: [
      apigateway.CorsHttpMethod.OPTIONS,
      apigateway.CorsHttpMethod.POST,
    ],
    allowCredentials: false,
    allowOrigins: ['https://example.com']
  },
});

httpApi.addRoutes({
  methods: [apigateway.HttpMethod.POST],
  path: '/myapi',
  integration: new apigatewayintegrations.LambdaProxyIntegration({
    handler: myFunction,
  }),
});

new route53.ARecord(this, 'myapirecord', {
  zone: someHostedZone,
  recordName: 'api.example.com',
  target: route53.RecordTarget.fromAlias(
    new route53targets.ApiGatewayv2DomainProperties(someApiDomain.regionalDomainName, someApiDomain.regionalHostedZoneId)
  ),
});

route53 记录是否在这里绕过了某些东西?

【问题讨论】:

    标签: cors aws-api-gateway aws-cdk aws-http-api


    【解决方案1】:

    这是预期的行为。 CORS 检查总是由浏览器执行,但是像 Postman 和 curl do not make CORS checks 这样的工具。

    CDK 的 ApiGatewayV2 HttpApi 构造支持 access control。 ApiGateway RestApi 具有 broader auth capabilities,包括 API 密钥。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2020-06-23
      • 2021-09-15
      • 2018-03-05
      • 2016-10-25
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多