All Projects → MarcGiffing → Bucket4j Spring Boot Starter

MarcGiffing / Bucket4j Spring Boot Starter

Licence: apache-2.0
Spring Boot Starter for Bucket4j

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Bucket4j Spring Boot Starter

Securing Rest Api Spring Security
Spring Boot 2.2.x + Spring 5.2.x Rest Api Security Example
Stars: ✭ 117 (-7.87%)
Mutual labels:  spring-boot, spring, spring-mvc
Java Spring Web
OpenTracing Spring Web instrumentation
Stars: ✭ 89 (-29.92%)
Mutual labels:  spring-boot, spring, spring-mvc
Curso Sistemas Web Com Spring Javascript Bootstrap
Stars: ✭ 74 (-41.73%)
Mutual labels:  spring-boot, spring, spring-mvc
Spring Boot Blog
Simple blog web app made using Spring Boot + Thymeleaf
Stars: ✭ 121 (-4.72%)
Mutual labels:  spring-boot, spring, spring-mvc
Ibase4j
Spring,SpringBoot 2.0,SpringMVC,Mybatis,mybatis-plus,motan/dubbo分布式,Redis缓存,Shiro权限管理,Spring-Session单点登录,Quartz分布式集群调度,Restful服务,QQ/微信登录,App token登录,微信/支付宝支付;日期转换、数据类型转换、序列化、汉字转拼音、身份证号码验证、数字转人民币、发送短信、发送邮件、加密解密、图片处理、excel导入导出、FTP/SFTP/fastDFS上传下载、二维码、XML读写、高精度计算、系统配置工具类等等。
Stars: ✭ 1,548 (+1118.9%)
Mutual labels:  spring-boot, spring, spring-mvc
Spring Higher Order Components
⚡️ Preconfigured components to speedup Spring Boot development
Stars: ✭ 65 (-48.82%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Spring Boot Shopping Cart
Simple shopping cart web app made using Spring Boot + Thymeleaf
Stars: ✭ 85 (-33.07%)
Mutual labels:  spring-boot, spring, spring-mvc
Spring Thrift Api Gateway
Gateway for Apache Thrift requests processing that is built on Spring Cloud stack
Stars: ✭ 38 (-70.08%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Wicket Spring Boot
Spring Boot starter for Apache Wicket
Stars: ✭ 117 (-7.87%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Whatsmars
Java生态研究(Spring Boot + Redis + Dubbo + RocketMQ + Elasticsearch)🔥🔥🔥🔥🔥
Stars: ✭ 1,389 (+993.7%)
Mutual labels:  spring-boot, spring, spring-mvc
Springboard
Spring Boot based production grade starter kit.
Stars: ✭ 59 (-53.54%)
Mutual labels:  spring-boot, spring, spring-mvc
Nb Springboot
NetBeans IDE plugin supporting programming with Spring Boot
Stars: ✭ 111 (-12.6%)
Mutual labels:  spring-boot, spring, spring-mvc
Disconf Spring Boot Starter
disconf-spring-boot-starter 让你可以使用spring-boot的方式开发依赖disconf的程序 只需要关心disconfi的配置文件和配置项,省略了编写xml的麻烦
Stars: ✭ 44 (-65.35%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Eshop Soa
EShop基于Dubbo实现SOA服务化拆分,并基于RocketMQ解决了分布式事务(新版SpringBootSOASkeleton)
Stars: ✭ 65 (-48.82%)
Mutual labels:  spring-boot, spring, spring-mvc
Botwall4j
A botwall for Java web applications
Stars: ✭ 41 (-67.72%)
Mutual labels:  spring-boot, spring, spring-mvc
Telegram Spring Boot Starter
Telegram Bot API Spring Boot Starter
Stars: ✭ 79 (-37.8%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Versioning Spring Boot Starter
Spring boot starter using for versioning rest easily.
Stars: ✭ 11 (-91.34%)
Mutual labels:  spring-boot, spring-mvc, spring-boot-starter
Spring Mvc Thymeleaf Crud
Spring MVC CRUD Application with Thymeleaf, HTML5, CSS3 and Bootstrap
Stars: ✭ 14 (-88.98%)
Mutual labels:  spring-boot, spring, spring-mvc
Reactive Spring Security 5 Workshop
Hands-On workshop for securing a reactive spring boot 2 application in multiple steps
Stars: ✭ 92 (-27.56%)
Mutual labels:  spring-boot, spring, spring-mvc
Spring Cloud Microservices Development
Spring Cloud Microservices Development.《Spring Cloud 微服务架构开发实战》
Stars: ✭ 106 (-16.54%)
Mutual labels:  spring-boot, spring, spring-mvc

= Spring Boot Starter for Bucket4j

https://github.com/vladimir-bukhtoyarov/bucket4j

Examples: https://github.com/MarcGiffing/bucket4j-spring-boot-starter-examples[bucket4j-spring-boot-starter-examples]

= Contents

  • <>
  • <<getting_started>>
  • <<bucket4j_complete_properties>>
  • <>
  • <<configuration_examples>>

[[introduction]] == Introduction

This project is a http://projects.spring.io/spring-boot/[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]] == 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 https://www.jcp.org/en/jsr/detail?id=107[JSR 107] provider like Ehcache or Hazelcast which will be auto configured with the https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html[Spring Boot Starter Cache].

[source, xml]

com.giffing.bucket4j.spring.boot.starter bucket4j-spring-boot-starter org.springframework.boot spring-boot-starter-cache org.ehcache ehcache ----

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.

[source,yml]

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.

[source,yml]

spring: cache: jcache: config: classpath:ehcache.xml

[source,xml]

3600 1000000 ----

[[bucket4j_complete_properties]] == Bucket4j properties

[source, 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][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 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html[Spring Expression Language] (SpEL). https://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-spel-compilation[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: [source]

getRemoteAddress()

Limiting based on Username - If not logged in use IP-Address: [source]

@securityService.username()?: getRemoteAddr()

[source,java]

/**

  • 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.

[source, properties]

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]] == 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 https://micrometer.io/docs/concepts#_tag_naming[custom tags] like the username or the IP-Address which can then be evaluated in a monitoring system like prometheus/grafana.

[source,yml]

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_examples]] == Configuration via properties

Simple configuration to allow a maximum of 5 requests within 10 seconds independently from the user.

[source,yml]

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.

[source,yml]

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.

[source,yml]

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].