fix: 登录页访问不再记成「登录成功」——认证端点只记录 POST,登录成败以响应确认为准

现场反馈:审计列表「没有失败,全是成功,登录失败也显示成功」。在 Halo 2.26.1 上
复现(afda8d0 构建):GET /login、GET /login?authentication_required 只是打开登录页
(HTTP 200),却被记成「未知用户 · 登录 200」,列表里看起来全是登录成功;POST /login
本身的成败判定是对的,但被这些记录淹没。

- 捕获:AuthEndpointAuditWebFilter 只记录 POST /login(登录尝试)与 POST /logout
  (登出);登录/登出页面的 GET 访问(打开登录页、被弹回登录页、失败回显、登出
  确认页)原样透传不记录(CaptureRule.isAuthOperation)
- 判定:LoginOutcome 只有响应明确确认才算成功——302 跳回登录页(含 error 参数或
  路径即 /login)、缺失 Location、4xx/5xx 一律判失败;移除失败回显页判定
- 结果语义:新增 AuditResult(成功 / 失败 / 页面访问),结果筛选、CSV 导出与前端
  resultBadge 共用同一规则;登录成功显示「成功」,与「失败」对称
- 历史数据:升级前落库的登录页访问记录(GET,标注 login/logout)展示与导出为
  「访问登录页/登出页」且不计成败,结果筛选与「登录/登出」语义动作筛选均排除;
  落库标注不改写
- demo 同步同一套规则;README 与设计/技术文档同步

