一个基于 Rust 构建的高性能身份认证和用户管理系统,提供完整的 JWT 认证、用户管理、文件上传等功能。
- 慧添翼架构设计:https://huiwings.cn/arch
- 慧添翼小程序展示:https://huiwings.cn/show
AuthCore 是从内部项目提取并开源的身份认证核心系统,专注于提供安全、高性能的用户认证和管理服务。项目采用微服务架构,支持高并发访问,适用于各种规模的应用程序。
- 🔐 安全认证: 基于 JWT 的令牌认证系统
- 🚀 高性能: Rust 语言构建,内存安全且性能优异
- 🏗️ 微服务架构: 模块化设计,易于扩展和维护
- 🔧 开发友好: 完善的工具链和开发体验
- 📦 开箱即用: 提供完整的用户管理功能
- 用户认证: JWT 令牌生成、验证和管理
- 用户管理: 用户注册、登录、信息管理
- 权限控制: 基于角色的访问控制
- 会话管理: Redis 缓存支持的用户会话
- 安全加密: AES 加密和证书管理
- 微信集成: 微信小程序和公众号支持
- 文件上传: 又拍云文件存储集成
- 数据库管理: PostgreSQL 数据库支持
- 日志系统: 完整的日志记录和监控
- 测试框架: 内置测试工具和脚手架
- 语言: Rust (Edition 2021)
- Web 框架: Axum
- 数据库: PostgreSQL
- ORM: Diesel
- 缓存: Redis
- 认证: JWT (jsonwebtoken)
- 加密: AES, Ring, Rust-crypto
- 序列化: Serde
- 日志: Log4rs, Tracing
- HTTP 客户端: Reqwest
- 时间处理: Chrono, Time
- 包管理: Cargo
- 测试: Rust 内置测试框架
- 代码格式化: rustfmt
- 依赖管理: Workspace 模式
- 自动化: Dependabot
- Rust 1.70+
- PostgreSQL 13+
- Redis 6+
- Docker (可选)
- 克隆项目
git clone https://github.com/your-org/AuthCore.git
cd AuthCore
- 安装依赖
cargo build
- 配置环境变量
cp .env.example .env
# 编辑 .env 文件,配置数据库连接等信息
- 启动数据库
# 使用 Docker
docker-compose up -d db redis
# 或手动启动
# PostgreSQL 和 Redis 服务
- 运行数据库迁移
cd htyuc_models
diesel setup
diesel migration run
- 启动服务
# 启动用户中心服务
cargo run --bin htyuc
AuthCore 提供了多个独立的包,可以在你的 Rust 项目中作为依赖使用。
- htycommons: 通用工具库,包含分页、数据库操作、JWT 处理等
- htyuc_models: 用户管理相关的数据模型
- htyuc_remote: 远程服务客户端
- htyuc: 完整的用户管理服务
[dependencies]
# 通用工具库
htycommons = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htycommons" }
# 用户管理模型
htyuc_models = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc_models" }
# 远程服务客户端
htyuc_remote = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc_remote" }
# 完整的用户管理服务
htyuc = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc" }
[dependencies]
htycommons = { path = "../AuthCore/htycommons" }
htyuc_models = { path = "../AuthCore/htyuc_models" }
htyuc_remote = { path = "../AuthCore/htyuc_remote" }
htyuc = { path = "../AuthCore/htyuc" }
use htycommons::pagination::*;
use diesel::prelude::*;
// 在你的查询中使用分页
let (results, total_pages, total_count) = users
.paginate(Some(1))
.per_page(Some(10))
.load_and_count_pages(&mut conn)?;
use htyuc_models::models::*;
use htycommons::db::*;
// 创建用户
let new_user = NewUser {
username: "test_user".to_string(),
email: "[email protected]".to_string(),
// ... 其他字段
};
let user = insert_into(users::table)
.values(&new_user)
.get_result(&mut conn)?;
use htyuc_remote::remote_calls::*;
// 调用远程用户服务
let user_info = get_user_by_id("user_id", &client).await?;
如果你在 Cargo 工作区中使用,可以在根目录的 Cargo.toml
中配置:
[workspace.dependencies]
htycommons = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htycommons" }
htyuc_models = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc_models" }
htyuc_remote = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc_remote" }
htyuc = { git = "https://github.com/alchemy-studio/AuthCore.git", package = "htyuc" }
# 然后在各个成员包中使用
[package]
name = "my_project"
[dependencies]
htycommons = { workspace = true }
htyuc_models = { workspace = true }
# 或使用提供的脚本
./htyuc/start.sh
# 测试服务是否正常运行
curl http://localhost:3001/health
# 运行测试
cargo test
AuthCore/
├── htycommons/ # 通用工具库
│ ├── src/
│ │ ├── jwt.rs # JWT 认证
│ │ ├── db.rs # 数据库工具
│ │ ├── web.rs # Web 工具
│ │ ├── wx.rs # 微信集成
│ │ ├── redis_util.rs # Redis 工具
│ │ ├── cert.rs # 证书管理
│ │ ├── upyun.rs # 又拍云集成
│ │ └── ...
│ └── tests/ # 测试文件
├── htyuc/ # 用户中心服务
│ ├── src/
│ │ ├── main.rs # 服务入口
│ │ ├── lib.rs # 核心逻辑
│ │ └── ...
│ └── tests/ # 测试文件
├── htyuc_models/ # 数据模型
│ ├── migrations/ # 数据库迁移
│ └── src/
│ ├── models.rs # 数据模型
│ └── schema.rs # 数据库模式
├── htyuc_remote/ # 远程服务调用
├── certutil/ # 证书管理工具
├── upyun_tool/ # 又拍云上传工具
└── Cargo.toml # 工作空间配置
POST /api/v1/uc/register
Content-Type: application/json
{
"username": "[email protected]",
"password": "secure_password",
"nickname": "用户昵称"
}
POST /api/v1/uc/login
Content-Type: application/json
{
"username": "[email protected]",
"password": "secure_password"
}
GET /api/v1/uc/user
Authorization: Bearer <jwt_token>
POST /api/v1/uc/refresh
Authorization: Bearer <jwt_token>
POST /api/v1/uc/wx/login
Content-Type: application/json
{
"code": "wx_auth_code"
}
GET /api/v1/uc/wx/userinfo
Authorization: Bearer <jwt_token>
POST /api/v1/uc/upload
Authorization: Bearer <jwt_token>
Content-Type: multipart/form-data
file: <file_data>
创建 .env
文件并配置以下环境变量:
# 数据库配置
DATABASE_URL=postgres://username:password@localhost/authcore
UC_DB_URL=postgres://username:password@localhost/htyuc
WS_DB_URL=postgres://username:password@localhost/htyws
# Redis 配置
REDIS_HOST=localhost
REDIS_PORT=6379
# JWT 配置
JWT_KEY=your_jwt_secret_key_here
EXPIRATION_DAYS=30
# 服务配置
UC_PORT=3001
WS_PORT=3000
LOGGER_LEVEL=INFO
POOL_SIZE=20
# 微信配置
WEIXIN_APP_ID=your_weixin_app_id
WEIXIN_SECRET=your_weixin_secret
# 又拍云配置
UPYUN_OPERATOR=your_upyun_operator
UPYUN_PASSWORD=your_upyun_password
# 功能开关
SKIP_POST_LOGIN=false
SKIP_REGISTRATION=false
SKIP_WX_PUSH=false
- 创建数据库
CREATE DATABASE authcore;
CREATE DATABASE htyuc;
CREATE DATABASE htyws;
- 运行迁移
cd htyuc_models
diesel setup
diesel migration run
- 安装 Rust 工具链
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env
- 安装 PostgreSQL 和 Redis
# macOS
brew install postgresql redis
# Ubuntu
sudo apt-get install postgresql redis-server
- 安装 Diesel CLI
cargo install diesel_cli --no-default-features --features postgres
# 运行所有测试
cargo test
# 运行特定模块测试
cargo test --package htycommons
# 运行测试并显示输出
print_debug=true cargo test -- --nocapture
# 运行测试并限制线程数
cargo test -- --test-threads=1
# 格式化代码
cargo fmt
# 检查代码风格
cargo clippy
# 开发构建
cargo build
# 发布构建
cargo build --release
# 构建特定模块
cargo build --package htyuc
# 运行 htycommons 测试
cd htycommons
cargo test
# 运行 htyuc 测试
cd htyuc
cargo test
# 运行完整的集成测试
cargo test --test integration_tests
# 运行基准测试
cargo bench
- 构建镜像
docker build -t authcore .
- 运行容器
docker run -d \
--name authcore \
-p 3001:3001 \
-e DATABASE_URL=postgres://user:pass@host/db \
authcore
- 编译发布版本
cargo build --release
- 配置系统服务
# 创建 systemd 服务文件
sudo cp scripts/authcore.service /etc/systemd/system/
sudo systemctl enable authcore
sudo systemctl start authcore
我们欢迎所有形式的贡献!请查看 CONTRIBUTORS.md 了解项目贡献者历史。
- Fork 本项目
- 创建功能分支 (
git checkout -b feature/AmazingFeature
) - 提交更改 (
git commit -m 'Add some AmazingFeature'
) - 推送到分支 (
git push origin feature/AmazingFeature
) - 创建 Pull Request
- 🐛 Bug 修复
- ✨ 新功能
- 📝 文档改进
- 🧪 测试用例
- ⚡ 性能优化
- 🔒 安全增强
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
感谢所有为 AuthCore 项目做出贡献的开发者。特别感谢:
- liweinan (阿男): 项目架构师和主要开发者
- Buddy119: 核心协作开发者
- xiaolitongxue666: 功能开发者
- Moicen: 功能开发者
- beyoung: 贡献者
- twainyoung: 贡献者
详细贡献信息请查看 CONTRIBUTORS.md。
- 项目主页: GitHub Repository
- 问题反馈: Issues
- 讨论区: Discussions
⭐ 如果这个项目对你有帮助,请给我们一个星标!