79159731

Date: 2024-11-05 15:32:05
Score: 0.5
Natty:
Report link

I have resolved the issue with rate limiting for POST requests in my Spring Cloud Gateway application. The problem was that rate limiting requires an identifier for the entity accessing the gateway. For authenticated requests, this identifier is provided automatically. However, for POST requests without authentication, the gateway lacks this identifier and consequently blocks the requests.

Implement Custom KeyResolver

@Bean
public KeyResolver userKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
}

Then specify the KeyResolver in your configuration:

spring:
  cloud:
    gateway:
      default-filters:
        - TokenRelay=
        - name: RequestRateLimiter
          args:
            key-resolver: "#{@userKeyResolver}"
            redis-rate-limiter.replenishRate: 1
            redis-rate-limiter.burstCapacity: 1
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: White space