Skip to content

认证系统

脚手架内置完整的 JWT 认证系统。

功能

  • JWT Token,过期时间可配置(默认 24 小时)
  • bcrypt 密码哈希
  • 角色权限(admin / user)
  • 重新登录刷新 Token

API 接口

方法路径说明
POST/api/auth/login用户名 + 密码登录
POST/api/auth/register注册新用户
GET/api/user/profile获取当前用户信息(需认证)

配置

config.yaml 中:

yaml
jwt:
    secret: your-secret-key    # 生产环境务必修改!
    expire: 24                 # Token 过期时间(小时)
    issuer: your-project-name

保护路由

为路由组添加 JWTAuth 中间件:

go
authorized := api.Group("/")
authorized.Use(middleware.JWTAuth(&cfg.JWT))
{
    authorized.GET("/user/profile", profileHandler())
}