ttulka / bucket4j-spring-boot-starter

Spring Boot Starter for Bucket4j

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring Boot Starter for Bucket4j

Project version overview:

  • 0.5.0 - Bucket4j 7.0.0 - Spring Boot 2.6.1

  • 0.4.1 - Bucket4j 7.0.0 - Spring Boot 2.4.1

  • 0.4.0 - Bucket4j 6.2.0 - Spring Boot 2.4.1

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,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].http-response-headers.<MY_CUSTOM_HEADER>=MY_CUSTOM_HEADER_VALUE # You can add any numbers of custom headers
bucket4j.filters[0].hide-http-response-headers=true # Hides response headers like x-rate-limit-remaining or x-rate-limit-retry-after-seconds on rate limiting
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=@securityService.username() != 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=getRemoteAddr() # 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

# Optional default metric tags for all filters
bucket4j.default-metric-tags[0].key=IP
bucket4j.default-metric-tags[0].expression=getRemoteAddr()
bucket4j.default-metric-tags[0].types=REJECTED_COUNTER

# Hide HTTP response headers

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,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())

  • 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|gateway|webflux filters) with specific rate limit configurations.

bucket4j:
  enabled: true
  filters: # each config entry creates one servlet filter or other 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

Programmatically define Cache Provider

Instead of determine the Caching Provider by the Bucket4j Spring Boot Starter project you can implement the SynchCacheResolver or the AsynchCacheResolver for reactive support by yourself.

Click here is an Example for a SynchSyncCacheResolver for Hazelcast Support without using JCache.

About

Spring Boot Starter for Bucket4j

License:Apache License 2.0


Languages

Language:Java 100.0%