取消游客模式,强制登录后使用

This commit is contained in:
2026-07-04 09:48:00 +08:00
parent 24630654a2
commit 323f50bfc0
10 changed files with 34 additions and 53 deletions
+16
View File
@@ -19,6 +19,11 @@ type CurrentUser struct {
Role models.RoleName
}
var publicPaths = map[string]struct{}{
"/api/auth/register": {},
"/api/auth/login": {},
}
func AuthMiddleware(cfg *config.Config, db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
@@ -41,6 +46,17 @@ func AuthMiddleware(cfg *config.Config, db *gorm.DB) gin.HandlerFunc {
}
}
if _, exists := c.Get("currentUser"); !exists {
if c.Request.Method == "OPTIONS" {
c.Next()
return
}
if _, ok := publicPaths[c.Request.URL.Path]; !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"success": false, "error": "访问令牌无效或已过期"})
return
}
}
c.Next()
}
}