feat(auth): 完整认证系统 — 注册/登录/JWT/路由(Task 4 + Task 5)

Task 4 — 认证服务层:
- pkg/utils/password.go: bcrypt 密码加密与校验
- pkg/utils/jwt.go: Access/Refresh Token 生成与解析(jwt/v5@v5.2.1,兼容 Go 1.23)
- app/dto/auth_dto.go: 认证相关请求/响应 DTO(含参数校验 binding tag)
- app/auth/service/auth_service.go: 核心业务逻辑(注册、登录、管理员登录、Token 刷新、个人信息、改密码)
- pkg/middleware/auth.go: JWT 认证中间件 + 角色权限检查中间件

Task 5 — Controller 与路由注册:
- app/auth/controller/auth_controller.go: 前台认证接口(7 个 API)
- app/auth/controller/admin_auth_controller.go: 后台管理认证接口
- app/auth/router.go: auth 模块路由定义(模块自包含)
- router/router.go: 主路由汇总入口(仅做调度,不含具体路由定义)
- cmd/server/main.go: 路由注册迁移到 router.Setup()

已验证全部接口:注册 ✓ 登录 ✓ 获取 profile ✓ 错误密码 ✓ 未认证拒绝 ✓ 非管理员拒绝 ✓

Made-with: Cursor
This commit is contained in:
bujinyuan
2026-02-28 16:42:28 +08:00
parent 4d3d486902
commit d975adaf2a
18 changed files with 1097 additions and 41 deletions

View File

@@ -3,6 +3,8 @@
package provider
import (
"github.com/echochat/backend/app/auth/controller"
"github.com/echochat/backend/app/auth/service"
"github.com/echochat/backend/config"
"github.com/echochat/backend/pkg/db"
"github.com/google/wire"
@@ -10,19 +12,32 @@ import (
"gorm.io/gorm"
)
// App 应用根容器,持有所有基础设施组件
// App 应用根容器,持有基础设施组件和各模块的 Controller/Service
type App struct {
Config *config.Config
DB *gorm.DB
Redis *redis.Client
Config *config.Config
DB *gorm.DB
Redis *redis.Client
AuthService *service.AuthService // Auth 认证服务
AuthController *controller.AuthController // 前台认证控制器
AdminAuthController *controller.AdminAuthController // 后台认证控制器
}
// NewApp 创建应用实例
func NewApp(cfg *config.Config, gormDB *gorm.DB, redisClient *redis.Client) *App {
func NewApp(
cfg *config.Config,
gormDB *gorm.DB,
redisClient *redis.Client,
authService *service.AuthService,
authCtrl *controller.AuthController,
adminAuthCtrl *controller.AdminAuthController,
) *App {
return &App{
Config: cfg,
DB: gormDB,
Redis: redisClient,
Config: cfg,
DB: gormDB,
Redis: redisClient,
AuthService: authService,
AuthController: authCtrl,
AdminAuthController: adminAuthCtrl,
}
}
@@ -36,10 +51,16 @@ func provideRedisConfig(cfg *config.Config) *config.RedisConfig {
return &cfg.Redis
}
// provideJWTConfig 从全局 Config 中提取 JWTConfig
func provideJWTConfig(cfg *config.Config) *config.JWTConfig {
return &cfg.JWT
}
// InfraSet 基础设施层 Provider Set
var InfraSet = wire.NewSet(
provideDBConfig,
provideRedisConfig,
provideJWTConfig,
db.NewPostgres,
db.NewRedis,
NewApp,

View File

@@ -4,6 +4,7 @@
package provider
import (
"github.com/echochat/backend/app/auth"
"github.com/echochat/backend/config"
"github.com/google/wire"
)
@@ -12,6 +13,7 @@ import (
func InitializeApp(cfg *config.Config) (*App, error) {
wire.Build(
InfraSet,
auth.AuthSet,
)
return nil, nil
}

View File

@@ -7,6 +7,9 @@
package provider
import (
"github.com/echochat/backend/app/auth/controller"
"github.com/echochat/backend/app/auth/dao"
"github.com/echochat/backend/app/auth/service"
"github.com/echochat/backend/config"
"github.com/echochat/backend/pkg/db"
)
@@ -25,6 +28,12 @@ func InitializeApp(cfg *config.Config) (*App, error) {
if err != nil {
return nil, err
}
app := NewApp(cfg, gormDB, client)
userDAO := dao.NewUserDAO(gormDB)
roleDAO := dao.NewRoleDAO(gormDB)
jwtConfig := provideJWTConfig(cfg)
authService := service.NewAuthService(userDAO, roleDAO, jwtConfig)
authController := controller.NewAuthController(authService)
adminAuthController := controller.NewAdminAuthController(authService)
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController)
return app, nil
}