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
@@ -9,6 +9,9 @@ import org.junit.jupiter.api.Test;
* 登录结果判定测试(LoginOutcome):Halo 2.26 登录端点表单提交的成功与失败均为
* 302 重定向(成功 → /uc、2FA 页;失败 → /login?error=...&method=...),仅 XHR/JSON
* 请求以 200/401 区分成败,成败判定必须结合重定向目标。
*
* <p>判定原则:只有响应明确确认登录完成才算成功,其余一律按失败记录——审计里把
* 失败登录记成成功,远比把异常响应记成失败更糟。
*/
class LoginOutcomeTest {
@@ -34,26 +37,42 @@ class LoginOutcomeTest {
// 两步验证:302 → /challenges/two-factor/totp
assertThat(LoginOutcome.isFailure(302,
URI.create("/challenges/two-factor/totp"))).isFalse();
// 登录前被弹回的原请求:302 → 已保存的受保护页面
assertThat(LoginOutcome.isFailure(302, URI.create("/console/posts"))).isFalse();
assertThat(LoginOutcome.isFailure(302,
URI.create("http://blog.guichumo.com/console/posts?page=1"))).isFalse();
}
@Test
void formFailureRedirectWithErrorParamIsFailure() {
// 表单登录失败:302 → /login?error=invalid-credential&method=local
// (Halo 2.26.1 LoginFailureHandler 的四种失败重定向:invalid-credential /
// account-disabled / rate-limit-exceeded / 无值 error)
assertThat(LoginOutcome.isFailure(302,
URI.create("/login?error=invalid-credential&method=local"))).isTrue();
// 账号禁用/限流等其他失败码同样含 error 参数
assertThat(LoginOutcome.isFailure(302,
URI.create("/login?error=account-disabled&method=local"))).isTrue();
assertThat(LoginOutcome.isFailure(302,
URI.create("/login?error=rate-limit-exceeded&method=local"))).isTrue();
// 兜底失败分支:error 参数无值(/login?error&method=...)
assertThat(LoginOutcome.isFailure(302, URI.create("/login?error&method=local"))).isTrue();
assertThat(LoginOutcome.isFailure(302,
URI.create("http://blog.guichumo.com/login?error=invalid-credential"))).isTrue();
}
@Test
void redirectWithoutLocationIsNotFailure() {
// 3xx 但缺失 Location(无法确认失败):按成功处理,不凭空判失败
assertThat(LoginOutcome.isFailure(302, null)).isFalse();
void redirectBackToLoginPageWithoutErrorParamIsFailure() {
// 跳回登录页必然是失败:成功只会去 /uc、已保存的原请求或 2FA 页。
// 不依赖 error 参数——宿主或第三方登录扩展换了失败重定向格式也不会把失败记成成功
assertThat(LoginOutcome.isFailure(302, URI.create("/login"))).isTrue();
assertThat(LoginOutcome.isFailure(302, URI.create("/login?authentication_required")))
.isTrue();
assertThat(LoginOutcome.isFailure(307, URI.create("/login?logout"))).isTrue();
}
@Test
void redirectWithoutLocationIsFailure() {
// 3xx 但缺失 Location:响应异常,无法确认登录成功,不记成功
assertThat(LoginOutcome.isFailure(302, null)).isTrue();
}
@Test
@@ -63,41 +82,22 @@ class LoginOutcomeTest {
assertThat(LoginOutcome.isFailure(0, null)).isTrue();
}
@Test
void clientAndServerErrorsAreFailure() {
// CSRF 拦截(403)、服务端异常(500)同为失败
assertThat(LoginOutcome.isFailure(403, URI.create("/login"))).isTrue();
assertThat(LoginOutcome.isFailure(500, null)).isTrue();
}
@Test
void errorParamNameMustMatchExactly() {
// 含 error 字样的其他参数(如 errors=1)不算失败标记
assertThat(LoginOutcome.isFailure(302, URI.create("/uc?errors=1"))).isFalse();
assertThat(LoginOutcome.isFailure(302, URI.create("/login?errors=1"))).isFalse();
}
// ===== GET /login?error=...:失败回显 =====
@Test
void loginPageWithErrorParamIsFailureEcho() {
assertThat(LoginOutcome.isFailureEcho("/login?error=invalid-credential&method=local"))
.isTrue();
assertThat(LoginOutcome.isFailureEcho("/login?error&method=local")).isTrue();
assertThat(LoginOutcome.isFailureEcho("/login?method=local&error=invalid-credential"))
.isTrue();
}
@Test
void plainLoginPageIsNotFailureEcho() {
assertThat(LoginOutcome.isFailureEcho("/login")).isFalse();
assertThat(LoginOutcome.isFailureEcho("/login?method=local")).isFalse();
}
@Test
void otherEndpointsAreNotFailureEcho() {
assertThat(LoginOutcome.isFailureEcho("/logout?error=x")).isFalse();
assertThat(LoginOutcome.isFailureEcho("/apis/core.halo.run/v1alpha1/login?error=x"))
.isFalse();
assertThat(LoginOutcome.isFailureEcho("/uc?error=x")).isFalse();
}
@Test
void nullOrQuerylessPathIsNotFailureEcho() {
assertThat(LoginOutcome.isFailureEcho(null)).isFalse();
assertThat(LoginOutcome.isFailureEcho("/apis/content.halo.run/v1alpha1/posts")).isFalse();
void loginPagePathMustMatchExactly() {
// 仅路径恰为 /login 才算跳回登录页;/login-xxx 等前缀相同的路径不算
assertThat(LoginOutcome.isFailure(302, URI.create("/login-callback"))).isFalse();
}
}