代码之家  ›  专栏  ›  技术社区  ›  Luan Kevin Ferreira

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

  •  1
  • Luan Kevin Ferreira  · 技术社区  · 7 年前

    2 回复  |  直到 7 年前
        1
  •  0
  •   NAVIN    7 年前

    您可以通过以下方式在AWS API网关中启用CORS AWS SDK guideline for CORS in Java .

    本文件以示例说明了CORS设置,包括:

    • 创建CORS配置并在bucket上设置配置

    • 将修改后的配置添加到bucket中

        2
  •  0
  •   Luan Kevin Ferreira    7 年前

    我是通过手动创建方法并配置头来解决的,例如:

    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;
    }