From 7dff7319a6f80f68d9de0c63f5852eee00b5abe4 Mon Sep 17 00:00:00 2001 From: Gaurav Ojha Date: Wed, 3 Aug 2022 11:00:53 -0700 Subject: [PATCH] Support spring.codec.max-in-memory-size configuration for CacheRequestBodyFilter Fixes gh-2693 Fixes gh-2797 --- .../gateway/config/GatewayAutoConfiguration.java | 5 +++-- .../CacheRequestBodyGatewayFilterFactory.java | 5 +++++ .../CacheRequestBodyGatewayFilterFactoryTests.java | 14 +++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 77a7706f6..534277df3 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -549,8 +549,9 @@ public class GatewayAutoConfiguration { @Bean @ConditionalOnEnabledFilter - public CacheRequestBodyGatewayFilterFactory cacheRequestBodyGatewayFilterFactory() { - return new CacheRequestBodyGatewayFilterFactory(); + public CacheRequestBodyGatewayFilterFactory cacheRequestBodyGatewayFilterFactory( + ServerCodecConfigurer codecConfigurer) { + return new CacheRequestBodyGatewayFilterFactory(codecConfigurer.getReaders()); } @Bean diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactory.java index 625f6ff9e..60ac5b53f 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactory.java @@ -51,6 +51,11 @@ public class CacheRequestBodyGatewayFilterFactory this.messageReaders = HandlerStrategies.withDefaults().messageReaders(); } + public CacheRequestBodyGatewayFilterFactory(List> messageReaders) { + super(CacheRequestBodyGatewayFilterFactory.Config.class); + this.messageReaders = messageReaders; + } + @Override public GatewayFilter apply(CacheRequestBodyGatewayFilterFactory.Config config) { return new GatewayFilter() { diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactoryTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactoryTests.java index bcabf1c29..7a9be4429 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactoryTests.java @@ -34,7 +34,9 @@ import org.springframework.cloud.gateway.test.BaseWebClientTests; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.core.io.buffer.PooledDataBuffer; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.test.annotation.DirtiesContext; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; @@ -43,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -@SpringBootTest(webEnvironment = RANDOM_PORT) +@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "spring.codec.max-in-memory-size=25") @DirtiesContext public class CacheRequestBodyGatewayFilterFactoryTests extends BaseWebClientTests { @@ -53,6 +55,8 @@ public class CacheRequestBodyGatewayFilterFactoryTests extends BaseWebClientTest private static final String BODY_CACHED_EXISTS = "BODY_CACHED_EXISTS"; + private static final String LARGE_BODY_VALUE = "here is request body which will cause payload size failure"; + @Test public void cacheRequestBodyWorks() { testClient.post().uri("/post").header("Host", "www.cacherequestbody.org").bodyValue(BODY_VALUE).exchange() @@ -65,6 +69,14 @@ public class CacheRequestBodyGatewayFilterFactoryTests extends BaseWebClientTest }); } + @Test + public void cacheRequestBodyDoesntWorkForLargePayload() { + testClient.post().uri("/post").header("Host", "www.cacherequestbody.org") + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).bodyValue(LARGE_BODY_VALUE) + .exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("message") + .isEqualTo("Exceeded limit on max bytes to buffer : 25"); + } + @Test public void cacheRequestBodyEmpty() { testClient.post().uri("/post").header("Host", "www.cacherequestbodyempty.org").exchange().expectStatus().isOk()