如何通过 SDK Java 在 AWS API 网关中启用 CORS

How to enable CORS in AWS API Gateway via SDK Java

我正在开发一项通过 sdk Java 直接与 AWS 通信的服务,当我创建 API 网关时,我没有看到任何启用 CORS 的选项,就像我可以通过 AWS 控制台 UI,有什么办法可以通过 sdk 做到这一点吗?

您可以按照 AWS SDK guideline for CORS in Java.

在 AWS API 网关中启用 CORS

文档用示例解释了 CORS 设置,包括:

  • 创建 CORS 配置并在存储桶上设置配置

  • 检索配置并通过添加规则对其进行修改

  • 将修改后的配置添加到存储桶

  • 删除配置

我通过手动创建方法并配置 headers 找到了答案,例如:

AmazonApiGateway amazonApiGateway;

private void putMethod(final RestApi restApi, final Resource resource, final HttpMethod httpMethod) throws AmazonClientException {
    PutMethodRequest putMethodRequest = new PutMethodRequest()
        .withOperationName(METHOD_OPERATION_NAME)
        .withHttpMethod(httpMethod.name())
        .withResourceId(resource.getId())
        .withRestApiId(restApi.getId())
        .withAuthorizationType("NONE");

    amazonApiGateway.putMethod(putMethodRequest);
}

private void putMethodIntegration(final RestApi restApi, final Resource resource, final HttpMethod httpMethod) throws AmazonClientException {
    PutIntegrationRequest putIntegrationRequest = new PutIntegrationRequest()
        .withRestApiId(restApi.getId())
        .withResourceId(resource.getId())
        .withHttpMethod(httpMethod.name())
        .withIntegrationHttpMethod(httpMethod.name())
        .withType(IntegrationType.HTTP);

    amazonApiGateway.putIntegration(putIntegrationRequest);
}

private void putMethodResponse(final RestApi restApi, final Resource resource, final HttpMethod httpMethod, final Map<String, Boolean> methodResponseParameters) throws AmazonClientException {
    amazonApiGateway.putMethodResponse(new PutMethodResponseRequest()
        .withRestApiId(restApi.getId())
        .withResourceId(resource.getId())
        .withHttpMethod(httpMethod.name())
        .withStatusCode("200")
        .withResponseModels(Collections.singletonMap("application/json", "Empty"))
        .withResponseParameters(methodResponseParameters));
}

private void putMethodIntegrationResponse(final RestApi restApi, final Resource resource, final HttpMethod httpMethod, final Map<String, String> parameters) throws AmazonClientException{
    final Map<String, String> dataPassThroughTemplate = new HashMap<>();
    dataPassThroughTemplate.put("application/json", "");

    amazonApiGateway.putIntegrationResponse(new PutIntegrationResponseRequest()
        .withRestApiId(restApi.getId())
        .withResourceId(resource.getId())
        .withHttpMethod(httpMethod.name())
        .withStatusCode("200")
        .withResponseParameters(parameters)
        .withResponseTemplates(dataPassThroughTemplate));
}

private Map<String, Boolean> getMethodResponseParameters() {
    final Map<String, Boolean> methodResponseParameters = new HashMap<>();
    methodResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Origin"), true);
    methodResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Headers"), true);
    methodResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Methods"), true);
    return methodResponseParameters;
}

private Map<String, String> getIntegrationResponseParameters() {
    final Map<String, String> integrationResponseParameters = new HashMap<>();
    integrationResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Origin"), "'*'");
    integrationResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Headers"), "'Content-Type,X-Amz-Date,Authorization,x-api-key,x-amz-security-token'");
    integrationResponseParameters.put(String.format(METHOD_RESPONSE_TEMPLATE, "Access-Control-Allow-Methods"), "'GET,POST,DELETE,PUT,PATCH,OPTIONS'");
    return integrationResponseParameters;
}