feat: 引入依赖注入模式
- 创建Repository接口定义(UserRepository、ProfileRepository、TextureRepository等) - 创建Repository接口实现 - 创建依赖注入容器(container.Container) - 改造Handler层使用依赖注入(AuthHandler、UserHandler、TextureHandler) - 创建新的路由注册方式(RegisterRoutesWithDI) - 提供main.go示例文件展示如何使用依赖注入 同时包含之前的安全修复: - CORS配置安全加固 - 头像URL验证安全修复 - JWT algorithm confusion漏洞修复 - Recovery中间件增强 - 敏感错误信息泄露修复 - 类型断言安全修复
This commit is contained in:
@@ -55,6 +55,10 @@ func (j *JWTService) GenerateToken(userID int64, username, role string) (string,
|
||||
// ValidateToken 验证JWT Token
|
||||
func (j *JWTService) ValidateToken(tokenString string) (*Claims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
// 验证签名算法,防止algorithm confusion攻击
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, errors.New("不支持的签名算法")
|
||||
}
|
||||
return []byte(j.secretKey), nil
|
||||
})
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
@@ -22,6 +23,7 @@ type Config struct {
|
||||
Log LogConfig `mapstructure:"log"`
|
||||
Upload UploadConfig `mapstructure:"upload"`
|
||||
Email EmailConfig `mapstructure:"email"`
|
||||
Security SecurityConfig `mapstructure:"security"`
|
||||
}
|
||||
|
||||
// ServerConfig 服务器配置
|
||||
@@ -107,6 +109,12 @@ type EmailConfig struct {
|
||||
FromName string `mapstructure:"from_name"`
|
||||
}
|
||||
|
||||
// SecurityConfig 安全配置
|
||||
type SecurityConfig struct {
|
||||
AllowedOrigins []string `mapstructure:"allowed_origins"` // 允许的CORS来源
|
||||
AllowedDomains []string `mapstructure:"allowed_domains"` // 允许的头像/材质URL域名
|
||||
}
|
||||
|
||||
// Load 加载配置 - 完全从环境变量加载,不依赖YAML文件
|
||||
func Load() (*Config, error) {
|
||||
// 加载.env文件(如果存在)
|
||||
@@ -160,7 +168,7 @@ func setDefaults() {
|
||||
|
||||
// RustFS默认配置
|
||||
viper.SetDefault("rustfs.endpoint", "127.0.0.1:9000")
|
||||
viper.SetDefault("rustfs.public_url", "") // 为空时使用 endpoint 构建 URL
|
||||
viper.SetDefault("rustfs.public_url", "") // 为空时使用 endpoint 构建 URL
|
||||
viper.SetDefault("rustfs.use_ssl", false)
|
||||
|
||||
// JWT默认配置
|
||||
@@ -188,6 +196,10 @@ func setDefaults() {
|
||||
// 邮件默认配置
|
||||
viper.SetDefault("email.enabled", false)
|
||||
viper.SetDefault("email.smtp_port", 587)
|
||||
|
||||
// 安全默认配置
|
||||
viper.SetDefault("security.allowed_origins", []string{"*"})
|
||||
viper.SetDefault("security.allowed_domains", []string{"localhost", "127.0.0.1"})
|
||||
}
|
||||
|
||||
// setupEnvMappings 设置环境变量映射
|
||||
@@ -310,6 +322,15 @@ func overrideFromEnv(config *Config) {
|
||||
if env := os.Getenv("ENVIRONMENT"); env != "" {
|
||||
config.Environment = env
|
||||
}
|
||||
|
||||
// 处理安全配置
|
||||
if allowedOrigins := os.Getenv("SECURITY_ALLOWED_ORIGINS"); allowedOrigins != "" {
|
||||
config.Security.AllowedOrigins = strings.Split(allowedOrigins, ",")
|
||||
}
|
||||
|
||||
if allowedDomains := os.Getenv("SECURITY_ALLOWED_DOMAINS"); allowedDomains != "" {
|
||||
config.Security.AllowedDomains = strings.Split(allowedDomains, ",")
|
||||
}
|
||||
}
|
||||
|
||||
// IsTestEnvironment 判断是否为测试环境
|
||||
|
||||
@@ -62,6 +62,3 @@ func MustGetRustFSConfig() *RustFSConfig {
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user