diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java index 76c66fb8b..4a43b16d1 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiter.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import javax.validation.constraints.Min; @@ -46,6 +47,7 @@ import org.springframework.validation.annotation.Validated; * https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L11-L34. * * @author Spencer Gibb + * @author Ronny Bräunlich */ @ConfigurationProperties("spring.cloud.gateway.redis-rate-limiter") public class RedisRateLimiter extends AbstractRateLimiter @@ -266,11 +268,15 @@ public class RedisRateLimiter extends AbstractRateLimiter getHeaders(Config config, Long tokensLeft) { - HashMap headers = new HashMap<>(); - headers.put(this.remainingHeader, tokensLeft.toString()); - headers.put(this.replenishRateHeader, String.valueOf(config.getReplenishRate())); - headers.put(this.burstCapacityHeader, String.valueOf(config.getBurstCapacity())); + public Map getHeaders(Config config, Long tokensLeft) { + Map headers = new HashMap<>(); + if (isIncludeHeaders()) { + headers.put(this.remainingHeader, tokensLeft.toString()); + headers.put(this.replenishRateHeader, + String.valueOf(config.getReplenishRate())); + headers.put(this.burstCapacityHeader, + String.valueOf(config.getBurstCapacity())); + } return headers; } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java index e56e5cfa3..c4183bdf7 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java @@ -44,6 +44,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L36-L62 * * @author Spencer Gibb + * @author Ronny Bräunlich */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @@ -110,6 +111,25 @@ public class RedisRateLimiterTests extends BaseWebClientTests { "request_rate_limiter.{1}.tokens", "request_rate_limiter.{1}.timestamp"); } + @Test + public void redisRateLimiterDoesNotSendHeadersIfDeactivated() throws Exception { + assumeThat("Ignore on Circle", System.getenv("CIRCLECI"), is(nullValue())); + + String id = UUID.randomUUID().toString(); + String routeId = "myroute"; + + rateLimiter.setIncludeHeaders(false); + + Response response = rateLimiter.isAllowed(routeId, id).block(); + assertThat(response.isAllowed()).isTrue(); + assertThat(response.getHeaders()) + .doesNotContainKey(RedisRateLimiter.REMAINING_HEADER); + assertThat(response.getHeaders()) + .doesNotContainKey(RedisRateLimiter.REPLENISH_RATE_HEADER); + assertThat(response.getHeaders()) + .doesNotContainKey(RedisRateLimiter.BURST_CAPACITY_HEADER); + } + @EnableAutoConfiguration @SpringBootConfiguration @Import(BaseWebClientTests.DefaultTestConfig.class)