Files
smartmate/backend/respond/respond.go
LoveLosita 5038ec2fc5 Version:0.0.2.dev.260203
feat: implement redis-based logout and jwt middleware 🚀 feat: 实现基于 Redis 的登出机制与 JWT 中间件 🚀

Middleware Construction: Implemented JWTTokenAuth middleware for Gin, featuring structured claims parsing and active session validation. 🛡️
中间件构建:为 Gin 框架实现了 JWTTokenAuth 中间件,支持结构化 Claims 解析与活跃会话验证。🛡️

Redis Integration: Introduced Redis for high-performance state management. Integrated CacheDAO into the Dependency Injection (DI) chain. 
Redis 引入:引入 Redis 进行高性能状态管理。将 CacheDAO 成功集成至依赖注入 (DI) 调用链中。

Secure Logout Module: Developed the logout functional module using a Redis Blacklist mechanism. 🔐
安全登出模块:开发了基于 Redis 黑名单 机制的登出功能模块。🔐

Marked invalidated tokens by storing jti (JWT ID) in Redis with automatic TTL expiration.
通过在 Redis 中存储 jti(JWT 唯一标识)并设置自动 TTL 过期,实现 Token 的主动失效。

Added blacklist checkpoints in both AuthMiddleware and RefreshToken logic to prevent session resurrection.
在认证中间件与 Token 刷新逻辑中同步增设黑名单检查点,杜绝登出后的“死灰复燃”。

Architecture Refinement: Upgraded ValidateRefreshToken and Service-layer handlers to use type-safe struct assertions instead of raw MapClaims. 🏗️
架构精进:升级了 ValidateRefreshToken 与 Service 层处理器,改用类型安全的结构体断言取代原始的 MapClaims,提升了代码健壮性。🏗️
2026-02-03 16:53:16 +08:00

127 lines
2.6 KiB
Go

// Package respond 响应处理
// 统一API响应格式和处理逻辑
package respond
type Response struct { //响应结构体
Status string `json:"status"`
Info string `json:"info"`
}
type FinalResponse struct { //最终响应结构体
Status string `json:"status"`
Info string `json:"info"`
Data interface{} `json:"data"`
}
// 实现error接口
func (r Response) Error() string { // 实现 error 接口
return r.Info
}
func OKWithData(response Response, data interface{}) FinalResponse { //传入一个响应结构体和数据,返回一个最终响应结构体
var finalResponse FinalResponse
finalResponse.Status = response.Status
finalResponse.Info = response.Info
finalResponse.Data = data
return finalResponse
}
func InternalError(err error) Response { //服务器错误
return Response{
Status: "500",
Info: err.Error(),
}
}
var ( //请求相关的响应
Ok = Response{ //正常
Status: "10000",
Info: "success",
}
WrongName = Response{ //用户名错误
Status: "40001",
Info: "wrong username",
}
WrongPwd = Response{ //密码错误
Status: "40002",
Info: "wrong password",
}
InvalidName = Response{ //用户名无效
Status: "40003",
Info: "the username already exists",
}
MissingParam = Response{ //缺少参数
Status: "40004",
Info: "missing param",
}
WrongParamType = Response{ //参数错误
Status: "40005",
Info: "wrong param type",
}
ParamTooLong = Response{ //参数过长
Status: "40006",
Info: "param too long",
}
WrongUsernameOrPwd = Response{ //用户名或密码错误
Status: "40007",
Info: "wrong username or password",
}
WrongGender = Response{ //性别错误
Status: "40008",
Info: "wrong gender",
}
MissingToken = Response{ //缺少token
Status: "40009",
Info: "missing token",
}
InvalidTokenSingingMethod = Response{ //jwt token签名方法无效
Status: "40010",
Info: "invalid signing method",
}
InvalidToken = Response{ //无效token
Status: "40011",
Info: "invalid token",
}
InvalidClaims = Response{ //无效声明
Status: "40012",
Info: "invalid claims",
}
WrongUserID = Response{ //用户ID错误
Status: "40013",
Info: "wrong userid",
}
ErrUnauthorized = Response{ //未授权,没有权限
Status: "40014",
Info: "unauthorized",
}
InvalidRefreshToken = Response{ //刷新令牌无效
Status: "40015",
Info: "invalid refresh token",
}
WrongTokenType = Response{ //无效令牌类型
Status: "40016",
Info: "wrong token type",
}
UserLoggedOut = Response{ //用户已登出
Status: "40017",
Info: "user logged out",
}
)