修复登录token重复生成导致redis token不断增长的bug。
This commit is contained in:
parent
a34892b8c6
commit
8a697e8bfc
|
|
@ -36,25 +36,33 @@ func loginHandler(c *gin.Context) {
|
|||
}
|
||||
user := service.GetUser(req_data.User, req_data.Password)
|
||||
if user.ID != 0 {
|
||||
// 生成 JWT 令牌
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"username": user.Name,
|
||||
"id": user.ID,
|
||||
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
||||
})
|
||||
tokenString, err = token.SignedString(signingKey)
|
||||
if err != nil {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
||||
return
|
||||
redis_token := worker.GetRedis("user_" + user.Name)
|
||||
if redis_token == "" {
|
||||
// 生成 JWT 令牌
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"username": user.Name,
|
||||
"id": user.ID,
|
||||
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 10小时后过期
|
||||
})
|
||||
tokenString, err = token.SignedString(signingKey)
|
||||
if err != nil {
|
||||
c.JSON(200, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
||||
return
|
||||
}
|
||||
|
||||
worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入
|
||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||
worker.SetHash(tokenString, int(user.ID), user.Name, user.Email) // 将用户信息存入
|
||||
} else {
|
||||
tokenString = redis_token
|
||||
}
|
||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||
// 返回令牌
|
||||
data := make(map[string]interface{})
|
||||
data["id"]= user.ID
|
||||
data["username"]=user.Name
|
||||
data["email"]=user.Email
|
||||
data["token"] =tokenString
|
||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data":data})
|
||||
data["id"] = user.ID
|
||||
data["username"] = user.Name
|
||||
data["email"] = user.Email
|
||||
data["token"] = tokenString
|
||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data})
|
||||
} else {
|
||||
c.JSON(200, gin.H{"error": "user not found", "code": 1, "message": "error"})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package worker
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
import (
|
||||
|
|
@ -17,7 +18,7 @@ func InitRedis() {
|
|||
// 连接redis
|
||||
redisClient = redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1:6379", // Redis 服务器地址
|
||||
Password: "lj502138", // 如果 Redis 设置了密码
|
||||
Password: "lj502138", // 如果 Redis 设置了密码
|
||||
DB: 2, // 使用的数据库编号
|
||||
})
|
||||
|
||||
|
|
@ -57,6 +58,29 @@ func SetRedis(key string, value string) {
|
|||
}
|
||||
}
|
||||
|
||||
// 设置hash
|
||||
func SetHash(key string, id int, name, email string) {
|
||||
ctx := context.Background() // 创建一个上下文
|
||||
// 设置哈希表的字段值, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等
|
||||
err := redisClient.HSet(ctx, key, "id", id, "name", name, "email", email, time.Hour*12).Err()
|
||||
if err != nil {
|
||||
log.Fatalf("Error setting key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取hash
|
||||
func GetHash(key string) RUser {
|
||||
ctx := context.Background()
|
||||
val, err := redisClient.HGetAll(ctx, key).Result() // 从 Redis 读取哈希表, 如果键不存在则返回空字符串, 如果出现错误则返回错误
|
||||
if err != nil {
|
||||
log.Fatalf("Error getting key: %v", err)
|
||||
}
|
||||
id, _ := strconv.Atoi(val["id"])
|
||||
name, _ := val["name"]
|
||||
email, _ := val["email"]
|
||||
return RUser{ID: id, Name: name, Email: email}
|
||||
}
|
||||
|
||||
// 设置redis
|
||||
func SetRedisWithExpire(key string, value string, expire time.Duration) { // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等
|
||||
ctx := context.Background()
|
||||
|
|
|
|||
Loading…
Reference in New Issue