Skip to content

feat: IP-based rate limiting using Bucket4j - #2

Open
aayushcodex17 wants to merge 1 commit into
developfrom
feature/rate-limiting
Open

feat: IP-based rate limiting using Bucket4j#2
aayushcodex17 wants to merge 1 commit into
developfrom
feature/rate-limiting

Conversation

@aayushcodex17

Copy link
Copy Markdown
Owner

What

Adds per-IP rate limiting to protect the API from abuse and brute force attacks.

Implementation

  • Library: bucket4j-core 8.10.1 — token bucket algorithm, in-memory, no Redis required
  • Filter: RateLimitingFilter extends OncePerRequestFilter, runs before JWTAuthFilter
  • Per-IP buckets: each IP gets its own Bucket stored in a ConcurrentHashMap

Rate Limits

Endpoint Type Limit Reason
/auth/login, /auth/signup, /auth/registerAsHotelManager 5 req/min Brute force & credential stuffing protection
All other API endpoints 60 req/min General abuse prevention
/webhook/** Unlimited Stripe webhooks must never be blocked

Response on Limit Exceeded

Returns HTTP 429 Too Many Requests in the existing ApiResponse format:

{
  "timestamp": "2026-04-15T10:30:00",
  "data": null,
  "error": {
    "httpStatus": "TOO_MANY_REQUESTS",
    "message": "Too many requests. Please slow down and try again in 42 second(s)."
  }
}

Headers

Header Description
X-RateLimit-Limit Max requests allowed in the window
X-RateLimit-Remaining Requests remaining in current window
X-RateLimit-Retry-After-Seconds Seconds to wait before retrying (on 429)

Proxy Support

Extracts real client IP from X-Forwarded-For header for deployments behind a reverse proxy or load balancer.

Files Changed

  • pom.xml — added bucket4j-core dependency
  • ratelimit/RateLimitingFilter.java — new filter
  • security/WebSecurityConfig.java — registered filter before JWTAuthFilter

- Added bucket4j-core 8.10.1 dependency
- Created RateLimitingFilter (OncePerRequestFilter) with per-IP token buckets
- Auth endpoints (/auth/login, /signup, /registerAsHotelManager): 5 req/min
- General API endpoints: 60 req/min per IP
- Webhooks (/webhook/**) bypass rate limiting for Stripe compatibility
- Returns 429 with existing ApiResponse/ApiError format on limit exceeded
- Adds X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Retry-After-Seconds headers
- Handles X-Forwarded-For header for requests behind reverse proxy
- Registered RateLimitingFilter before JWTAuthFilter in security chain

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant