首页 文章

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

提问于
浏览
1

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

2 回答

  • 0

    您可以通过以下AWS SDK guideline for CORS in Java在AWS API Gateway中启用CORS .

    文档已经解释了CORS设置,其中包括:

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

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

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

    • 删除配置

  • 0

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

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

相关问题