- RateLimitFilter: fixed-window counters in Redis (Lua INCR+PEXPIRE) per client IP for login, registration, refresh, check-login, post creation, uploads, chat start and public ad endpoints; 429 with Retry-After; fails open when Redis is down. Rules live in rate-limit.* in application.yaml. - Login is blocked for an account after 10 failed attempts in 15 minutes, independent of the client IP. - Registration requires a password of at least 8 characters. - server.forward-headers-strategy=native so the client IP survives a reverse proxy; graceful shutdown. - /actuator/health with liveness/readiness probes is public (no details). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
1.8 KiB
Java
50 lines
1.8 KiB
Java
package com.krylov.refound.ratelimit;
|
||
|
||
import java.time.Duration;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import lombok.Getter;
|
||
import lombok.Setter;
|
||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||
|
||
/**
|
||
* Лимиты запросов ({@code rate-limit.*} в application.yaml). Счётчики лежат в
|
||
* Redis, поэтому лимит общий для всех экземпляров приложения.
|
||
*/
|
||
@Getter
|
||
@Setter
|
||
@ConfigurationProperties(prefix = "rate-limit")
|
||
public class RateLimitProperties {
|
||
|
||
/** Выключатель всех лимитов (например, для нагрузочных тестов). */
|
||
private boolean enabled = true;
|
||
|
||
/** Правила по маршрутам; ключ лимита — IP клиента. */
|
||
private List<Rule> rules = new ArrayList<>();
|
||
|
||
/** Защита конкретного аккаунта от перебора пароля с разных IP. */
|
||
private Login login = new Login();
|
||
|
||
@Getter
|
||
@Setter
|
||
public static class Rule {
|
||
/** Имя правила — часть ключа в Redis. */
|
||
private String name;
|
||
/** HTTP-метод; пусто — любой. */
|
||
private String method;
|
||
/** Шаблоны путей в стиле Spring MVC ({@code /api/v1/auth/login}, {@code /api/ads/**}). */
|
||
private List<String> paths = new ArrayList<>();
|
||
/** Сколько запросов разрешено за окно. */
|
||
private int limit;
|
||
private Duration window = Duration.ofMinutes(1);
|
||
}
|
||
|
||
@Getter
|
||
@Setter
|
||
public static class Login {
|
||
/** Сколько неудачных входов в один аккаунт допускается за окно. */
|
||
private int maxFailures = 10;
|
||
private Duration window = Duration.ofMinutes(15);
|
||
}
|
||
}
|