多次限制对 public 服务的访问
Restrict access to public service many times
我有这样的情况。假设有一个 public REST 服务。我们不希望有人能够在短时间内多次访问此服务,因为他们将能够阻止我们的数据库(本质上是 DDOS 攻击,我想?)。
有没有办法有效防范此类攻击?我们使用的技术是 Spring/Spring 安全。
如果您正在使用 Spring Boot 有一个相当新的开源项目可以处理这个问题:
https://github.com/weddini/spring-boot-throttling
Declarative approach of throttling control over the Spring services.
@Throttling annotation helps you to limit the number of service method
calls per java.util.concurrent.TimeUnit for a particular user, IP
address, HTTP header/cookie value, or using Spring Expression Language
(SpEL).
显然这不会在 Web 服务器级别阻止 DDOS 攻击,但它有助于限制对长 运行 查询的访问或实施公平使用政策。
对于那些对该主题感兴趣的人,spring-boot-throttling 似乎不再维护。
所以,我看一下 bucket4j
使用很简单:主要有3个对象:
- Bucket :允许定义可用令牌总容量的接口。它还提供了使用令牌的方法。
- 带宽 : Class 允许定义桶的限制。
- Refill : Class 允许定义用新令牌给桶的方式。
带有简单 Spring 引导控制器的示例:
@RestController
public class TestLimit {
private Bucket bucket = null;
public MsGeneratorController() {
Bandwidth limit = Bandwidth.classic(120, Refill.greedy(120, Duration.ofMinutes(1)));
this.bucket = Bucket4j.builder().addLimit(limit).build();
}
@RequestMapping(path = "/test-limit/", method = RequestMethod.GET)
public ResponseEntity<String> download() throws IOException {
if (this.bucket.tryConsume(1)) {
return ResponseEntity.status(HttpStatus.OK).build();
}else {
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build();
}
}
}
在这种情况下,我们有每分钟 120 个请求的限制,桶容量为 120,每分钟重新填充 120 个令牌。
如果超过此限制,我们将收到 HTTP 429 代码 (TOO_MANY_REQUESTS)。
我有这样的情况。假设有一个 public REST 服务。我们不希望有人能够在短时间内多次访问此服务,因为他们将能够阻止我们的数据库(本质上是 DDOS 攻击,我想?)。
有没有办法有效防范此类攻击?我们使用的技术是 Spring/Spring 安全。
如果您正在使用 Spring Boot 有一个相当新的开源项目可以处理这个问题:
https://github.com/weddini/spring-boot-throttling
Declarative approach of throttling control over the Spring services. @Throttling annotation helps you to limit the number of service method calls per java.util.concurrent.TimeUnit for a particular user, IP address, HTTP header/cookie value, or using Spring Expression Language (SpEL).
显然这不会在 Web 服务器级别阻止 DDOS 攻击,但它有助于限制对长 运行 查询的访问或实施公平使用政策。
对于那些对该主题感兴趣的人,spring-boot-throttling 似乎不再维护。
所以,我看一下 bucket4j
使用很简单:主要有3个对象:
- Bucket :允许定义可用令牌总容量的接口。它还提供了使用令牌的方法。
- 带宽 : Class 允许定义桶的限制。
- Refill : Class 允许定义用新令牌给桶的方式。
带有简单 Spring 引导控制器的示例:
@RestController
public class TestLimit {
private Bucket bucket = null;
public MsGeneratorController() {
Bandwidth limit = Bandwidth.classic(120, Refill.greedy(120, Duration.ofMinutes(1)));
this.bucket = Bucket4j.builder().addLimit(limit).build();
}
@RequestMapping(path = "/test-limit/", method = RequestMethod.GET)
public ResponseEntity<String> download() throws IOException {
if (this.bucket.tryConsume(1)) {
return ResponseEntity.status(HttpStatus.OK).build();
}else {
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build();
}
}
}
在这种情况下,我们有每分钟 120 个请求的限制,桶容量为 120,每分钟重新填充 120 个令牌。
如果超过此限制,我们将收到 HTTP 429 代码 (TOO_MANY_REQUESTS)。