-1

我有这个创建 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 记录是否在这里绕过了某些东西?

4

1 回答 1

0

这是预期的行为。CORS 检查总是由浏览器执行,但 Postman 和 curl 等工具不会进行 CORS 检查

CDK 的 ApiGatewayV2HttpApi构造支持访问控制。ApiGatewayRestApi具有更广泛的身份验证功能,包括 API 密钥。

于 2021-12-11T10:31:36.697 回答