1.实现思路:将登录用户的token加入黑名单
2.
//1.2 用户退出 exploreRouter.POST("/logout", sysCtrl.Logout)
3.loginController.go
//用户退出 func Logout(c *gin.Context) {logger := commonLog.InitLogger()sysUser := service.GetProfile1(c)fmt.Println("UUID=============================" + sysUser.Uuid)blacklist := service.NewBlacklist()logger.Println(constant.LOGINFO() + "用户名称为:"+sysUser.UserName +"; 退出成功!!!")// 将token添加到黑名单blacklist.AddToken(sysUser.Token) }
4.LoginOutService.go
package service
import (
"sync"
)// Blacklist是一个简单的结构体,用于管理token黑名单
type Blacklist struct {mu sync.Mutextokens map[string]bool
}// NewBlacklist创建一个新的Blacklist实例
func NewBlacklist() *Blacklist {return &Blacklist{tokens: make(map[string]bool),}
}// AddToken将指定的token添加到黑名单中
func (b *Blacklist) AddToken(tokenString string) {b.mu.Lock()b.tokens[tokenString] = trueb.mu.Unlock()
}// IsTokenBlacklisted检查指定的token是否在黑名单中
func (b *Blacklist) IsTokenBlacklisted(tokenString string) bool {b.mu.Lock()defer b.mu.Unlock()b2 := b.tokens[tokenString]return b2 && b.tokens[tokenString] == true
}