-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Labels
enhancementNew feature or requestNew feature or requestjavaPull requests that update Java codePull requests that update Java code
Description
Problem
The project currently lacks protection against API abuse and excessive request rates. This leads to:
- Vulnerability to brute-force attacks and DoS attempts
- Potential resource exhaustion from malicious or misconfigured clients
- Unfair resource allocation when multiple clients access the API
- No throttling mechanism to protect backend services and database
- Difficulty managing costs and capacity planning
- Poor quality of service for legitimate users during traffic spikes
Without rate limiting, the API is exposed to abuse scenarios that can degrade performance, increase infrastructure costs, and compromise system availability.
Proposed Solution
Integrate Bucket4j as the rate-limiting library for the Spring Boot RESTful API. This will:
- Provide robust, in-memory rate limiting based on the token bucket algorithm
- Protect endpoints from excessive requests and abuse patterns
- Ensure fair resource allocation across API consumers
- Return standard HTTP 429 (Too Many Requests) responses when limits are exceeded
- Improve overall system reliability and user experience
- Enable fine-grained control over request rates per endpoint or client
- Integrate seamlessly with Spring Boot's filter/interceptor architecture
Suggested Approach
-
Add Bucket4j dependency
- Update
pom.xml:
<dependency> <groupId>com.bucket4j</groupId> <artifactId>bucket4j-core</artifactId> <version>8.17.0</version> </dependency>
- Update
-
Create rate limit configuration class
- Create
src/main/java/.../config/RateLimitConfig.java: - Define rate limit rules (e.g., 10 requests per minute, 100 per hour)
- Configure bucket capacity and refill strategy
- Consider different tiers for different endpoint types
- Create
-
Implement rate limiting filter or interceptor
- Create
src/main/java/.../filters/RateLimitFilter.java: - Extend
OncePerRequestFilteror implementHandlerInterceptor - Extract client identifier (IP address, API key, or user ID)
- Maintain
ConcurrentHashMap<String, Bucket>for in-memory bucket storage - Check bucket consumption before processing request
- Return HTTP 429 with
Retry-Afterheader when limit exceeded
- Create
-
Apply rate limiting to controllers
- Protect public endpoints in
BooksController - Consider different limits for read vs. write operations
- Option A: Apply filter globally via
FilterRegistrationBean - Option B: Use custom annotation + AOP for selective rate limiting
- Protect public endpoints in
-
Configure rate limit headers
- Add standard rate limit response headers:
X-RateLimit-Limit: Maximum requests allowedX-RateLimit-Remaining: Remaining requests in current windowX-RateLimit-Reset: Time when the rate limit resetsRetry-After: Seconds until next request is allowed (on 429)
- Add standard rate limit response headers:
-
Add observability and logging
- Log rate limit events using SLF4J
- Include client identifier, endpoint, and timestamp
- Consider metrics integration for monitoring (future enhancement)
-
Externalize configuration
- Move rate limit values to
application.properties:
ratelimit.enabled=true ratelimit.requests-per-minute=10 ratelimit.requests-per-hour=100
- Move rate limit values to
-
Key files to modify/create
pom.xml- Add Bucket4j dependencysrc/main/java/.../config/RateLimitConfig.java- Rate limit configurationsrc/main/java/.../filters/RateLimitFilter.java- Rate limiting filtersrc/main/resources/application.properties- Rate limit settingssrc/test/java/.../filters/RateLimitFilterTests.java- Filter unit testssrc/test/java/.../controllers/BooksControllerTests.java- Update integration testsREADME.md- Document rate limiting behavior and configuration
-
Testing strategy
- Unit test the filter with mocked buckets
- Integration test controller endpoints with rapid requests
- Verify HTTP 429 status and headers
- Test bucket refill behavior over time
- Validate that legitimate traffic passes through
-
Documentation updates
- Update API documentation (OpenAPI/Swagger) with rate limit info
- Add rate limiting section to README.md
- Document configuration options and customization
- Include examples of rate limit responses
Acceptance Criteria
- Bucket4j dependency is added to the project
- Rate limiting filter/interceptor is implemented and registered
- At least one public endpoint (
GET /books) is protected with rate limiting - HTTP 429 (Too Many Requests) is returned when rate limit is exceeded
- Response includes standard rate limit headers (
X-RateLimit-*,Retry-After) - Client identification uses IP address from request
- Rate limit configuration is externalized to
application.properties - Buckets are stored in-memory using
ConcurrentHashMap - Rate limit events are logged with client identifier and timestamp
- Unit tests validate filter behavior with bucket consumption
- Integration tests verify rate limiting on protected endpoints
- Tests confirm bucket refill works correctly after cooldown period
- OpenAPI/Swagger documentation reflects rate limiting behavior
- README.md includes rate limiting configuration guide
- Application starts successfully with rate limiting enabled
- Rate limiting can be disabled via configuration flag
References
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestjavaPull requests that update Java codePull requests that update Java code