Skip to content

认证与安全

后台管理接口使用 JWT 作为主要认证手段,辅以验证码与角色切换机制。以下接口均位于 /auth 路径下。

获取验证码

  • MethodGET
  • Path/auth/captcha
  • 说明:返回 SVG 图片。后端会将验证码存入 session (req.session.code) 并通过响应头下发 Set-Cookie。前端需在登录前调用此接口并保持 Cookie。

登录

  • MethodPOST
  • Path/auth/login
  • 守卫LocalGuard

请求体

字段类型必填说明
usernamestring用户名(6~20 位)。
passwordstring密码(6~20 位)。
captchastring验证码,忽略大小写。预览模式且 isQuick=true 时可省略。
isQuickboolean仅在 IS_PREVIEW=true 时启用,无需验证码快速登录。

响应

json
{
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

后端会在 Redis 中缓存 token,键名形如 user:access:token:<userId>

常见错误:

  • 10003:验证码未匹配或缺失。
  • 10002:用户名/密码错误。
  • 11003:当前用户无启用的角色。

刷新 Token

  • MethodGET
  • Path/auth/refresh/token
  • 守卫JwtGuard
  • 说明:使用仍在有效期内的旧 token 调用,返回新的 accessToken

切换角色

  • MethodPOST
  • Path/auth/current-role/switch/:roleCode
  • 守卫JwtGuard

Params

参数说明
roleCode目标角色编码,例如 super_admin

若用户未拥有该角色,会返回 ERR_11005

登出

  • MethodPOST
  • Path/auth/logout
  • 守卫JwtGuard
  • 说明:清除 Redis 中的 token 缓存,客户端需删除本地 token。

修改密码

  • MethodPOST
  • Path/auth/password
  • 守卫JwtGuard + PreviewGuard(仅预览环境开放)

请求体

字段类型必填说明
oldPasswordstring旧密码。
newPasswordstring新密码。

成功后会自动调用 logout 使 token 失效,需要重新登录。

注册(预览环境)

  • MethodPOST
  • Path/auth/register
  • 守卫PreviewGuard
  • 说明:仅在 IS_PREVIEW=true 时允许注册新账号,正式环境请通过数据库脚本或后台管理创建。