feat(gateway): 新增消息网关及apikey功能
Quality check / Web UI (push) Successful in 9m16s

This commit is contained in:
mei
2026-08-30 21:09:26 +08:00
parent 360169ee64
commit 0d89b7fe8f
18 changed files with 363 additions and 20 deletions
+21
View File
@@ -210,6 +210,27 @@ func HandleUpdateNotifyConfig(c *gin.Context) {
// AuthMiddleware 认证中间件,同时把当前用户放进 context 供后续 handler 复用
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
apiKey := c.GetHeader("X-API-Key")
if apiKey == "" {
auth := c.GetHeader("Authorization")
const prefix = "Bearer "
if len(auth) > len(prefix) && auth[:len(prefix)] == prefix {
apiKey = auth[len(prefix):]
}
}
if apiKey != "" {
gormDB, err := db.GetGormDB()
if err == nil {
var user model.User
if gormDB.Where("api_key = ?", apiKey).First(&user).Error == nil {
c.Set(contextKey, &user)
c.Next()
return
}
}
response.AbortWith(c, http.StatusUnauthorized, "API Key 无效")
return
}
session := sessions.Default(c)
uid, ok := session.Get("uid").(uint)
if !ok {