|
--- |
|
title: '身份认证与安全' |
|
description: '为 MCPHub 配置身份认证和安全设置' |
|
--- |
|
|
|
|
|
|
|
MCPHub 提供灵活的身份认证机制来保护您的 MCP 服务器管理平台。系统支持多种身份认证方法和基于角色的访问控制。 |
|
|
|
|
|
|
|
|
|
|
|
使用环境变量配置基础认证: |
|
|
|
```bash |
|
|
|
AUTH_USERNAME=admin |
|
AUTH_PASSWORD=your-secure-password |
|
|
|
|
|
JWT_SECRET=your-jwt-secret-key |
|
JWT_EXPIRES_IN=24h |
|
``` |
|
|
|
|
|
|
|
对于生产环境部署,启用基于数据库的用户管理: |
|
|
|
```json |
|
{ |
|
"auth": { |
|
"provider": "database", |
|
"database": { |
|
"url": "postgresql://user:pass@localhost:5432/mcphub", |
|
"userTable": "users" |
|
} |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
通过管理界面或 API 创建用户: |
|
|
|
```bash |
|
|
|
curl -X POST http: |
|
-H "Content-Type: application/json" \ |
|
-H "Authorization: Bearer $ADMIN_TOKEN" \ |
|
-d '{ |
|
"username": "newuser", |
|
"email": "user@example.com", |
|
"password": "securepassword", |
|
"role": "user" |
|
}' |
|
``` |
|
|
|
|
|
|
|
MCPHub 支持基于角色的访问控制: |
|
|
|
- **管理员**: 完整系统访问权限、用户管理、服务器配置 |
|
- **管理者**: 服务器管理、组管理、监控 |
|
- **用户**: 在分配组内的基本服务器访问权限 |
|
- **查看者**: 对分配资源的只读访问权限 |
|
|
|
|
|
|
|
|
|
|
|
```bash |
|
|
|
curl -X POST http: |
|
-H "Authorization: Bearer $TOKEN" \ |
|
-d '{"userId": "user123"}' |
|
``` |
|
|
|
|
|
|
|
配置组级别权限: |
|
|
|
```json |
|
{ |
|
"groupId": "dev-team", |
|
"permissions": { |
|
"servers": ["read", "write", "execute"], |
|
"tools": ["read", "execute"], |
|
"logs": ["read"], |
|
"config": ["read"] |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
```javascript |
|
|
|
const response = await fetch('/api/auth/login', { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
body: JSON.stringify({ |
|
username: 'your-username', |
|
password: 'your-password', |
|
}), |
|
}); |
|
|
|
const { token } = await response.json(); |
|
|
|
|
|
const protectedResponse = await fetch('/api/servers', { |
|
headers: { |
|
Authorization: `Bearer ${token}`, |
|
}, |
|
}); |
|
``` |
|
|
|
|
|
|
|
为系统集成生成 API 密钥: |
|
|
|
```bash |
|
|
|
curl -X POST http: |
|
-H "Authorization: Bearer $TOKEN" \ |
|
-d '{ |
|
"name": "Integration Key", |
|
"permissions": ["servers:read", "servers:write"], |
|
"expiresAt": "2024-12-31T23:59:59.000Z" |
|
}' |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
为生产环境启用 HTTPS: |
|
|
|
```nginx |
|
server { |
|
listen 443 ssl http2; |
|
server_name mcphub.example.com; |
|
|
|
ssl_certificate /path/to/certificate.crt; |
|
ssl_certificate_key /path/to/private.key; |
|
|
|
location / { |
|
proxy_pass http: |
|
proxy_set_header Host $host; |
|
proxy_set_header X-Real-IP $remote_addr; |
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
|
proxy_set_header X-Forwarded-Proto $scheme; |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
配置安全的会话设置: |
|
|
|
```javascript |
|
|
|
{ |
|
"session": { |
|
"secret": "your-session-secret", |
|
"secure": true, |
|
"httpOnly": true, |
|
"maxAge": 86400000, |
|
"sameSite": "strict" |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
实施 API 速率限制: |
|
|
|
```javascript |
|
{ |
|
"rateLimit": { |
|
"windowMs": 900000, |
|
"max": 100, |
|
"message": "请求过于频繁,请稍后再试", |
|
"standardHeaders": true, |
|
"legacyHeaders": false |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
为管理员帐户启用基于时间的一次性密码: |
|
|
|
```bash |
|
|
|
curl -X POST http: |
|
-H "Authorization: Bearer $TOKEN" \ |
|
-d '{ |
|
"type": "totp", |
|
"appName": "MCPHub" |
|
}' |
|
``` |
|
|
|
|
|
|
|
```javascript |
|
|
|
const loginResponse = await fetch('/api/auth/login', { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
body: JSON.stringify({ |
|
username: 'admin', |
|
password: 'password', |
|
mfaCode: '123456', // 来自认证器应用的 6 位数字 |
|
}), |
|
}); |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
跟踪所有认证和授权事件: |
|
|
|
```json |
|
{ |
|
"audit": { |
|
"enabled": true, |
|
"logLevel": "info", |
|
"events": [ |
|
"login", |
|
"logout", |
|
"password_change", |
|
"role_change", |
|
"permission_change", |
|
"server_access", |
|
"config_change" |
|
], |
|
"storage": { |
|
"type": "database", |
|
"retention": "90d" |
|
} |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
```bash |
|
|
|
curl -X GET "http://localhost:3000/api/audit/logs?startDate=2024-01-01&endDate=2024-01-31" \ |
|
-H "Authorization: Bearer $TOKEN" |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
```json |
|
{ |
|
"passwordPolicy": { |
|
"minLength": 12, |
|
"requireUppercase": true, |
|
"requireLowercase": true, |
|
"requireNumbers": true, |
|
"requireSpecialChars": true, |
|
"preventCommonPasswords": true, |
|
"preventReuse": 5, |
|
"maxAge": 7776000 |
|
} |
|
} |
|
``` |
|
|
|
|
|
|
|
|
|
|
|
1. **JWT 令牌过期** |
|
|
|
```bash |
|
|
|
curl -X GET http: |
|
-H "Authorization: Bearer $TOKEN" |
|
``` |
|
|
|
2. **权限被拒绝** |
|
|
|
```bash |
|
|
|
curl -X GET http: |
|
-H "Authorization: Bearer $TOKEN" |
|
``` |
|
|
|
3. **会话问题** |
|
- 清除浏览器 cookies |
|
- 检查会话配置 |
|
- 验证服务器时间同步 |
|
|
|
|
|
|
|
启用调试日志: |
|
|
|
```bash |
|
|
|
export DEBUG=auth:* |
|
export LOG_LEVEL=debug |
|
|
|
|
|
npm start |
|
``` |
|
|
|
|
|
|
|
1. **定期更新凭据**: 定期轮换 JWT 密钥和 API 密钥 |
|
2. **最小权限原则**: 只授予用户执行其任务所需的最小权限 |
|
3. **监控异常活动**: 设置警报以检测可疑的登录模式 |
|
4. **备份配置**: 定期备份认证配置和用户数据 |
|
5. **安全更新**: 保持 MCPHub 和依赖项的最新状态 |
|
|
|
更多安全配置选项,请参阅 [环境变量配置](/zh/configuration/environment-variables) 和 [Docker 设置](/zh/configuration/docker-setup) 文档。 |
|
|