feat: 登录失败告警通知(阈值窗口统计 + Webhook JSON 推送)
- 新增 alert 包:AlertSettings(alerts 配置组,归一校验)、AlertSettingProvider (60s TTL 缓存,同审计设置模式)、LoginFailureAlertService(阈值窗口状态机: 达阈值告警一次并重开窗口防轰炸,登录成功立即清零,2xx 成功/其余失败)、 AlertWebhookSender(JSON POST fire-and-forget,10s 超时,失败不重试仅记日志) - 捕获过滤器挂钩:POST /login 请求结束经 CaptureRule.isLoginAttempt 回调告警服务, 独立于审计开关;登录页 GET 与登出不触发 - 设置新增「告警通知」表单组(开关默认关、Webhook 地址、触发阈值、统计窗口) - 测试 +32(共 188 全绿):设置归一与 TTL 缓存、阈值窗口推进/告警防轰炸/成功清零/ 3xx 失败归类/降级路径、JDK HttpServer 真实 HTTP 推送与非 2xx/非法 URL 静默降级、 过滤器告警回调与过滤器行为测试更新 - 文档同步:设计文档 §6 标注 V2 已实现并补 §7 局限,双 README 功能/设置/技术说明
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
package run.halo.operationaudit.alert;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.operationaudit.audit.RequestSnapshot;
|
||||
|
||||
/**
|
||||
* 登录失败告警(产品设计 V2+「告警通知」):统计窗口内登录失败达到阈值时向 Webhook
|
||||
* 推送一次 JSON 告警。
|
||||
*
|
||||
* <p>触发点在捕获过滤器(POST {@code /login} 请求结束,见 {@code CaptureRule#isLoginAttempt}):
|
||||
* 结果为 2xx 视为登录成功并立即清零计数,其余视为失败累计。告警后重开窗口(同一攻击
|
||||
* 波次不会连环轰炸,下次告警需再次达到阈值);窗口过期同样重开,无失败时不累计。
|
||||
*
|
||||
* <p>登录失败暂无法得知尝试的用户名(产品设计已知局限,与登录记录一致),告警负载
|
||||
* 仅含失败次数、窗口、最近一次来源 IP 与 User-Agent。
|
||||
*
|
||||
* <p>告警开关与阈值经 {@link AlertSettingProvider} 读取(60s TTL 缓存),与审计开关
|
||||
* 相互独立。任何失败仅记 warn 日志,绝不影响登录主流程(产品设计原则 4)。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class LoginFailureAlertService {
|
||||
|
||||
private final AlertSettingProvider settingProvider;
|
||||
private final AlertWebhookSender sender;
|
||||
|
||||
private final Object stateLock = new Object();
|
||||
|
||||
/** 阈值窗口状态(窗口起点与累计失败数);包内可见以便测试直接构造状态。 */
|
||||
Instant windowStart;
|
||||
int failureCount;
|
||||
|
||||
/**
|
||||
* 登录结果回调:仅处理 POST /login 请求(成功 2xx 清零计数,失败累计告警)。
|
||||
* 任何失败静默降级,不影响调用方。
|
||||
*/
|
||||
public void onLoginResult(RequestSnapshot snapshot, Integer statusCode) {
|
||||
var success = statusCode != null && statusCode >= 200 && statusCode < 300;
|
||||
settingProvider.get().subscribe(
|
||||
settings -> handle(settings, snapshot, success, Instant.now()),
|
||||
e -> log.warn("Failed to load alert settings, skip login alert for {}",
|
||||
snapshot.getFullPath(), e));
|
||||
}
|
||||
|
||||
/**
|
||||
* 阈值窗口判定与告警决策。now 由调用方传入(测试可控);发送在锁外执行。
|
||||
* handle 包内可见以便测试直接驱动窗口推进。
|
||||
*/
|
||||
void handle(AlertSettings settings, RequestSnapshot snapshot, boolean success, Instant now) {
|
||||
try {
|
||||
if (!settings.enabledOrDefault()) {
|
||||
return;
|
||||
}
|
||||
var url = settings.webhookUrlOrDefault();
|
||||
if (url == null) {
|
||||
return;
|
||||
}
|
||||
var window = Duration.ofMinutes(settings.windowMinutesOrDefault());
|
||||
int failures;
|
||||
synchronized (stateLock) {
|
||||
if (success) {
|
||||
// 登录成功立即清零:零星输错密码不应累积
|
||||
windowStart = null;
|
||||
failureCount = 0;
|
||||
return;
|
||||
}
|
||||
if (windowStart == null || now.isAfter(windowStart.plus(window))) {
|
||||
windowStart = now;
|
||||
failureCount = 0;
|
||||
}
|
||||
failureCount++;
|
||||
if (failureCount < settings.thresholdCountOrDefault()) {
|
||||
return;
|
||||
}
|
||||
failures = failureCount;
|
||||
// 达到阈值:触发告警并重开窗口,下次告警需再次达到阈值(防轰炸)
|
||||
windowStart = now;
|
||||
failureCount = 0;
|
||||
}
|
||||
sender.send(url, buildPayload(failures, settings.windowMinutesOrDefault(),
|
||||
snapshot.getRemoteIp(), snapshot.getUserAgent(), now));
|
||||
} catch (Exception e) {
|
||||
// 告警为旁路功能,任何异常静默降级,绝不影响登录主流程
|
||||
log.warn("Failed to handle login failure alert for {}",
|
||||
snapshot.getFullPath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装通用 JSON 告警负载:type/title/content 供消息类接收方直接展示,
|
||||
* data 供程序化消费方取结构化字段。
|
||||
*/
|
||||
static Map<String, Object> buildPayload(int failures, int windowMinutes, String remoteIp,
|
||||
String userAgent, Instant timestamp) {
|
||||
var content = windowMinutes + " 分钟内出现 " + failures + " 次登录失败,最近一次来自 "
|
||||
+ remoteIp + (userAgent != null && !userAgent.isBlank()
|
||||
? "(" + userAgent + ")" : "")
|
||||
+ "。可在 Console「操作审计」查看详情。";
|
||||
var data = new LinkedHashMap<String, Object>();
|
||||
data.put("failures", failures);
|
||||
data.put("windowMinutes", windowMinutes);
|
||||
data.put("remoteIp", remoteIp);
|
||||
data.put("userAgent", userAgent);
|
||||
var payload = new LinkedHashMap<String, Object>();
|
||||
payload.put("type", "login-failure");
|
||||
payload.put("title", "操作审计告警:登录失败");
|
||||
payload.put("content", content);
|
||||
payload.put("timestamp", timestamp.toString());
|
||||
payload.put("data", data);
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user