- 修正 RustFS 端点地址为 192.168.1.154:9000(对应服务器实际部署) - 移除 config.go 中硬编码的 AccessKey/SecretKey 默认值,改为环境变量传入 - 移除 middleware/auth.go 中硬编码的 API_KEY_VALUE 常量,改为参数注入 - 新增 Config.AuthAPIKey 字段,通过 AUTH_API_KEY 环境变量配置 - 移除 login.html 页面上的密钥格式提示,防止信息泄露 - docker-compose.yml 补全 RustFS 连接所需的环境变量 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
32 lines
659 B
Go
32 lines
659 B
Go
package middleware
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
const API_KEY_HEADER = "X-API-Key"
|
||
|
||
// AuthMiddleware 验证API密钥的中间件
|
||
func AuthMiddleware(apiKey string) gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
// 从请求头中获取API密钥
|
||
key := c.GetHeader(API_KEY_HEADER)
|
||
|
||
// 验证密钥是否正确
|
||
if key != apiKey {
|
||
c.JSON(http.StatusUnauthorized, gin.H{
|
||
"code": http.StatusUnauthorized,
|
||
"message": "未授权:请在请求头中提供有效的API密钥",
|
||
"error": "Missing or invalid API key",
|
||
})
|
||
c.Abort()
|
||
return
|
||
}
|
||
|
||
// 密钥验证通过,继续处理请求
|
||
c.Next()
|
||
}
|
||
}
|