修复登录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)
|
user := service.GetUser(req_data.User, req_data.Password)
|
||||||
if user.ID != 0 {
|
if user.ID != 0 {
|
||||||
|
redis_token := worker.GetRedis("user_" + user.Name)
|
||||||
|
if redis_token == "" {
|
||||||
// 生成 JWT 令牌
|
// 生成 JWT 令牌
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"username": user.Name,
|
"username": user.Name,
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 1分钟后过期
|
"exp": time.Now().Add(time.Hour * 10).Unix(), // 令牌过期时间, 10小时后过期
|
||||||
})
|
})
|
||||||
tokenString, err = token.SignedString(signingKey)
|
tokenString, err = token.SignedString(signingKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
c.JSON(200, gin.H{"error": err.Error(), "code": 1, "message": "error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
worker.SetRedisWithExpire("user_"+user.Name, tokenString, time.Hour*10) // 将用户信息存入
|
||||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||||
|
worker.SetHash(tokenString, int(user.ID), user.Name, user.Email) // 将用户信息存入
|
||||||
|
} else {
|
||||||
|
tokenString = redis_token
|
||||||
|
}
|
||||||
// 返回令牌
|
// 返回令牌
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
data["id"]= user.ID
|
data["id"] = user.ID
|
||||||
data["username"]=user.Name
|
data["username"] = user.Name
|
||||||
data["email"]=user.Email
|
data["email"] = user.Email
|
||||||
data["token"] =tokenString
|
data["token"] = tokenString
|
||||||
c.JSON(200, gin.H{"code": 0, "message": "success", "data":data})
|
c.JSON(200, gin.H{"code": 0, "message": "success", "data": data})
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, gin.H{"error": "user not found", "code": 1, "message": "error"})
|
c.JSON(200, gin.H{"error": "user not found", "code": 1, "message": "error"})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package worker
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
import (
|
import (
|
||||||
|
|
@ -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
|
// 设置redis
|
||||||
func SetRedisWithExpire(key string, value string, expire time.Duration) { // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等
|
func SetRedisWithExpire(key string, value string, expire time.Duration) { // 设置键值对, 0 表示不设置过期时间, 如果需要设置过期时间, 可以设置为 time.Second * 10 等
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue