feat: 添加用户登录认证功能

- 新增登录页面(使用 shadcn-svelte 组件)
- 后端添加 JWT 认证 API (/api/auth/login, /api/auth/validate)
- 用户账号通过 server/config.yaml 配置
- 前端路由保护(未登录跳转登录页)
- 侧边栏显示当前用户信息
- 支持退出登录功能
This commit is contained in:
clz
2026-01-11 18:50:01 +08:00
parent 4884993d27
commit 829b3445bc
11 changed files with 639 additions and 16 deletions

View File

@@ -28,12 +28,26 @@ type Config struct {
MongoDatabase string // 数据库名称
MongoRawCollection string // 原始数据集合名称
MongoCleanedCollection string // 清洗后数据集合名称
// 认证配置
JWTSecret string // JWT 密钥
TokenExpiry int // Token 过期时间(小时)
Users []UserInfo // 用户列表
}
// UserInfo 用户信息
type UserInfo struct {
Username string `json:"username"`
Password string `json:"-"` // 不序列化密码
Name string `json:"name"`
Email string `json:"email"`
Role string `json:"role"`
}
// configFile YAML 配置文件结构
type configFile struct {
Version string `yaml:"version"`
Server struct {
Server struct {
Port int `yaml:"port"`
} `yaml:"server"`
Python struct {
@@ -56,6 +70,17 @@ type configFile struct {
Cleaned string `yaml:"cleaned"`
} `yaml:"collections"`
} `yaml:"mongodb"`
Auth struct {
JWTSecret string `yaml:"jwt_secret"`
TokenExpiry int `yaml:"token_expiry"` // 小时
Users []struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Name string `yaml:"name"`
Email string `yaml:"email"`
Role string `yaml:"role"`
} `yaml:"users"`
} `yaml:"auth"`
}
// Global 全局配置实例
@@ -186,6 +211,23 @@ func Load() {
if cfg.MongoDB.Collections.Cleaned != "" {
Global.MongoCleanedCollection = cfg.MongoDB.Collections.Cleaned
}
// 认证配置
if cfg.Auth.JWTSecret != "" {
Global.JWTSecret = cfg.Auth.JWTSecret
}
if cfg.Auth.TokenExpiry > 0 {
Global.TokenExpiry = cfg.Auth.TokenExpiry
}
// 用户列表
for _, u := range cfg.Auth.Users {
Global.Users = append(Global.Users, UserInfo{
Username: u.Username,
Password: u.Password,
Name: u.Name,
Email: u.Email,
Role: u.Role,
})
}
}
// 环境变量覆盖