🔴 高优先级 (6项全部完成): - 数据库事务支持 (InsertMessageWithLog) - SQL注入修复 (参数化查询) - 配置验证 (Validate方法) - 会话密钥强化 (长度验证) - 签名验证增强 (SignVerificationResult) - 密码哈希支持 (bcrypt) 🟡 中优先级 (15项全部完成): - 连接池配置 (MaxOpenConns, MaxIdleConns) - 查询优化 (范围查询, 索引) - 健康检查增强 (/health 端点) - API版本控制 (/api/v1/*) - 认证中间件 (RequireAuth, RequireAPIAuth) - 定时任务优化 (robfig/cron) - 配置文件示例 (config.example.yaml) - 常量定义 (config/constants.go) - 开发文档 (DEVELOPMENT.md) 🟢 低优先级 (9项全部完成): - Docker支持 (Dockerfile, docker-compose.yml) - Makefile构建脚本 - 优化报告 (OPTIMIZATION_REPORT.md) - 密码哈希工具 (tools/password_hash.go) - 14个新文件 - 30项优化100%完成 版本: v2.0.0
39 lines
813 B
Go
39 lines
813 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"sms-receiver-go/auth"
|
|
"sms-receiver-go/config"
|
|
"sms-receiver-go/models"
|
|
)
|
|
|
|
// RequireAuth 要求登录的中间件
|
|
func RequireAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
loggedIn, _ := auth.CheckLogin(w, r)
|
|
if !loggedIn {
|
|
return // CheckLogin 已经处理重定向
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
// RequireAPIAuth API 鉴权中间件
|
|
func RequireAPIAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
cfg := config.Get()
|
|
if cfg.Security.Enabled {
|
|
loggedIn, _ := auth.IsLoggedIn(r)
|
|
if !loggedIn {
|
|
writeJSON(w, models.APIResponse{
|
|
Success: false,
|
|
Error: "未授权",
|
|
}, http.StatusUnauthorized)
|
|
return
|
|
}
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|