Files
JoyD/WeCom/README.md
2026-03-11 13:18:05 +08:00

137 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# WeCom Server
企业微信验证服务器 - 基于 Rust 和 Axum 框架实现的 Web 服务器,用于企业微信应用的服务器验证。
## 功能特性
- 基于 Axum 0.7 框架的高性能 Web 服务器
- 企业微信服务器验证接口
- SHA1 签名验证
- 异步处理(基于 Tokio
## 技术栈
- **Rust** 2021 Edition
- **Axum** 0.7 - Web 框架
- **Tokio** 1.0 - 异步运行时
- **Serde** 1.0 - 序列化/反序列化
- **SHA1** 0.10 - 哈希算法
- **Tracing** 0.1 - 日志追踪
## 项目结构
```
WeCom/
├── Cargo.toml # 项目配置和依赖
├── README.md # 项目说明文档
└── src/
└── main.rs # 主程序入口
```
## 安装
确保已安装 Rust 工具链(建议使用 rustup
```bash
rustup update stable
```
克隆项目后,进入项目目录:
```bash
cd d:\Projects\trunk\JoyD\WeCom
```
## 运行
开发模式运行:
```bash
cargo run
```
生产构建:
```bash
cargo build --release
```
运行生产版本:
```bash
.\target\release\wecom_server.exe
```
## 配置
`src/main.rs` 中修改以下配置:
```rust
const WECOM_TOKEN: &str = "mytoken123456"; // 企业微信后台填写的 Token
```
服务器默认监听地址:`127.0.0.1:8000`
## API 接口
### GET /wecom
企业微信服务器验证接口。
**请求参数:**
- `msg_signature` - 消息签名
- `timestamp` - 时间戳
- `nonce` - 随机数
- `echostr` - 随机字符串
**响应:**
- 验证成功:返回 `echostr` 原值
- 验证失败:返回 `"invalid"`
## 验证逻辑
1. 接收企业微信发送的验证请求
2.`token``timestamp``nonce``echostr` 四个参数进行字典序排序
3. 将排序后的字符串拼接成一个字符串
4. 对拼接后的字符串进行 SHA1 哈希
5. 将哈希值与 `msg_signature` 进行比较
6. 验证成功返回 `echostr`,失败返回 `"invalid"`
## 开发
代码检查:
```bash
cargo check
```
代码格式化:
```bash
cargo fmt
```
运行 linter
```bash
cargo clippy
```
运行测试:
```bash
cargo test
```
## 注意事项
- 确保 Token 与企业微信后台配置一致
- 服务器需要能够被企业微信服务器访问(如需外网访问,请配置端口转发或使用公网 IP
- 生产环境建议使用环境变量管理敏感配置
## 许可证
本项目仅供学习和参考使用。