Files
EchoChat/backend/go-service/router/router.go
bujinyuan b607434c57 fix(auth): 有状态 JWT + 统一响应格式,严格遵循设计方案
1. 有状态 JWT(Token 存 Redis):
   - 新增 token_store.go: Token 的 Redis 存取管理
     - echo:auth:token:{user_id} → Access Token (TTL = access_expire_min)
     - echo:auth:refresh:{user_id} → Refresh Token (TTL = refresh_expire_day)
   - 登录/注册时自动将 Token 存入 Redis(覆盖旧 Token,实现单设备登录)
   - JWT 中间件增加 Redis 有效性校验(TokenValidator 接口解耦)
   - 登出时从 Redis 删除 Token,使其立即失效
   - 刷新 Token 时校验 Redis 中的 Refresh Token

2. 统一成功响应为 "success" + 200:
   - 注册接口改用 ResponseOK(原 ResponseCreated/201)
   - 所有成功响应统一为 {"code": 0, "message": "success"}

3. API 文档同步更新:
   - frontend/auth.md: 登出说明改为 Redis 方案、注册响应统一
   - README.md: 移除 "created" 示例

已验证:登出后 Token 立即失效 ✓ 重新登录后新 Token 有效 ✓

Made-with: Cursor
2026-02-28 16:57:46 +08:00

40 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package router 主路由汇总入口
// 不含任何具体路由定义,仅调用各模块的 RegisterRoutes 函数
// 新增模块时在 Setup 函数中添加一行调用即可
package router
import (
"time"
"github.com/echochat/backend/app/auth"
"github.com/echochat/backend/app/provider"
"github.com/echochat/backend/pkg/middleware"
"github.com/echochat/backend/pkg/utils"
"github.com/gin-gonic/gin"
)
// Setup 注册所有路由
// 包含健康检查和各业务模块的路由注册
func Setup(engine *gin.Engine, app *provider.App) {
// 健康检查(不经过业务中间件)
engine.GET("/health", func(c *gin.Context) {
utils.ResponseOK(c, gin.H{
"status": "ok",
"service": "echochat",
"time": time.Now().Format("2006-01-02 15:04:05"),
})
})
// JWT 认证中间件实例(有状态 JWT通过 AuthService 校验 Redis
jwtAuth := middleware.JWTAuth(&app.Config.JWT, app.AuthService)
// --- 各模块路由注册 ---
auth.RegisterRoutes(engine, app.AuthController, app.AdminAuthController, jwtAuth)
// [未来] im.RegisterRoutes(engine, app.ImController, jwtAuth)
// [未来] meeting.RegisterRoutes(engine, app.MeetingController, jwtAuth)
// [未来] contact.RegisterRoutes(engine, app.ContactController, jwtAuth)
// [未来] notify.RegisterRoutes(engine, app.NotifyController, jwtAuth)
// [未来] admin.RegisterRoutes(engine, app.AdminController, jwtAuth)
}