-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 黑名单
- Loading branch information
Showing
7 changed files
with
106 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/TensoRaws/NuxBT-Backend/internal/middleware/jwt" | ||
"github.com/TensoRaws/NuxBT-Backend/module/cache" | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// JWTBlacklist 检查JWT是否在黑名单中 | ||
func JWTBlacklist(redisClient *cache.Client, enableBlacklist bool) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// 从输入的 url 中查询 token 值 | ||
token := c.Query("token") | ||
if len(token) == 0 { | ||
// 从输入的表单中查询 token 值 | ||
token = c.PostForm("token") | ||
} | ||
|
||
if len(token) == 0 { | ||
util.AbortWithMsg(c, "JSON WEB TOKEN IS NULL") | ||
return | ||
} | ||
|
||
log.Logger.Info("Get token successfully") | ||
|
||
// 检查 Token 是否存在于 Redis 黑名单中 | ||
exists := redisClient.Exists(token).Val() | ||
if exists > 0 { | ||
log.Logger.Info("Token has been blacklisted") | ||
util.AbortWithMsg(c, "Token has been blacklisted") | ||
return | ||
} | ||
|
||
// 如果 Token 不在黑名单中,继续处理请求 | ||
c.Next() | ||
|
||
// 如果启用拉黑模式,处理请求拉黑 Token | ||
if enableBlacklist { | ||
err := redisClient.Set(token, "", jwt.GetJWTTokenExpiredDuration()).Err() | ||
if err != nil { | ||
log.Logger.Error("Error adding token to blacklist: " + err.Error()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package user | ||
|
||
import ( | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Logout 用户登出 (POST /logout) | ||
func Logout(c *gin.Context) { | ||
user, err := util.GetUserIDFromGinContext(c) | ||
if err != nil { | ||
util.AbortWithMsg(c, "Please login first") | ||
return | ||
} | ||
|
||
util.OKWithMsg(c, "Logout success") | ||
|
||
log.Logger.Info("Logout success: " + util.StructToString(user)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters