310 lines
6.1 KiB
Markdown
310 lines
6.1 KiB
Markdown
|
|
# 文档在线管理系统
|
|||
|
|
|
|||
|
|
一个基于FastAPI的文档在线管理系统后端API,提供用户管理、文档管理和操作日志记录功能。
|
|||
|
|
|
|||
|
|
## 功能特性
|
|||
|
|
|
|||
|
|
### 用户管理模块
|
|||
|
|
- 用户注册/删除
|
|||
|
|
- 用户登录/登出
|
|||
|
|
- 用户信息维护
|
|||
|
|
- 用户操作记录
|
|||
|
|
- 会话管理机制
|
|||
|
|
|
|||
|
|
### 文档管理模块
|
|||
|
|
- 文档创建、查看、更新、删除
|
|||
|
|
- 文档权限控制(私有/公开)
|
|||
|
|
- 文档类型和大小管理
|
|||
|
|
|
|||
|
|
### 操作日志模块
|
|||
|
|
- 完整的用户操作记录
|
|||
|
|
- 管理员可查看所有操作日志
|
|||
|
|
- 用户可查看自己的操作日志
|
|||
|
|
|
|||
|
|
## 技术栈
|
|||
|
|
|
|||
|
|
- **后端框架**: FastAPI
|
|||
|
|
- **数据库**: SQLite (支持其他数据库)
|
|||
|
|
- **认证**: JWT Token
|
|||
|
|
- **ORM**: SQLAlchemy
|
|||
|
|
|
|||
|
|
## 安装和运行
|
|||
|
|
|
|||
|
|
### 1. 安装依赖
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
pip install -r requirements.txt
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 配置环境变量
|
|||
|
|
|
|||
|
|
复制环境变量示例文件:
|
|||
|
|
```bash
|
|||
|
|
cp .env.example .env
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
编辑 `.env` 文件,设置你的配置:
|
|||
|
|
```env
|
|||
|
|
SECRET_KEY=your-super-secret-jwt-key
|
|||
|
|
DATABASE_URL=sqlite:///./document_management.db
|
|||
|
|
DEBUG=true
|
|||
|
|
HOST=0.0.0.0
|
|||
|
|
PORT=8000
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 运行应用
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 方式1: 使用uvicorn直接运行
|
|||
|
|
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
|||
|
|
|
|||
|
|
# 方式2: 使用python运行
|
|||
|
|
python main.py
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 访问API文档
|
|||
|
|
|
|||
|
|
应用启动后,可以访问以下地址查看API文档:
|
|||
|
|
- Swagger UI: http://localhost:8000/docs
|
|||
|
|
- ReDoc: http://localhost:8000/redoc
|
|||
|
|
|
|||
|
|
## API接口说明
|
|||
|
|
|
|||
|
|
### 认证接口
|
|||
|
|
|
|||
|
|
#### 用户注册
|
|||
|
|
```http
|
|||
|
|
POST /auth/register
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"username": "testuser",
|
|||
|
|
"email": "test@example.com",
|
|||
|
|
"password": "password123",
|
|||
|
|
"full_name": "测试用户"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 用户登录
|
|||
|
|
```http
|
|||
|
|
POST /auth/login
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"username": "testuser",
|
|||
|
|
"password": "password123"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 用户登出
|
|||
|
|
```http
|
|||
|
|
POST /auth/logout
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 用户管理接口
|
|||
|
|
|
|||
|
|
#### 获取当前用户信息
|
|||
|
|
```http
|
|||
|
|
GET /users/me
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 更新用户信息
|
|||
|
|
```http
|
|||
|
|
PUT /users/me
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"email": "newemail@example.com",
|
|||
|
|
"full_name": "新姓名"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 获取所有用户(仅管理员)
|
|||
|
|
```http
|
|||
|
|
GET /users?skip=0&limit=100
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 删除用户(仅管理员)
|
|||
|
|
```http
|
|||
|
|
DELETE /users/{user_id}
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 文档管理接口
|
|||
|
|
|
|||
|
|
#### 创建文档
|
|||
|
|
```http
|
|||
|
|
POST /documents
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"title": "我的文档",
|
|||
|
|
"content": "文档内容",
|
|||
|
|
"description": "文档描述",
|
|||
|
|
"file_type": "txt",
|
|||
|
|
"is_public": false
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 获取文档列表
|
|||
|
|
```http
|
|||
|
|
GET /documents?skip=0&limit=100
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 获取文档详情
|
|||
|
|
```http
|
|||
|
|
GET /documents/{document_id}
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 更新文档
|
|||
|
|
```http
|
|||
|
|
PUT /documents/{document_id}
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
Content-Type: application/json
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
"title": "更新后的标题",
|
|||
|
|
"content": "更新后的内容"
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 删除文档
|
|||
|
|
```http
|
|||
|
|
DELETE /documents/{document_id}
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 操作日志接口
|
|||
|
|
|
|||
|
|
#### 获取我的操作日志
|
|||
|
|
```http
|
|||
|
|
GET /logs/my?skip=0&limit=100
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 获取所有操作日志(仅管理员)
|
|||
|
|
```http
|
|||
|
|
GET /logs?skip=0&limit=100
|
|||
|
|
Authorization: Bearer <token>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 数据库结构
|
|||
|
|
|
|||
|
|
### Users表(用户表)
|
|||
|
|
- id: 主键
|
|||
|
|
- username: 用户名(唯一)
|
|||
|
|
- email: 邮箱(唯一)
|
|||
|
|
- hashed_password: 加密密码
|
|||
|
|
- full_name: 全名
|
|||
|
|
- is_admin: 是否管理员
|
|||
|
|
- is_active: 是否激活
|
|||
|
|
- created_at: 创建时间
|
|||
|
|
- updated_at: 更新时间
|
|||
|
|
|
|||
|
|
### Documents表(文档表)
|
|||
|
|
- id: 主键
|
|||
|
|
- title: 文档标题
|
|||
|
|
- content: 文档内容
|
|||
|
|
- description: 文档描述
|
|||
|
|
- file_type: 文件类型
|
|||
|
|
- file_size: 文件大小
|
|||
|
|
- is_public: 是否公开
|
|||
|
|
- owner_id: 所有者ID(外键)
|
|||
|
|
- created_at: 创建时间
|
|||
|
|
- updated_at: 更新时间
|
|||
|
|
|
|||
|
|
### UserOperationLogs表(用户操作日志表)
|
|||
|
|
- id: 主键
|
|||
|
|
- user_id: 用户ID(外键)
|
|||
|
|
- operation: 操作类型
|
|||
|
|
- details: 操作详情
|
|||
|
|
- ip_address: IP地址
|
|||
|
|
- user_agent: 用户代理
|
|||
|
|
- created_at: 创建时间
|
|||
|
|
|
|||
|
|
## 安全特性
|
|||
|
|
|
|||
|
|
1. **JWT认证**: 基于Token的认证机制
|
|||
|
|
2. **权限控制**: 用户只能访问自己的数据
|
|||
|
|
3. **输入验证**: 使用Pydantic进行数据验证
|
|||
|
|
4. **CORS支持**: 支持跨域请求
|
|||
|
|
|
|||
|
|
### ⚠️ 重要安全说明
|
|||
|
|
|
|||
|
|
**简化认证模式**: 当前系统使用简化认证模式,密码以明文形式存储。
|
|||
|
|
- **仅适用于开发环境**: 此模式仅用于开发和测试目的
|
|||
|
|
- **生产环境警告**: 在生产环境中,强烈建议使用bcrypt等安全加密算法
|
|||
|
|
- **安全升级**: 如需升级到生产环境,请参考扩展功能建议中的安全升级部分
|
|||
|
|
|
|||
|
|
## 部署说明
|
|||
|
|
|
|||
|
|
### 本地部署
|
|||
|
|
```bash
|
|||
|
|
# 1. 克隆项目
|
|||
|
|
git clone <repository-url>
|
|||
|
|
cd web_security
|
|||
|
|
|
|||
|
|
# 2. 创建虚拟环境
|
|||
|
|
python -m venv .venv
|
|||
|
|
|
|||
|
|
# 3. 激活虚拟环境
|
|||
|
|
# Windows:
|
|||
|
|
.venv\\Scripts\\activate
|
|||
|
|
# Linux/Mac:
|
|||
|
|
source .venv/bin/activate
|
|||
|
|
|
|||
|
|
# 4. 安装依赖
|
|||
|
|
pip install -r requirements.txt
|
|||
|
|
|
|||
|
|
# 5. 运行应用
|
|||
|
|
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 生产环境部署
|
|||
|
|
|
|||
|
|
1. 修改 `.env` 文件中的配置:
|
|||
|
|
- 设置强密钥
|
|||
|
|
- 关闭DEBUG模式
|
|||
|
|
- 使用生产数据库
|
|||
|
|
|
|||
|
|
2. 使用生产级服务器(如uvicorn with gunicorn)
|
|||
|
|
|
|||
|
|
3. 配置反向代理(如Nginx)
|
|||
|
|
|
|||
|
|
## 开发说明
|
|||
|
|
|
|||
|
|
### 项目结构
|
|||
|
|
```
|
|||
|
|
web_security/
|
|||
|
|
├── main.py # 主应用文件
|
|||
|
|
├── database.py # 数据库配置
|
|||
|
|
├── models.py # 数据模型
|
|||
|
|
├── schemas.py # Pydantic模式
|
|||
|
|
├── auth.py # 认证模块
|
|||
|
|
├── requirements.txt # 依赖列表
|
|||
|
|
├── .env.example # 环境变量示例
|
|||
|
|
└── README.md # 项目说明
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 扩展功能建议
|
|||
|
|
|
|||
|
|
1. **文件上传**: 支持文档文件上传
|
|||
|
|
2. **文档分享**: 文档分享和协作功能
|
|||
|
|
3. **版本控制**: 文档版本管理
|
|||
|
|
4. **搜索功能**: 文档内容搜索
|
|||
|
|
5. **权限分级**: 更细粒度的权限控制
|
|||
|
|
6. **安全升级**: 升级到生产级安全认证
|
|||
|
|
- 安装bcrypt: `pip install bcrypt passlib[bcrypt]`
|
|||
|
|
- 更新auth.py中的密码处理函数
|
|||
|
|
- 重新创建数据库以加密现有密码
|
|||
|
|
|
|||
|
|
## 许可证
|
|||
|
|
|
|||
|
|
MIT License
|