Spring Boot Starter for Bucket4j
Contents
Introduction
This project is a Spring Boot Starter for Bucket4j. It can be used limit the rate of access to your REST APIs.
-
Prevention of DoS Attacks, brute-force logins attempts
-
Request throttling for specific regions, unauthenticated users, authenticated users, not paying users.
The benefit of this project is the configuration of Bucket4j via Spring Boots properties or yaml files. You don’t have to write a single line of code. .
Getting started
To use the rate limit in your project you have to add the Bucket4j Spring Boot Starter dependency in your project. Additionally you need to add a JSR 107 provider like Ehcache or Hazelcast which will be auto configured with the Spring Boot Starter Cache.
<dependency>
<groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
<artifactId>bucket4j-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
Don’t forget to enable the caching feature by adding the @EnableCaching annotation to any of the configuration classes.
The configuration can be done in the application.properties / application.yml. The following configuration limits all requests independently from the user. It allows a maximum of 5 requests within 10 seconds independently from the user.
bucket4j:
enabled: true
filters:
- cache-name: buckets
url: .*
rate-limits:
- bandwidths:
- capacity: 5
time: 10
unit: seconds
For Ehcache 3 you also need a ehcache.xml which can be placed in the classpath. The configured cache name buckets must be defined in the configuration file.
spring:
cache:
jcache:
config: classpath:ehcache.xml
<config xmlns="...">
<cache alias="buckets">
<expiry>
<ttl unit="seconds">3600</ttl>
</expiry>
<heap unit="entries">1000000</heap>
</cache>
</config>
Bucket4j properties
bucket4j.enabled=true # enable/disable bucket4j support
bucket4j.filters[0].cache-name=buckets # the name of the cache key
bucket4j.filters[0].filter-method=servlet # [servlet,zuul,webflux,gateway]
bucket4j.filters[0].filter-order= # Per default the lowest integer plus 10. Set it to a number higher then zero to execute it after e.g. Spring Security.
bucket4j.filters[0].http-response-body={ "message": "Too many requests" } # the json response which should be added to the body
bucket4j.filters[0].url=.* # a regular expression
bucket4j.filters[0].metrics.enabled=true
bucket4j.filters[0].metrics.types=CONSUMED_COUNTER,REJECTED_COUNTER # (optional) if your not interested in the consumed counter you can specify only the rejected counter
bucket4j.filters[0].metrics.tags[0].key=IP
bucket4j.filters[0].metrics.tags[0].expression=getRemoteAddr()
bucket4j.filters[0].metrics.tags[0].types=REJECTED_COUNTER # (optionial) this tag should for example only be applied for the rejected counter
bucket4j.filters[0].metrics.tags[1].key=URL
bucket4j.filters[0].metrics.tags[1].expression=getRequestURI()
bucket4j.filters[0].metrics.tags[2].key=USERNAME
bucket4j.filters[0].metrics.tags[2].expression[email protected]() != null ? @securityService.username() : 'anonym'
bucket4j.filters[0].strategy=first # [first, all] if multiple rate limits configured the 'first' strategy stops the processing after the first matching
bucket4j.filters[0].rate-limits[0].expression=getRemoteAddress() # if filter-key-type is expression the key can be retrieved by an Spring Expression Language
bucket4j.filters[0].rate-limits[0].execute-condition=1==1 # an optional SpEl expression to decide to execute the rate limit or not
bucket4j.filters[0].rate-limits[0].skip-condition=1==1 # an optional SpEl expression to skip the rate limit
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=10
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=1
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=minutes
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval=0
bucket4j.filters[0].rate-limits[0].bandwidths[0].fixed-refill-interval-unit=minutes
Filter types (bad name, should be renamed in the feature)
Filter types are predefined configuration option on how to define the key which should be used to limiting the requests.
Default
The default options doesn’t differentiates between incoming requests (user, ip, etc). Its a general limiting.
IP
The IP filter type limits the access based on the IP address (httpServletRequest.getRemoteAddr()). So each IP address will independently throttled.
Expression
The expression based filter type provides the most flexible one and uses the Spring Expression Language (SpEL). The expression compiles to a Java class which will be used. It provides an easy way to configure the throttling in different environments without writing one line of code.
Depending on the filter method [servlet,zuul,webflux,gateway] different SpEL root objects object can be used in the expression so that you have a direct access to the method of these request objects:
-
servlet: javax.servlet.http.HttpServletRequest (e.g. getRemoteAddr() or getRequestURI())
-
zuul: javax.servlet.http.HttpServletRequest
-
webflux: org.springframework.http.server.reactive.ServerHttpRequest
-
gateway: org.springframework.http.server.reactive.ServerHttpRequest
Limiting based on IP-Address:
getRemoteAddress()
Limiting based on Username - If not logged in use IP-Address:
@securityService.username()?: getRemoteAddr()
/**
* You can define custom beans like the SecurityService which can be used in the SpEl expressions.
**/
@Service
public class SecurityService {
public String username() {
String name = SecurityContextHolder.getContext().getAuthentication().getName();
if(name == "anonymousUser") {
return null;
}
return name;
}
}
Filter strategy
The filter strategy defines how the execution of the rate limits will be performed.
bucket4j.filters[0].strategy=first # [first, all]
first
The first is the default strategy. This the default strategy which only executes one rate limit configuration.
all
The all strategy executes all rate limit independently.
Monitoring - Spring Boot 2 Actuator
Spring Boot 2 ships with a great support for collecting metrics. This project automatically provides metric information about the consumed and rejected buckets. You can extend these information with configurable custom tags like the username or the IP-Address which can then be evaluated in a monitoring system like prometheus/grafana.
bucket4j:
enabled: true
filters:
- cache-name: buckets
filter-method: servlet
filter-order: 1
url: .*
metrics:
tags:
- key: IP
expression: getRemoteAddr()
types: REJECTED_COUNTER # for data privacy reasons the IP should only be collected on bucket rejections
- key: USERNAME
expression: "@securityService.username() != null ? @securityService.username() : 'anonym'"
- key: URL
expression: request.getRequestURI()
rate-limits:
execute-condition: "@securityService.username() == 'admin'"
expression: "@securityService.username()?: getRemoteAddr()"
bandwidths:
- capacity: 30
time: 1
unit: minutes
Configuration via properties
Simple configuration to allow a maximum of 5 requests within 10 seconds independently from the user.
bucket4j:
enabled: true
filters:
- cache-name: buckets
url: .*
rate-limits:
bandwidths:
- capacity: 5
time: 10
unit: seconds
Conditional filtering depending of anonymous or logged in user. Because the bucket4j.filters[0].strategy is first you havn’t to check in the second rate-limit that the user is logged in. Only the first one is executed.
bucket4j:
enabled: true
filters:
- cache-name: buckets
filter-method: servlet
url: .*
rate-limits:
execute-condition: @securityService.notSignedIn() # only for not logged in users
expression: "getRemoteAddr()"
bandwidths:
- capacity: 10
time: 1
unit: minutes
execute-condition: "@securityService.username() != 'admin'" # strategy is only evaluate first. so the user must be logged in and user is not admin
expression: @securityService.username()
bandwidths:
- capacity: 1000
time: 1
unit: minutes
execute-condition: "@securityService.username() == 'admin'" # user is admin
expression: @securityService.username()
bandwidths:
- capacity: 1000000000
time: 1
unit: minutes
Configuration of multiple independently filters (servlet filter or zuul) with specific rate limit configurations.
bucket4j:
enabled: true
filters: # each config entry creates one servlet filter or zuul filter
- cache-name: buckets # create new servlet filter with bucket4j configuration
url: /admin*
rate-limits:
bandwidths: # maximum of 5 requests within 10 seconds
- capacity: 5
time: 10
unit: seconds
- cache-name: buckets
url: /public*
rate-limits:
- expression: getRemoteAddress() # IP based filter
bandwidths: # maximum of 5 requests within 10 seconds
- capacity: 5
time: 10
unit: seconds
- cache-name: buckets
url: /users*
rate-limits:
skip-condition: "@securityService.username() == 'admin'" # we don't check the rate limit if user is the admin user
expression: "@securityService.username()?: getRemoteAddr()" # use the username as key. if authenticated use the ip address
bandwidths:
- capacity: 100
time: 1
unit: seconds
- capacity: 10000
time: 1
unit: minutes