最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

aws lambda - API Gateway Access Errors with Java SDK - Stack Overflow

programmeradmin1浏览0评论

I'm using the AWS SDK v2 in my Java program to send a request to an API Gateway I've hosted. I'm seeing some unexpected errors where I'm receiving a 403 Forbidden response from my gateway, but only in cases where I'm providing a request body via contentStreamProvider. If I remove the request body, the request passes my authorizer and is able to hit my lambda function. I'm really stumped on what the issue is.

The below code returns a 403 Forbidden Error from the APIG

        Map<String, String> payload = new HashMap<String, String>();
        payload.put("A", "foo");
        payload.put("B", "bar");

        String requestBody = objectMapper.writeValueAsString(payload);
        
        SdkHttpFullRequest request = SdkHttpFullRequest.builder()
            .method(SdkHttpMethod.POST)
            .protocol("https")
            .host(API_ENDPOINT)
            .encodedPath(PATH)
            .appendHeader("Content-Type", "application/json")
            .contentStreamProvider(() -> new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)))
            .build();

        Aws4Signer signer = Aws4Signer.create();
        Aws4SignerParams signerParams = Aws4SignerParams.builder()
            .signingName("execute-api")
            .signingRegion(Region.US_EAST_1)
            .awsCredentials(credentialsProvider.resolveCredentials())
            .build();

        SdkHttpFullRequest signedRequest = signer.sign(request, signerParams);
        
        HttpExecuteRequest req = HttpExecuteRequest.builder()
            .request(signedRequest)
            .build();

The below modification (removing the contentStreamProvider) passes the authorizer.

        SdkHttpFullRequest request = SdkHttpFullRequest.builder()
            .method(SdkHttpMethod.POST)
            .protocol("https")
            .host(API_ENDPOINT)
            .encodedPath(PATH)
            .appendHeader("Content-Type", "application/json")
            .build();

Any idea what the issue could be?

发布评论

评论列表(0)

  1. 暂无评论