Skip to content

审计日志 API

审计日志接口用于查询系统操作日志,包括用户登录、注册、令牌签发等关键事件。审计日志为只读,不支持修改或删除。

所有接口均需要管理员令牌认证:Authorization: Bearer <admin_token>


获取日志列表

GET /api/v1/logs

分页获取审计日志列表。日志按时间倒序排列,最新的日志排在最前面。

查询参数:

参数类型必填默认值说明
pageinteger1页码
page_sizeinteger20每页条数

请求示例:

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
  }
}

响应字段说明:

字段类型说明
idstring日志 ID
keystring日志事件类型(见下方事件类型列表)
payloadobject事件详细数据(JSON 对象,结构因事件类型而异)
user_idstring/null关联的用户 ID(未认证的操作可能为 null)
application_idstring/null关联的应用 ID
ipstring请求来源 IP 地址
user_agentstring请求的 User-Agent 信息
created_atstring日志创建时间(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 获取日志详细信息。

路径参数:

参数类型说明
idstring日志 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": ""
}

Released under the MIT License.