测试:后端 286、UI 74、demo 61 全过;真实 Halo 2.26.1 升级场景联调记录见测试报告
This commit is contained in:
2026-12-05 13:24:26 +08:00
parent afda8d04fd
commit fc7984291f
35 changed files with 1307 additions and 437 deletions
@@ -103,11 +103,6 @@ public class AuditLogCsvExporter {
private String toCsvLine(AuditLog log) {
var spec = log.getSpec();
var statusCode = spec.getStatusCode();
// 结果语义与前端一致:登录失败标注(HTTP 状态码可能为 200/302——失败回显页
// 渲染/失败重定向)一律失败,其余 2xx/3xx 成功
var failedLogin = OperationDescription.LOGIN_FAILURE_ACTION.equals(spec.getAction());
var success = !failedLogin && statusCode != null && statusCode >= 200
&& statusCode < 400;
return String.join(",",
escape(formatTime(log.getMetadata().getCreationTimestamp())),
escape(spec.getUsername()),
@@ -116,10 +111,13 @@ public class AuditLogCsvExporter {
escape(spec.getResourceType()),
escape(spec.getResourceName()),
escape(spec.getPath()),
// 优先落库语义动作(含登录失败标注),历史记录回退 method/path 现场推导
// 优先落库语义动作(含登录失败标注与页面访问纠正),历史记录回退
// method/path 现场推导
escape(OperationDescription.describe(spec)),
escape(ChangeDiffEngine.formatChanges(spec.getChanges())),
escape(success ? "成功" : "失败"),
// 结果语义与前端一致(AuditResult):登录失败标注一律失败(HTTP 状态码
// 可能是 302/200),认证端点的页面访问记录(历史 GET)不计成败,其余按状态码
escape(AuditResult.of(spec).label()),
escape(statusCode == null ? null : String.valueOf(statusCode)),
escape(spec.getRemoteIp()),
escape(spec.getUserAgent()));
@@ -25,6 +25,22 @@ final class AuditLogFilters {
static final Set<String> FILTERABLE_FIELDS = Set.of("spec.username", "spec.resourceType",
"spec.method", "spec.action", "spec.statusCode");
/**
* 登录/登出操作的语义动作键:按其筛选时排除页面访问记录——升级前落库的
* GET /login、GET /logout 带同样的标注,但展示为「访问登录页/登出页」。
*/
private static final Set<String> AUTH_OPERATION_ACTIONS = Set.of("login", "logout");
/**
* 页面访问记录({@link OperationDescription#isAuthPageVisit} 的索引查询形态):
* 非 POST 的 {@code /login}、{@code /logout} 请求(含查询串)。不是操作,不计成败。
*/
private static final Condition PAGE_VISIT = Queries.and(
Queries.notEqual("spec.method", "POST"),
Queries.or(
Queries.startsWith("spec.path", "/login"),
Queries.startsWith("spec.path", "/logout")));
private AuditLogFilters() {
}
@@ -44,7 +60,11 @@ final class AuditLogFilters {
if (!FILTERABLE_FIELDS.contains(field)) {
throw new IllegalArgumentException("Field not filterable: " + field);
}
conditions.add(Queries.equal(field, value));
var condition = Queries.equal(field, value);
if ("spec.action".equals(field) && AUTH_OPERATION_ACTIONS.contains(value)) {
condition = Queries.and(condition, Queries.not(PAGE_VISIT));
}
conditions.add(condition);
}
// 关键字:资源名称 / 完整路径 模糊匹配(OR)
@@ -56,9 +76,9 @@ final class AuditLogFilters {
Queries.contains("spec.path", kw)));
}
// 结果筛选:success = 2xx/3xx 且非登录失败标注,failure = 4xx/5xx 或登录失败
// (登录失败的 HTTP 状态码可能为 200/302——失败回显页渲染/失败重定向,
// 已由语义动作 login-failure 标注,见 LoginOutcome)
// 结果筛选(与 AuditResult 同一规则):success = 2xx/3xx 且非登录失败标注、
// 非页面访问;failure = 登录失败标注(表单登录失败的 HTTP 状态码是 302,
// 仅凭状态码识别不出)或 4xx/5xx(页面访问除外)。页面访问记录不计成败
var resultFilter = params.getFirst("result");
if (StringUtils.hasText(resultFilter)) {
var loginFailure = Queries.equal("spec.action",
@@ -68,10 +88,12 @@ final class AuditLogFilters {
conditions.add(Queries.greaterThan("spec.statusCode", "200", true));
conditions.add(Queries.lessThan("spec.statusCode", "400", true));
conditions.add(Queries.not(loginFailure));
conditions.add(Queries.not(PAGE_VISIT));
}
case "failure" -> conditions.add(Queries.or(
Queries.greaterThan("spec.statusCode", "400", true),
loginFailure));
loginFailure,
Queries.and(Queries.greaterThan("spec.statusCode", "400", true),
Queries.not(PAGE_VISIT))));
default -> throw new IllegalArgumentException(
"Invalid result, expect success or failure: " + resultFilter);
}
@@ -27,8 +27,10 @@ public class AuditLogSpec {
/**
* 语义动作键(语义动作标注,产品设计 V2):捕获时由 method/path 推导落库
* (如 login/logout/create/update/delete/publish/restore),注册索引后支持
* 按语义动作筛选。升级前写入的历史记录无此字段,展示不受影响(现场推导)。
* (如 login/logout/create/update/delete/publish/login-failure,可空),注册索引后
* 支持按语义动作筛选。升级前写入的历史记录无此字段,展示不受影响(现场推导);
* 升级前落库的登录页访问记录(GET,现已不再记录)带 login/logout 标注,
* 展示时按方法纠正为页面访问。
*/
private String action;
@@ -41,7 +43,10 @@ public class AuditLogSpec {
/** 完整请求路径(资源语义解析的兜底,保证完整性)。 */
private String path;
/** 响应状态码(结果:2xx 成功,其余失败)。 */
/**
* 响应状态码,如实记录。结果语义见 {@link AuditResult}:不能只看状态码——表单登录
* 成败均为 302,登录失败靠语义动作 {@code login-failure} 标注识别。
*/
private Integer statusCode;
/** 来源 IP。 */
@@ -39,9 +39,8 @@ public class AuditRecorder {
* @param attemptedUsername 登录尝试的用户名(功能评审 P0-1,仅 POST /login 请求
* 由 {@link LoginAttemptCapture} 提取,其余请求为 null)
* @param loginFailed 登录失败标注:POST /login 结果失败(表单登录失败为
* 302 重定向且目标含 error 参数)或失败回显
* (GET /login?error=...,HTTP 200),语义动作标注
* login-failure;其余请求为 false
* 302 重定向且回跳登录页),语义动作标注 login-failure;
* 其余请求为 false
*/
public void record(RequestSnapshot snapshot, ChangeCapture capture, Integer statusCode,
String username, String attemptedUsername, boolean loginFailed) {
@@ -162,14 +161,14 @@ public class AuditRecorder {
spec.setMethod(snapshot.getMethod());
spec.setResourceType(resolved.resourceType());
// 语义动作标注(产品设计 V2):捕获时推导落库,供按语义动作筛选;
// 登录失败(POST /login 失败或 GET /login?error=... 失败回显)标注
// login-failure——回显页 HTTP 200 无法由路径重新推导,必须落库标注
// 登录失败(POST /login 回跳登录页或 4xx/5xx)标注 login-failure——
// 表单登录失败的 HTTP 状态码与成功相同(302),结果只能靠落库标注区分
spec.setAction(loginFailed ? OperationDescription.LOGIN_FAILURE_ACTION
: OperationDescription.actionOf(
snapshot.getMethod(), resolved.resourceType(), snapshot.getPath()));
spec.setResourceName(resolved.optionalName().orElse(null));
spec.setPath(snapshot.getFullPath());
// 状态码如实记录(失败回显页为 200、表单登录失败为 302),登录成败看语义动作标注
// 状态码如实记录(表单登录成败均为 302),登录成败看语义动作标注
spec.setStatusCode(statusCode);
spec.setRemoteIp(snapshot.getRemoteIp());
spec.setUserAgent(snapshot.getUserAgent());
@@ -0,0 +1,44 @@
package run.halo.operationaudit.audit;
/**
* 审计记录的结果语义(纯函数):结果筛选({@link AuditLogFilters})、CSV 导出
* ({@link AuditLogCsvExporter})与前端 {@code ui/src/utils/audit.ts} 的结果徽标
* 共用同一规则——什么算成功、什么算失败,只能有一个定义。
*
* <ul>
* <li>{@link #FAILURE}:登录失败(语义动作 {@code login-failure},HTTP 状态码可能是
* 302/200);其余按状态码判定,4xx/5xx 为失败;</li>
* <li>{@link #PAGE_VISIT}:认证端点的页面访问(升级前落库的 GET /login、GET /logout
* 历史记录)——不是操作,不计成败;</li>
* <li>{@link #SUCCESS}:其余状态码 2xx/3xx。</li>
* </ul>
*/
enum AuditResult {
SUCCESS("成功"),
FAILURE("失败"),
PAGE_VISIT("页面访问");
/** 导出 CSV「结果」列的中文值。 */
private final String label;
AuditResult(String label) {
this.label = label;
}
static AuditResult of(AuditLogSpec spec) {
if (OperationDescription.LOGIN_FAILURE_ACTION.equals(
OperationDescription.semanticAction(spec))) {
return FAILURE;
}
if (OperationDescription.isAuthPageVisit(spec.getMethod(), spec.getPath())) {
return PAGE_VISIT;
}
var statusCode = spec.getStatusCode();
return statusCode != null && statusCode >= 200 && statusCode < 400
? SUCCESS : FAILURE;
}
String label() {
return label;
}
}
@@ -1,6 +1,6 @@
package run.halo.operationaudit.audit;
import static run.halo.operationaudit.audit.CaptureRule.isAuthEndpoint;
import static run.halo.operationaudit.audit.CaptureRule.isAuthOperation;
import static run.halo.operationaudit.audit.CaptureRule.isLoginAttempt;
import lombok.RequiredArgsConstructor;
@@ -13,16 +13,21 @@ import run.halo.app.security.BeforeSecurityWebFilter;
import run.halo.operationaudit.alert.LoginFailureAlertService;
/**
* 认证端点({@code /login}、{@code /logout})审计捕获过滤器(P0 修复,见测试报告)。
* 认证操作({@code POST /login}、{@code POST /logout})审计捕获过滤器(P0 修复,见测试报告)。
*
* <p>以 {@link BeforeSecurityWebFilter} 扩展点注册:位于 Security 过滤链最前
* ({@code SecurityWebFiltersOrder.FIRST})。原先认证端点由
* {@code AfterSecurityWebFilter} 扩展点注册的 {@link OperationAuditWebFilter} 捕获,
* 但该扩展点位于认证环节之后——表单登录(FORM_LOGIN)与登出(LOGOUT)过滤器命中
* 请求后直接完成 302 响应、不再向下游传递,POST /login 与 POST /logout 永远到不了
* 链尾的审计过滤器(真实环境实测,登录失败告警生产路径随之失效)。因此认证端点整体
* 链尾的审计过滤器(真实环境实测,登录失败告警生产路径随之失效)。因此认证操作整体
* 移交本过滤器,在 Security 链之前承接捕获与记录。
*
* <p>只记录认证操作本身:{@code POST /login} 是登录尝试,{@code POST /logout} 是登出。
* 登录/登出页面的 GET 访问(打开登录页、匿名访问被弹回登录页、登录失败后的回显、
* 登出确认页)原样透传、不记录——它们只是渲染页面(HTTP 200),落库会显示成
* 「未知用户 · 登录 · 200」,看起来像登录成功,真正的成败反而被淹没(现场实测反馈)。
*
* <p>操作者归属:Security 链最前无认证上下文(SecurityContext 由链内更晚位置的
* ReactorContextWebFilter 写入订阅上下文,对链首过滤器不可见),统一记
* {@link AuditLogSpec#UNKNOWN_USERNAME 未知用户}(既有兜底路径);登录尝试的账号
@@ -30,13 +35,12 @@ import run.halo.operationaudit.alert.LoginFailureAlertService;
* 并进入告警负载(功能评审 P0-1),账号归属信息不受影响。
*
* <p>登录失败告警(产品设计 V2「告警通知」):POST {@code /login} 请求结束时将结果
* 回调给 {@code LoginFailureAlertService},按阈值窗口推送 Webhook;与审计开关相互独立。
* 结果由 {@link LoginOutcome} 判定(表单登录成败均为 302 重定向,以重定向目标是否含
* {@code error} 参数区分,不能只看状态码)。
* 回调给 {@link LoginFailureAlertService},按阈值窗口推送 Webhook;与审计开关相互独立。
* 结果由 {@link LoginOutcome} 判定(表单登录成败均为 302 重定向,以重定向目标是否
* 跳回登录页区分,不能只看状态码)。
*
* <p>登录失败标注(语义动作 {@code login-failure}):登录尝试失败与失败回显
* ({@code GET /login?error=...},HTTP 200)的审计记录标注登录失败,供列表红色标记
* 与筛选;HTTP 状态码仍如实记录。
* <p>登录失败标注(语义动作 {@code login-failure}):失败的 POST {@code /login} 落库
* 标注登录失败,供列表红色标记与筛选;HTTP 状态码仍如实记录。
*
* <p>任何失败均静默降级(记录器与告警服务内部兜底),绝不影响登录主流程(设计原则 4)。
*/
@@ -54,16 +58,17 @@ public class AuthEndpointAuditWebFilter implements BeforeSecurityWebFilter {
var request = exchange.getRequest();
var path = request.getPath().value();
var method = request.getMethod().name();
if (!isAuthEndpoint(path)) {
// 非 /login、/logout 一律原样透传:不触碰请求体、不读会话,零附加开销
if (!isAuthOperation(path, method)) {
// 登录/登出页面的 GET 访问等非认证操作一律原样透传:不触碰请求体、不记录
return chain.filter(exchange);
}
writeMarker.onRequestStart(request);
var snapshot = RequestSnapshot.of(request);
var loginAttempt = new LoginAttempt();
var loginAttempted = isLoginAttempt(path, method);
// 登录请求先缓存请求体并提取尝试的用户名(P0-1),下游登录处理可重复读;
// 登出与登录页 GET 原样透传
var bodyPrepared = isLoginAttempt(path, method)
// 登出原样透传
var bodyPrepared = loginAttempted
? loginAttemptCapture.prepare(exchange, loginAttempt)
: Mono.just(exchange);
return bodyPrepared
@@ -72,9 +77,10 @@ public class AuthEndpointAuditWebFilter implements BeforeSecurityWebFilter {
writeMarker.onRequestComplete(request);
var statusCode = statusOf(exchange);
// 登录结果判定(LoginOutcome):表单登录成败均为 302 重定向,
// 以重定向目标是否含 error 参数区分;GET /login?error=... 为失败回显
var loginFailed = isLoginFailed(path, method, statusCode, exchange, snapshot);
if (isLoginAttempt(path, method)) {
// 以重定向目标是否跳回登录页区分;登出不存在登录成败
var loginFailed = loginAttempted && LoginOutcome.isFailure(statusCode,
exchange.getResponse().getHeaders().getLocation());
if (loginAttempted) {
// 登录失败告警(产品设计 V2):POST /login 结束即回调结果,
// 成功清零计数、失败累计,独立于审计记录
loginFailureAlertService.onLoginResult(snapshot, !loginFailed,
@@ -90,21 +96,4 @@ public class AuthEndpointAuditWebFilter implements BeforeSecurityWebFilter {
var status = exchange.getResponse().getStatusCode();
return status != null ? status.value() : 0;
}
/**
* 登录失败判定(LoginOutcome):POST {@code /login} 按状态码 + 重定向目标判定
* (表单登录成败均为 302,以 Location 是否含 {@code error} 参数区分);
* 登出与登录页 GET 仅失败回显({@code GET /login?error=...},HTTP 200)判为失败。
*/
private static boolean isLoginFailed(String path, String method, Integer statusCode,
ServerWebExchange exchange, RequestSnapshot snapshot) {
if (isLoginAttempt(path, method)) {
return LoginOutcome.isFailure(statusCode,
exchange.getResponse().getHeaders().getLocation());
}
if ("GET".equals(method)) {
return LoginOutcome.isFailureEcho(snapshot.getFullPath());
}
return false;
}
}
@@ -9,13 +9,18 @@ import java.util.Set;
* <li>{@code /apis/**} 下的 {@code POST/PUT/DELETE/PATCH} 写操作({@link #shouldCapture},
* 由 {@code AfterSecurityWebFilter} 注册的 {@link OperationAuditWebFilter} 消费,
* 可获得认证用户上下文);</li>
* <li>认证端点 {@code /login}、{@code /logout} 精确匹配({@link #isAuthEndpoint},
* 由 {@link AuthEndpointAuditWebFilter} 消费)——登录端点不在 /apis/** 下,且
* 表单登录/登出过滤器命中请求后直接完成响应、不再向下游传递,链尾的
* AfterSecurityWebFilter 永远看不到(P0 缺陷实测),因此必须在 Security 链之前
* 独立捕获。</li>
* <li>认证操作 {@code POST /login}(登录尝试)与 {@code POST /logout}(登出)精确匹配
* ({@link #isAuthOperation},由 {@link AuthEndpointAuditWebFilter} 消费)——
* 登录/登出端点不在 /apis/** 下,且表单登录/登出过滤器命中请求后直接完成响应、
* 不再向下游传递,链尾的 AfterSecurityWebFilter 永远看不到(P0 缺陷实测),
* 因此必须在 Security 链之前独立捕获。</li>
* </ul>
*
* <p>登录/登出页面的 GET 访问不是操作,不记录({@link #isAuthEndpoint} 仅用于路径归类):
* 打开登录页、匿名访问被弹回登录页({@code /login?authentication_required})、登录失败
* 后的回显({@code /login?error=...})、登出确认页都只是渲染页面(HTTP 200),若落库
* 会显示成「未知用户 · 登录 · 200」,看起来像登录成功,而真正的登录尝试是 POST /login。
*
* <p>匿名判断不在请求期做,而在记录时做(技术方案 D1)。
*/
public final class CaptureRule {
@@ -24,16 +29,19 @@ public final class CaptureRule {
public static final Set<String> AUTH_ENDPOINTS = Set.of("/login", "/logout");
public static final String APIS_PREFIX = "/apis/";
/** 登录端点(告警通道唯一触发路径;登出与登录页 GET 不算登录尝试)。 */
/** 登录端点(告警通道唯一触发路径;登录页 GET 不算登录尝试)。 */
public static final String LOGIN_ENDPOINT = "/login";
/** 登出端点(POST 为登出操作;GET 为登出确认页)。 */
public static final String LOGOUT_ENDPOINT = "/logout";
private CaptureRule() {
}
/**
* 是否命中捕获规则。path 为不含查询串的请求路径。
*
* <p>仅覆盖 {@code /apis/**} 写操作;认证端点由 {@link #isAuthEndpoint} 单独判定、
* <p>仅覆盖 {@code /apis/**} 写操作;认证操作由 {@link #isAuthOperation} 单独判定、
* 由 {@link AuthEndpointAuditWebFilter} 在 Security 链之前捕获,两个过滤器按此分治、
* 互不重叠,不会重复记录。
*/
@@ -44,11 +52,20 @@ public final class CaptureRule {
return method != null && WRITE_METHODS.contains(method);
}
/** 是否为登录/登出端点(由 {@link AuthEndpointAuditWebFilter} 捕获)。 */
/** 是否为登录/登出端点(仅用于路径归类,不代表需要记录)。 */
public static boolean isAuthEndpoint(String path) {
return path != null && AUTH_ENDPOINTS.contains(path);
}
/**
* 是否为认证操作(由 {@link AuthEndpointAuditWebFilter} 捕获):{@code POST /login}
* 与 {@code POST /logout}。登录/登出页面的 GET 访问不是操作,不记录。
*/
public static boolean isAuthOperation(String path, String method) {
return isLoginAttempt(path, method)
|| "POST".equals(method) && LOGOUT_ENDPOINT.equals(path);
}
/**
* 是否为登录尝试(POST {@value #LOGIN_ENDPOINT},产品设计 V2「告警通知」):
* 登录失败告警的触发条件——结果由 {@link LoginOutcome} 判定(表单登录成败均为
@@ -9,9 +9,15 @@ import java.util.Arrays;
* {@code /login?error=...&method=...}),仅 XHR/JSON 请求才以 200/401 区分成败
* (见 Halo {@code LoginSuccessHandler} / {@code LoginFailureHandler})。
* 因此登录结果须结合重定向目标判定,不能只看状态码。
*
* <p>判定原则:只有响应明确确认登录完成才算成功,其余一律按失败记录——审计里把失败
* 登录记成成功,远比把异常响应记成失败更糟。
*/
public final class LoginOutcome {
/** 登录页路径:重定向回登录页即表示没有登录成功。 */
private static final String LOGIN_PAGE_PATH = "/login";
private LoginOutcome() {
}
@@ -20,9 +26,9 @@ public final class LoginOutcome {
*
* <ul>
* <li>2xx → 成功(XHR/JSON 请求返回 200 + 主体 JSON);</li>
* <li>3xx → 重定向目标查询串含 {@code error} 参数 → 失败(表单登录失败统一重定向
* 到 {@code /login?error=invalid-credential&method=...} 等,含无值的
* {@code /login?error&method=...});其余重定向目标(成功 → {@code /uc}、
* <li>3xx → 重定向目标跳回登录页(查询串含 {@code error} 参数,如
* {@code /login?error=invalid-credential&method=...},或路径就是 {@code /login})
* 或缺失 Location → 失败;其余重定向目标(成功 → {@code /uc}、
* 两步验证 → {@code /challenges/two-factor/totp})→ 成功;</li>
* <li>其余(401 等)→ 失败(XHR 登录失败;状态码缺失/为 0 亦按失败处理)。</li>
* </ul>
@@ -35,29 +41,17 @@ public final class LoginOutcome {
return false;
}
if (statusCode >= 300 && statusCode < 400) {
return location != null && hasErrorParam(location.getRawQuery());
// 跳回登录页必然是失败:Halo 2.26 的四种失败重定向均带 error 参数,
// 无参跳回登录页同样不可能是登录成功(成功只会去 /uc、已保存的原请求或 2FA 页)
return location == null
|| LOGIN_PAGE_PATH.equals(location.getPath())
|| hasErrorParam(location.getRawQuery());
}
return true;
}
/**
* 登录失败回显判定:{@code GET /login} 且查询串含 {@code error} 参数。
* 登录失败后 Halo 重定向到带 error 参数的登录页,浏览器随后渲染该页(HTTP 200);
* 该请求是失败登录的回显而非独立操作,审计记录须标注登录失败而非「登录成功 200」。
*/
public static boolean isFailureEcho(String fullPath) {
if (fullPath == null) {
return false;
}
var queryStart = fullPath.indexOf('?');
if (queryStart < 0 || !"/login".equals(fullPath.substring(0, queryStart))) {
return false;
}
return hasErrorParam(fullPath.substring(queryStart + 1));
}
/** 查询串是否含 {@code error} 参数(可为无值形式 {@code error})。 */
private static boolean hasErrorParam(String query) {
static boolean hasErrorParam(String query) {
if (query == null || query.isEmpty()) {
return false;
}
@@ -12,10 +12,10 @@ import java.util.Map;
* 展示/导出由 {@link #describe} 推导中文短语,历史记录(无 action 字段)同样按
* method/path 现场推导,不受影响。
*
* <p>与前端 {@code ui/src/utils/audit.ts} 的 {@code deriveAction} / {@code describeOperation}
* 保持一致(同一套 RESOURCE_LABELS / METHOD_ACTIONS / SEGMENT_ACTIONS / ACTION_PHRASES /
* ACTION_VERBS 映射与子动作解析规则),两处需同步修改。未识别的子动作回退为
* 「method 动词 + 资源名」,未识别的资源类型直接使用原始 resourceType。
* <p>与前端 {@code ui/src/utils/audit.ts} 的 {@code deriveAction} / {@code semanticAction} /
* {@code describeOperation} 保持一致(同一套 RESOURCE_LABELS / METHOD_ACTIONS /
* SEGMENT_ACTIONS / ACTION_PHRASES / ACTION_VERBS 映射与子动作解析规则),两处需同步修改。
* 未识别的子动作回退为「method 动词 + 资源名」,未识别的资源类型直接使用原始 resourceType。
*/
final class OperationDescription {
@@ -65,17 +65,28 @@ final class OperationDescription {
Map.entry("resetpassword", "resetpassword"));
/**
* 登录失败语义动作键:POST {@code /login} 失败(重定向目标含 error 参数或 4xx/5xx)
* 与登录失败回显(GET {@code /login?error=...},HTTP 200)落库标注,供列表红色
* 标记、按语义动作筛选与结果筛选(前端 {@code audit.ts} 同步此键)。
* 登录失败语义动作键:POST {@code /login} 失败(重定向跳回登录页或 4xx/5xx)落库
* 标注,供列表红色标记、按语义动作筛选与结果筛选(前端 {@code audit.ts} 同步此键)。
* 升级前落库的失败回显页(GET {@code /login?error=...},HTTP 200)带同一标注。
*/
static final String LOGIN_FAILURE_ACTION = "login-failure";
/**
* 登录页访问语义动作键:GET {@code /login}(打开登录页、被弹回登录页、失败回显)。
* 仅升级前落库的历史记录会出现——页面访问不是操作,现已不记录。
*/
static final String LOGIN_PAGE_ACTION = "login-page";
/** 登出页访问语义动作键:GET {@code /logout}(登出确认页),仅升级前的历史记录。 */
static final String LOGOUT_PAGE_ACTION = "logout-page";
/** 语义动作键 → 完整短语(不拼接资源名)。 */
private static final Map<String, String> ACTION_PHRASES = Map.ofEntries(
Map.entry("login", "登录"),
Map.entry(LOGIN_FAILURE_ACTION, "登录失败"),
Map.entry("logout", "登出"),
Map.entry(LOGIN_PAGE_ACTION, "访问登录页"),
Map.entry(LOGOUT_PAGE_ACTION, "访问登出页"),
Map.entry("auth", "认证操作"),
Map.entry("password", "修改密码"),
Map.entry("resetpassword", "重置密码"));
@@ -102,17 +113,26 @@ final class OperationDescription {
}
/**
* 推导语义动作键(捕获时落库标注,供筛选):登录/登出/认证操作由路径判定,
* 推导语义动作键(捕获时落库标注,供筛选):登录/登出/认证操作由路径与方法判定,
* 子动作段优先于 HTTP 方法,其余方法归为 create/update/delete/view;
* 无法识别(未知方法等)返回 null,记录不标注。
*
* <p>认证端点按方法区分:POST 是登录/登出操作,其余方法是页面访问
* ({@link #LOGIN_PAGE_ACTION} / {@link #LOGOUT_PAGE_ACTION});路径含 {@code error}
* 参数的登录页是升级前落库的失败回显,无标注时现场推导为登录失败。
*/
static String actionOf(String method, String resourceType, String path) {
var cleanPath = pathWithoutQuery(path);
if ("/login".equals(cleanPath)) {
return "login";
var queryStart = path == null ? -1 : path.indexOf('?');
if (queryStart >= 0
&& LoginOutcome.hasErrorParam(path.substring(queryStart + 1))) {
return LOGIN_FAILURE_ACTION;
}
return "POST".equals(method) ? "login" : LOGIN_PAGE_ACTION;
}
if ("/logout".equals(cleanPath)) {
return "logout";
return "POST".equals(method) ? "logout" : LOGOUT_PAGE_ACTION;
}
if ("auth".equals(resourceType)) {
return "auth";
@@ -132,6 +152,35 @@ final class OperationDescription {
return METHOD_ACTIONS.get(method);
}
/**
* 是否为认证端点的页面访问:{@code GET /login}、{@code GET /logout}(含查询串),
* 即非 POST 的认证端点请求。页面访问不是登录/登出操作,结果不计成败;仅升级前
* 落库的历史记录会出现。
*/
static boolean isAuthPageVisit(String method, String path) {
if ("POST".equals(method)) {
return false;
}
var cleanPath = pathWithoutQuery(path);
return "/login".equals(cleanPath) || "/logout".equals(cleanPath);
}
/**
* 记录的语义动作键(展示、导出与结果判定共用):优先落库标注;认证端点的失败回显与
* 页面访问(升级前落库为 login/logout 或无标注的历史记录)按方法与路径纠正;
* 落库标注未识别时回退 method/path 现场推导(与前端 {@code semanticAction} 一致)。
*/
static String semanticAction(AuditLogSpec spec) {
var derived = actionOf(spec.getMethod(), spec.getResourceType(), spec.getPath());
if (LOGIN_FAILURE_ACTION.equals(derived) || LOGIN_PAGE_ACTION.equals(derived)
|| LOGOUT_PAGE_ACTION.equals(derived)) {
return derived;
}
var stored = spec.getAction();
return stored != null && describeAction(stored, spec.getResourceType()) != null
? stored : derived;
}
/** 由语义动作键推导中文描述;键为 null 或未识别返回 null(调用方回退 method 推导)。 */
static String describeAction(String action, String resourceType) {
if (action == null) {
@@ -158,12 +207,12 @@ final class OperationDescription {
}
/**
* 由落库 spec 推导操作中文描述(导出用):优先落库的语义动作标注(含登录失败
* 标注——回显页 HTTP 200 无法由路径重新推导出失败),无标注或未识别时回退为
* method/path 现场推导(与前端 {@code describeOperation} 一致)。
* 由落库 spec 推导操作中文描述(导出用):以 {@link #semanticAction} 为准
* (含登录失败标注与页面访问纠正),无标注或未识别时回退为 method/path 现场推导
* (与前端 {@code describeOperation} 一致)。
*/
static String describe(AuditLogSpec spec) {
var phrase = describeAction(spec.getAction(), spec.getResourceType());
var phrase = describeAction(semanticAction(spec), spec.getResourceType());
if (phrase != null) {
return phrase;
}