审计日志 API
审计日志接口用于查询系统操作日志,包括用户登录、注册、令牌签发等关键事件。审计日志为只读,不支持修改或删除。
所有接口均需要管理员令牌认证:
Authorization: Bearer <admin_token>
获取日志列表
GET /api/v1/logs
分页获取审计日志列表。日志按时间倒序排列,最新的日志排在最前面。
查询参数:
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
page | integer | 否 | 1 | 页码 |
page_size | integer | 否 | 20 | 每页条数 |
请求示例:
bash
curl -X GET "https://your-domain/api/v1/logs?page=1&page_size=10" \
-H "Authorization: Bearer <admin_token>"成功响应:
json
{
"code": 0,
"message": "success",
"result": {
"data": [
{
"id": "log_001",
"key": "SignIn.Password",
"payload": {
"username": "john_doe",
"result": "success"
},
"user_id": "usr_abc123",
"application_id": "app_001",
"ip": "192.168.1.100",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"created_at": "2025-06-15T10:30:00Z"
},
{
"id": "log_002",
"key": "SignIn.Password",
"payload": {
"username": "unknown_user",
"result": "failed",
"error": "invalid_credentials"
},
"user_id": null,
"application_id": "app_001",
"ip": "10.0.0.55",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"created_at": "2025-06-15T10:25:00Z"
},
{
"id": "log_003",
"key": "Register.Username",
"payload": {
"username": "new_user",
"result": "success"
},
"user_id": "usr_xyz789",
"application_id": "app_001",
"ip": "172.16.0.10",
"user_agent": "CodeBird/1.0 (iOS)",
"created_at": "2025-06-15T09:00:00Z"
},
{
"id": "log_004",
"key": "Token.Exchange",
"payload": {
"grant_type": "authorization_code",
"result": "success"
},
"user_id": "usr_abc123",
"application_id": "app_001",
"ip": "192.168.1.100",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"created_at": "2025-06-15T08:45:00Z"
}
],
"total": 1280,
"page": 1,
"page_size": 10
}
}响应字段说明:
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 日志 ID |
key | string | 日志事件类型(见下方事件类型列表) |
payload | object | 事件详细数据(JSON 对象,结构因事件类型而异) |
user_id | string/null | 关联的用户 ID(未认证的操作可能为 null) |
application_id | string/null | 关联的应用 ID |
ip | string | 请求来源 IP 地址 |
user_agent | string | 请求的 User-Agent 信息 |
created_at | string | 日志创建时间(ISO 8601) |
常见事件类型(key):
| 事件类型 | 说明 |
|---|---|
SignIn.Password | 用户名密码登录 |
SignIn.VerificationCode | 验证码登录 |
SignIn.Social | 社交账号登录 |
Register.Username | 用户名注册 |
Register.Email | 邮箱注册 |
Register.Phone | 手机号注册 |
Token.Exchange | 令牌交换(授权码换令牌) |
Token.Refresh | 令牌刷新 |
Token.Revoke | 令牌撤销 |
ForgotPassword | 忘记密码/重置密码 |
VerificationCode.Send | 发送验证码 |
VerificationCode.Verify | 验证码校验 |
获取日志详情
GET /api/v1/logs/:id
根据日志 ID 获取日志详细信息。
路径参数:
| 参数 | 类型 | 说明 |
|---|---|---|
id | string | 日志 ID |
请求示例:
bash
curl -X GET https://your-domain/api/v1/logs/log_001 \
-H "Authorization: Bearer <admin_token>"成功响应:
json
{
"code": 0,
"message": "success",
"result": {
"id": "log_001",
"key": "SignIn.Password",
"payload": {
"username": "john_doe",
"result": "success",
"session_id": "sess_abc123"
},
"user_id": "usr_abc123",
"application_id": "app_001",
"ip": "192.168.1.100",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"created_at": "2025-06-15T10:30:00Z"
}
}错误响应(日志不存在):
json
{
"code": 404,
"message": "日志记录不存在",
"result": ""
}