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:
@@ -6,8 +6,10 @@ import {
|
||||
deriveAction,
|
||||
describeOperation,
|
||||
displayUsername,
|
||||
isAuthPageVisit,
|
||||
isLoginFailure,
|
||||
resultBadge,
|
||||
resultOutcome,
|
||||
SEMANTIC_ACTIONS,
|
||||
SYSTEM_USERNAME,
|
||||
UNKNOWN_USERNAME,
|
||||
@@ -28,27 +30,58 @@ function spec(overrides: Partial<AuditLogSpec>): AuditLogSpec {
|
||||
}
|
||||
}
|
||||
|
||||
/** 升级前落库的登录页访问记录:GET、HTTP 200、带着 login 标注(当时 GET 也被当成登录记录)。 */
|
||||
function legacyLoginPageVisit(overrides: Partial<AuditLogSpec> = {}): AuditLogSpec {
|
||||
return spec({
|
||||
username: 'unknown',
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
statusCode: 200,
|
||||
remoteIp: '43.228.157.244',
|
||||
action: 'login',
|
||||
...overrides,
|
||||
})
|
||||
}
|
||||
|
||||
describe('describeOperation', () => {
|
||||
it('登录端点(含查询串)描述为 登录', () => {
|
||||
it('登录端点:POST 描述为 登录,GET(含查询串)描述为 访问登录页', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
}))).toBe('登录')
|
||||
// GET /login?redirect_uri=... 也应识别为登录(spec.path 含查询串)
|
||||
// GET /login 只是打开登录页,不是登录操作(spec.path 含查询串同样如此)
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole',
|
||||
}))).toBe('登录')
|
||||
}))).toBe('访问登录页')
|
||||
// 匿名访问受保护页面被弹回登录页
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?authentication_required',
|
||||
}))).toBe('访问登录页')
|
||||
// 升级前落库的失败回显(GET /login?error=...)无标注时由 error 参数现场推导
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
}))).toBe('登录失败')
|
||||
})
|
||||
|
||||
it('登出端点描述为 登出', () => {
|
||||
it('登出端点:POST 描述为 登出,GET(登出确认页)描述为 访问登出页', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/logout',
|
||||
}))).toBe('登出')
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/logout',
|
||||
}))).toBe('访问登出页')
|
||||
})
|
||||
|
||||
it('auth 类型但非登录/登出路径回退为 认证操作', () => {
|
||||
@@ -147,25 +180,26 @@ describe('describeOperation', () => {
|
||||
})
|
||||
|
||||
describe('deriveAction 语义动作推导', () => {
|
||||
it('登录/登出/认证操作由路径判定(含查询串剥离)', () => {
|
||||
it('登录/登出按方法区分:POST 是操作,GET 是页面访问;认证操作由路径判定', () => {
|
||||
expect(deriveAction('POST', 'auth', '/login')).toBe('login')
|
||||
expect(deriveAction('GET', 'auth', '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com'))
|
||||
.toBe('login')
|
||||
.toBe('login-page')
|
||||
expect(deriveAction('GET', 'auth', '/login?authentication_required')).toBe('login-page')
|
||||
expect(deriveAction('POST', 'auth', '/logout')).toBe('logout')
|
||||
expect(deriveAction('GET', 'auth', '/logout')).toBe('logout-page')
|
||||
expect(deriveAction('POST', 'auth', '/auth/other')).toBe('auth')
|
||||
})
|
||||
|
||||
it('登录失败回显(GET /login?error=...)推导为 login-failure', () => {
|
||||
// 登录失败后 Halo 重定向到带 error 参数的登录页,页面渲染 HTTP 200;
|
||||
// 历史记录(无落库标注)由路径中的 error 参数现场推导
|
||||
// 升级前落库的失败回显页(HTTP 200)无标注时由路径中的 error 参数现场推导
|
||||
expect(deriveAction('GET', 'auth', '/login?error=invalid-credential&method=local'))
|
||||
.toBe('login-failure')
|
||||
// 兜底失败分支:error 参数无值
|
||||
expect(deriveAction('GET', 'auth', '/login?error&method=local')).toBe('login-failure')
|
||||
// 首次打开登录页(无 error 参数)不是失败回显
|
||||
expect(deriveAction('GET', 'auth', '/login?method=local')).toBe('login')
|
||||
// 不带 error 参数的登录页只是页面访问
|
||||
expect(deriveAction('GET', 'auth', '/login?method=local')).toBe('login-page')
|
||||
// 含 error 字样的其他参数不算失败标记
|
||||
expect(deriveAction('GET', 'auth', '/login?errors=1')).toBe('login')
|
||||
expect(deriveAction('GET', 'auth', '/login?errors=1')).toBe('login-page')
|
||||
})
|
||||
|
||||
it('子动作段优先于 HTTP 方法', () => {
|
||||
@@ -236,92 +270,126 @@ describe('describeOperation 落库标注优先', () => {
|
||||
})
|
||||
|
||||
it('登录失败标注(login-failure)描述为 登录失败', () => {
|
||||
// 表单登录失败(POST /login,302)与失败回显页(GET /login?error=...,200)
|
||||
// 均落库标注,描述为 登录失败 而非按路径推导出的 登录
|
||||
// 表单登录失败(POST /login,302)落库标注 login-failure,描述为 登录失败
|
||||
// 而非按路径推导出的 登录
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
action: 'login-failure',
|
||||
}))).toBe('登录失败')
|
||||
// 升级前落库的失败回显页(GET /login?error=...,200)带同一标注
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
action: 'login-failure',
|
||||
}))).toBe('登录失败')
|
||||
// 历史回显记录(无落库标注)由路径现场推导,同样得到 登录失败
|
||||
// 无标注的历史回显记录由路径现场推导,同样得到 登录失败
|
||||
expect(describeOperation(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
}))).toBe('登录失败')
|
||||
})
|
||||
|
||||
it('升级前落库的登录页访问记录(GET + login 标注)纠正为 访问登录页,不再显示成登录', () => {
|
||||
// 当时登录页的 GET 访问被记成「未知用户 · 登录 · 200」,看起来像登录成功
|
||||
expect(describeOperation(legacyLoginPageVisit())).toBe('访问登录页')
|
||||
expect(describeOperation(legacyLoginPageVisit({
|
||||
path: '/login?authentication_required',
|
||||
}))).toBe('访问登录页')
|
||||
expect(describeOperation(legacyLoginPageVisit({
|
||||
path: '/logout',
|
||||
action: 'logout',
|
||||
}))).toBe('访问登出页')
|
||||
// 无标注的页面访问记录同样推导为页面访问
|
||||
expect(describeOperation(legacyLoginPageVisit({ action: undefined }))).toBe('访问登录页')
|
||||
// POST 才是登录/登出操作,落库标注照常生效
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
action: 'login',
|
||||
}))).toBe('登录')
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/logout',
|
||||
action: 'logout',
|
||||
}))).toBe('登出')
|
||||
})
|
||||
})
|
||||
|
||||
describe('isLoginFailure / resultBadge 登录失败结果展示', () => {
|
||||
it('落库标注 login-failure 判定为登录失败(HTTP 200/302 也可能是失败)', () => {
|
||||
expect(isLoginFailure(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
statusCode: 302,
|
||||
describe('isAuthPageVisit 认证端点的页面访问', () => {
|
||||
it('GET /login、GET /logout(含查询串)是页面访问;POST 是认证操作', () => {
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/login' }))).toBe(true)
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/login?authentication_required' })))
|
||||
.toBe(true)
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/login?error=x' }))).toBe(true)
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/logout' }))).toBe(true)
|
||||
expect(isAuthPageVisit(spec({ method: 'POST', path: '/login' }))).toBe(false)
|
||||
expect(isAuthPageVisit(spec({ method: 'POST', path: '/logout' }))).toBe(false)
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/console/' }))).toBe(false)
|
||||
expect(isAuthPageVisit(spec({ method: 'GET', path: '/apis/core.halo.run/v1alpha1/login' })))
|
||||
.toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('resultOutcome / resultBadge 结果语义', () => {
|
||||
it('登录失败为 failure:红色 失败,不显示裸状态码(表单失败是 302、失败回显是 200)', () => {
|
||||
const failed = spec({
|
||||
method: 'POST', resourceType: 'auth', path: '/login', statusCode: 302,
|
||||
action: 'login-failure',
|
||||
}))).toBe(true)
|
||||
expect(isLoginFailure(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
})
|
||||
expect(isLoginFailure(failed)).toBe(true)
|
||||
expect(resultOutcome(failed)).toBe('failure')
|
||||
expect(resultBadge(failed)).toEqual({ text: '失败', class: 'status-fail' })
|
||||
// XHR 登录失败(401)、CSRF 拦截(403)
|
||||
expect(resultOutcome({ ...failed, statusCode: 401 })).toBe('failure')
|
||||
expect(resultOutcome({ ...failed, statusCode: 403 })).toBe('failure')
|
||||
// 升级前落库的失败回显页(GET,200):带标注或由路径推导
|
||||
const echo = spec({
|
||||
method: 'GET', resourceType: 'auth', statusCode: 200,
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
statusCode: 200,
|
||||
action: 'login-failure',
|
||||
}))).toBe(true)
|
||||
})
|
||||
expect(resultOutcome({ ...echo, action: 'login-failure' })).toBe('failure')
|
||||
expect(isLoginFailure(echo)).toBe(true)
|
||||
expect(resultBadge({ ...echo, action: 'login-failure' }))
|
||||
.toEqual({ text: '失败', class: 'status-fail' })
|
||||
})
|
||||
|
||||
it('历史回显记录(无落库标注)由路径现场推导', () => {
|
||||
expect(isLoginFailure(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
statusCode: 200,
|
||||
}))).toBe(true)
|
||||
// 表单登录失败的历史记录(POST /login,302)无法由路径推导,仍按登录展示
|
||||
// (升级后新记录落库标注,不再有此局限)
|
||||
expect(isLoginFailure(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
statusCode: 302,
|
||||
}))).toBe(false)
|
||||
// 成功登录(POST /login,302 → /uc)不是登录失败
|
||||
expect(isLoginFailure(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
statusCode: 302,
|
||||
action: 'login',
|
||||
}))).toBe(false)
|
||||
it('登录成功为 success:绿色 成功(表单成功同样是 302,裸状态码读不出成败)', () => {
|
||||
const success = spec({
|
||||
method: 'POST', resourceType: 'auth', path: '/login', statusCode: 302, action: 'login',
|
||||
})
|
||||
expect(isLoginFailure(success)).toBe(false)
|
||||
expect(resultOutcome(success)).toBe('success')
|
||||
expect(resultBadge(success)).toEqual({ text: '成功', class: 'status-ok' })
|
||||
// XHR 登录成功(200)
|
||||
expect(resultBadge({ ...success, statusCode: 200 }))
|
||||
.toEqual({ text: '成功', class: 'status-ok' })
|
||||
})
|
||||
|
||||
it('登录失败记录结果徽标为红色 失败(不显示裸状态码)', () => {
|
||||
// 失败回显页 HTTP 200:修复前显示绿色 200,误读为登录成功
|
||||
expect(resultBadge(spec({
|
||||
method: 'GET',
|
||||
resourceType: 'auth',
|
||||
path: '/login?error=invalid-credential&method=local',
|
||||
statusCode: 200,
|
||||
action: 'login-failure',
|
||||
}))).toEqual({ text: '失败', class: 'status-fail' })
|
||||
// 表单登录失败 302 重定向:同理
|
||||
expect(resultBadge(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
statusCode: 302,
|
||||
action: 'login-failure',
|
||||
}))).toEqual({ text: '失败', class: 'status-fail' })
|
||||
it('登录页访问记录为 page-visit:灰色 —,既不算成功也不算失败', () => {
|
||||
// 升级前落库的 GET /login(HTTP 200、带 login 标注)原先显示绿色「200」,
|
||||
// 看起来像未知用户登录成功;现在按方法纠正为页面访问
|
||||
const visit = legacyLoginPageVisit({ path: '/login?authentication_required' })
|
||||
expect(isLoginFailure(visit)).toBe(false)
|
||||
expect(resultOutcome(visit)).toBe('page-visit')
|
||||
expect(resultBadge(visit)).toEqual({ text: '—', class: 'status-na' })
|
||||
expect(resultOutcome(legacyLoginPageVisit({ path: '/logout', action: 'logout' })))
|
||||
.toBe('page-visit')
|
||||
// 无标注的历史记录、页面自身出错(5xx)同样不计成败
|
||||
expect(resultOutcome(legacyLoginPageVisit({ action: undefined }))).toBe('page-visit')
|
||||
expect(resultOutcome(legacyLoginPageVisit({ statusCode: 500 }))).toBe('page-visit')
|
||||
})
|
||||
|
||||
it('非登录记录保持原状:数字状态码徽标(2xx/3xx 绿、其余红)', () => {
|
||||
it('登出与其他记录保持原状:数字状态码徽标(2xx/3xx 绿、其余红)', () => {
|
||||
const logout = spec({
|
||||
method: 'POST', resourceType: 'auth', path: '/logout', statusCode: 302, action: 'logout',
|
||||
})
|
||||
expect(resultBadge(logout)).toEqual({ text: '302', class: 'status-ok' })
|
||||
expect(resultBadge(spec({ statusCode: 200 }))).toEqual({ text: '200', class: 'status-ok' })
|
||||
expect(resultBadge(spec({ statusCode: 302 }))).toEqual({ text: '302', class: 'status-ok' })
|
||||
expect(resultBadge(spec({ statusCode: 403 }))).toEqual({ text: '403', class: 'status-fail' })
|
||||
|
||||
@@ -107,11 +107,19 @@ const SEGMENT_ACTIONS: Record<string, string> = {
|
||||
/** 登录失败语义动作键(与后端 OperationDescription.LOGIN_FAILURE_ACTION 同步)。 */
|
||||
const LOGIN_FAILURE_ACTION = 'login-failure'
|
||||
|
||||
/** 登录页访问(打开登录页、被弹回登录页、失败回显):仅升级前落库的历史记录。 */
|
||||
const LOGIN_PAGE_ACTION = 'login-page'
|
||||
|
||||
/** 登出页访问(登出确认页):仅升级前落库的历史记录。 */
|
||||
const LOGOUT_PAGE_ACTION = 'logout-page'
|
||||
|
||||
/** 语义动作键 → 完整短语(不拼资源名)。 */
|
||||
const ACTION_PHRASES: Record<string, string> = {
|
||||
login: '登录',
|
||||
[LOGIN_FAILURE_ACTION]: '登录失败',
|
||||
logout: '登出',
|
||||
[LOGIN_PAGE_ACTION]: '访问登录页',
|
||||
[LOGOUT_PAGE_ACTION]: '访问登出页',
|
||||
auth: '认证操作',
|
||||
password: '修改密码',
|
||||
resetpassword: '重置密码',
|
||||
@@ -174,7 +182,7 @@ function pathWithoutQuery(path?: string | null): string {
|
||||
|
||||
/**
|
||||
* 查询串是否含 error 参数(可为无值形式 error)。
|
||||
* 登录失败回显页(GET /login?error=invalid-credential&method=...)由该参数识别。
|
||||
* 升级前落库的失败回显页(GET /login?error=invalid-credential&method=...)由该参数识别。
|
||||
*/
|
||||
function hasErrorParam(path?: string | null): boolean {
|
||||
if (!path) {
|
||||
@@ -190,26 +198,40 @@ function hasErrorParam(path?: string | null): boolean {
|
||||
.some((pair) => (pair.indexOf('=') < 0 ? pair : pair.slice(0, pair.indexOf('='))) === 'error')
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为认证端点的页面访问(GET /login、GET /logout,含查询串):打开登录页、匿名访问
|
||||
* 被弹回登录页(?authentication_required)、失败回显(?error=...)、登出确认页。
|
||||
* 页面访问不是登录/登出操作,结果不计成败;现已不再记录,只有升级前落库的历史记录
|
||||
* 会出现——当时它们被记成「未知用户 · 登录 · 200」,看起来像登录成功。
|
||||
*/
|
||||
export function isAuthPageVisit(spec: Pick<AuditLogSpec, 'method' | 'path'>): boolean {
|
||||
if (spec.method === 'POST') {
|
||||
return false
|
||||
}
|
||||
const cleanPath = pathWithoutQuery(spec.path)
|
||||
return cleanPath === '/login' || cleanPath === '/logout'
|
||||
}
|
||||
|
||||
/**
|
||||
* 推导语义动作键(语义动作标注,与后端 OperationDescription.actionOf 保持一致):
|
||||
* 登录/登录失败/登出/认证操作由路径判定(GET /login?error=... 为登录失败回显),
|
||||
* 子动作段优先于 HTTP 方法,其余方法归为 create/update/delete/view;
|
||||
* 认证端点按方法区分——POST 是登录/登出操作,其余方法是页面访问,路径带 error 参数的
|
||||
* 登录页是失败回显;子动作段优先于 HTTP 方法,其余方法归为 create/update/delete/view;
|
||||
* 无法识别返回 null。
|
||||
*
|
||||
* <p>历史局限:升级前落库的表单登录失败记录(POST /login,302)无法由路径重新
|
||||
* 推导出失败,仍推导为 login;仅回显页(GET,error 参数在路径中)可现场推导。
|
||||
*/
|
||||
export function deriveAction(
|
||||
method: string,
|
||||
resourceType: string,
|
||||
path?: string | null,
|
||||
path?: string | null
|
||||
): string | null {
|
||||
const cleanPath = pathWithoutQuery(path)
|
||||
if (cleanPath === '/login') {
|
||||
return hasErrorParam(path) ? LOGIN_FAILURE_ACTION : 'login'
|
||||
if (hasErrorParam(path)) {
|
||||
return LOGIN_FAILURE_ACTION
|
||||
}
|
||||
return method === 'POST' ? 'login' : LOGIN_PAGE_ACTION
|
||||
}
|
||||
if (cleanPath === '/logout') {
|
||||
return 'logout'
|
||||
return method === 'POST' ? 'logout' : LOGOUT_PAGE_ACTION
|
||||
}
|
||||
if (resourceType === 'auth') {
|
||||
return 'auth'
|
||||
@@ -240,19 +262,36 @@ function describeFromAction(action: string | null, resourceType: string): string
|
||||
return verb ? `${verb}${RESOURCE_LABELS[resourceType] ?? resourceType}` : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录的语义动作键(展示与结果判定共用,与后端 OperationDescription.semanticAction 一致):
|
||||
* 优先落库标注(spec.action);认证端点的失败回显与页面访问——升级前落库为 login/logout
|
||||
* 或无标注的历史记录——按方法与路径纠正;落库标注未识别时回退 method/path 现场推导。
|
||||
*/
|
||||
function semanticAction(
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action'>
|
||||
): string | null {
|
||||
const derived = deriveAction(spec.method, spec.resourceType, spec.path)
|
||||
if (
|
||||
derived === LOGIN_FAILURE_ACTION ||
|
||||
derived === LOGIN_PAGE_ACTION ||
|
||||
derived === LOGOUT_PAGE_ACTION
|
||||
) {
|
||||
return derived
|
||||
}
|
||||
return spec.action && describeFromAction(spec.action, spec.resourceType) ? spec.action : derived
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作描述(这条操作真正干了什么):推导中文短语,
|
||||
* 如 登录、登出、新建用户、修改文章、发布文章、启用插件、修改密码。
|
||||
*
|
||||
* <p>优先使用落库的语义动作标注(spec.action);历史记录(无 action 字段)由
|
||||
* method/path 现场推导,同样适用。识别失败回退为「method 动词 + 资源名」,
|
||||
* 未识别的资源类型直接使用原始 resourceType。
|
||||
* <p>以记录的语义动作键为准(含登录失败标注与页面访问纠正);识别失败回退为
|
||||
* 「method 动词 + 资源名」,未识别的资源类型直接使用原始 resourceType。
|
||||
*/
|
||||
export function describeOperation(
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action'>,
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action'>
|
||||
): string {
|
||||
const action = spec.action ?? deriveAction(spec.method, spec.resourceType, spec.path)
|
||||
const fromAction = describeFromAction(action, spec.resourceType)
|
||||
const fromAction = describeFromAction(semanticAction(spec), spec.resourceType)
|
||||
if (fromAction) {
|
||||
return fromAction
|
||||
}
|
||||
@@ -277,23 +316,49 @@ export function isSuccessful(statusCode?: number | null): boolean {
|
||||
return statusCode != null && statusCode >= 200 && statusCode < 400
|
||||
}
|
||||
|
||||
/** 结果语义(与后端 AuditResult 同一规则,结果筛选与 CSV 导出沿用)。 */
|
||||
export type ResultOutcome = 'success' | 'failure' | 'page-visit'
|
||||
|
||||
type ResultSpec = Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action' | 'statusCode'>
|
||||
|
||||
/**
|
||||
* 记录的结果语义:登录失败(语义动作 login-failure,表单登录失败的 HTTP 状态码与成功
|
||||
* 相同、均为 302,只能靠标注识别)为 failure;认证端点的页面访问(升级前落库的 GET
|
||||
* 记录)不是操作,为 page-visit,不计成败;其余按状态码 2xx/3xx 判成功。
|
||||
*/
|
||||
export function resultOutcome(spec: ResultSpec): ResultOutcome {
|
||||
if (isLoginFailure(spec)) {
|
||||
return 'failure'
|
||||
}
|
||||
if (isAuthPageVisit(spec)) {
|
||||
return 'page-visit'
|
||||
}
|
||||
return isSuccessful(spec.statusCode) ? 'success' : 'failure'
|
||||
}
|
||||
|
||||
/** 结果徽标展示形态:文本与配色 class。 */
|
||||
export interface ResultBadge {
|
||||
text: string
|
||||
class: 'status-ok' | 'status-fail'
|
||||
class: 'status-ok' | 'status-fail' | 'status-na'
|
||||
}
|
||||
|
||||
/**
|
||||
* 结果列徽标(列表/详情共用):登录失败记录(语义动作 login-failure,HTTP 状态码可能
|
||||
* 为 200/302——失败回显页渲染/表单登录失败重定向)显示「失败」而非裸状态码,避免
|
||||
* 绿色 200/302 误读为登录成功;其余记录保持原状——按 HTTP 状态码展示
|
||||
* (2xx/3xx 绿、其余红),与既有行为一致。
|
||||
* 结果列徽标(列表/详情共用):
|
||||
* - 登录失败显示红色「失败」——表单登录失败是 302、失败回显页是 200,裸状态码是绿的;
|
||||
* - 登录成功显示绿色「成功」——表单登录成功同样是 302,与「失败」对称才读得出成败;
|
||||
* - 认证端点的页面访问(升级前落库的历史 GET 记录)显示灰色「—」,不计成败;
|
||||
* - 其余记录按 HTTP 状态码展示(2xx/3xx 绿、其余红)。
|
||||
*/
|
||||
export function resultBadge(spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'
|
||||
| 'action' | 'statusCode'>): ResultBadge {
|
||||
export function resultBadge(spec: ResultSpec): ResultBadge {
|
||||
if (isLoginFailure(spec)) {
|
||||
return { text: '失败', class: 'status-fail' }
|
||||
}
|
||||
if (isAuthPageVisit(spec)) {
|
||||
return { text: '—', class: 'status-na' }
|
||||
}
|
||||
if (semanticAction(spec) === 'login' && isSuccessful(spec.statusCode)) {
|
||||
return { text: '成功', class: 'status-ok' }
|
||||
}
|
||||
return {
|
||||
text: String(spec.statusCode),
|
||||
class: isSuccessful(spec.statusCode) ? 'status-ok' : 'status-fail',
|
||||
@@ -301,13 +366,13 @@ export function resultBadge(spec: Pick<AuditLogSpec, 'method' | 'resourceType' |
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为登录失败记录:优先落库语义动作标注(spec.action,POST /login 失败与
|
||||
* 失败回显均落库),历史记录回退由路径现场推导(仅回显页可推导)。
|
||||
* 是否为登录失败记录:以记录的语义动作键为准(POST /login 失败落库标注 login-failure;
|
||||
* 升级前落库的失败回显页带同一标注,或由路径中的 error 参数现场推导)。
|
||||
*/
|
||||
export function isLoginFailure(spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'
|
||||
| 'action'>): boolean {
|
||||
const action = spec.action ?? deriveAction(spec.method, spec.resourceType, spec.path)
|
||||
return action === LOGIN_FAILURE_ACTION
|
||||
export function isLoginFailure(
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action'>
|
||||
): boolean {
|
||||
return semanticAction(spec) === LOGIN_FAILURE_ACTION
|
||||
}
|
||||
|
||||
/** 变更类型 → 中文标签(未知类型回退原始值)。 */
|
||||
|
||||
Reference in New Issue
Block a user