202 lines
4.4 KiB
Markdown
202 lines
4.4 KiB
Markdown
# 前端RSA加密使用指南
|
||||
|
|
|
|||
|
|
## 概述
|
|||
|
|
|
|||
|
|
本项目提供了与后端RSAUtil对应的前端RSA加密解决方案,确保敏感数据在传输过程中的安全性。
|
|||
|
|
|
|||
|
|
## 核心文件
|
|||
|
|
|
|||
|
|
- `rsaUtil.ts` - RSA加密核心工具类
|
|||
|
|
- `securityUtil.ts` - 安全辅助工具类
|
|||
|
|
- `useSecurity.ts` - React Hook,提供安全操作
|
|||
|
|
- `SecureLoginForm` - 安全登录表单组件
|
|||
|
|
|
|||
|
|
## 基本使用
|
|||
|
|
|
|||
|
|
### 1. 密码加密
|
|||
|
|
|
|||
|
|
```typescript
|
|||
|
|
import RSAUtil from '@/utils/rsaUtil';
|
|||
|
|
|
|||
|
|
// 加密密码
|
|||
|
|
const encryptedPassword = RSAUtil.encryptPassword('user123456');
|
|||
|
|
|
|||
|
|
// 或使用便捷导入
|
|||
|
|
import { encryptPassword } from '@/utils/rsaUtil';
|
|||
|
|
const encrypted = encryptPassword('user123456');
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 登录表单处理
|
|||
|
|
|
|||
|
|
```typescript
|
|||
|
|
import { useSecurity } from '@/hooks/useSecurity';
|
|||
|
|
|
|||
|
|
const LoginPage = () => {
|
|||
|
|
const { secureLogin } = useSecurity();
|
|||
|
|
|
|||
|
|
const handleLogin = async (values) => {
|
|||
|
|
const secureData = await secureLogin(values);
|
|||
|
|
if (secureData) {
|
|||
|
|
// 发送到后端
|
|||
|
|
api.login(secureData);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 批量敏感字段加密
|
|||
|
|
|
|||
|
|
```typescript
|
|||
|
|
import SecurityUtil from '@/utils/securityUtil';
|
|||
|
|
|
|||
|
|
const userForm = {
|
|||
|
|
username: 'admin',
|
|||
|
|
password: '123456',
|
|||
|
|
email: 'admin@example.com'
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 只加密密码字段
|
|||
|
|
const secureForm = SecurityUtil.processFormData(userForm, ['password']);
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 使用安全登录组件
|
|||
|
|
|
|||
|
|
```typescript
|
|||
|
|
import SecureLoginForm from '@/components/SecureLoginForm';
|
|||
|
|
|
|||
|
|
const LoginPage = () => {
|
|||
|
|
const handleLogin = async (loginData) => {
|
|||
|
|
// loginData.password 已经被自动加密
|
|||
|
|
const response = await api.login(loginData);
|
|||
|
|
// 处理登录响应
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<SecureLoginForm
|
|||
|
|
onLogin={handleLogin}
|
|||
|
|
showPasswordStrength={true}
|
|||
|
|
/>
|
|||
|
|
);
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 安全最佳实践
|
|||
|
|
|
|||
|
|
### 1. 加密时机
|
|||
|
|
- ✅ 在发送请求前立即加密
|
|||
|
|
- ✅ 在表单提交时加密
|
|||
|
|
- ❌ 不要在组件状态中存储加密数据
|
|||
|
|
- ❌ 不要过早加密(影响用户体验)
|
|||
|
|
|
|||
|
|
### 2. 错误处理
|
|||
|
|
```typescript
|
|||
|
|
const encryptedData = RSAUtil.encrypt(sensitiveData);
|
|||
|
|
if (!encryptedData) {
|
|||
|
|
// 处理加密失败
|
|||
|
|
message.error('数据加密失败,请重试');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 加密前验证
|
|||
|
|
```typescript
|
|||
|
|
// 密码强度验证
|
|||
|
|
const strength = SecurityUtil.validatePasswordStrength(password);
|
|||
|
|
if (strength.level === 'weak') {
|
|||
|
|
// 提示用户增强密码
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 输入安全过滤
|
|||
|
|
const safeInput = SecurityUtil.sanitizeInput(userInput);
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 敏感数据脱敏
|
|||
|
|
```typescript
|
|||
|
|
// 显示时脱敏
|
|||
|
|
const maskedPhone = SecurityUtil.maskSensitiveData('13812345678', 'phone');
|
|||
|
|
// 结果: 138****5678
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 与后端对接
|
|||
|
|
|
|||
|
|
### 后端解密示例
|
|||
|
|
你的后端使用 `RSAUtil.decryptOne()` 方法解密:
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
// Java后端
|
|||
|
|
String rawPassword = RSAUtil.decryptOne(req.getPassword());
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 前后端数据流程
|
|||
|
|
1. 前端: 用户输入密码 `"123456"`
|
|||
|
|
2. 前端: RSA加密 → `"LBS9qzlDcBQ..."`(Base64 + URL编码)
|
|||
|
|
3. 后端: 接收加密数据
|
|||
|
|
4. 后端: `RSAUtil.decryptOne()` 解密 → `"123456"`
|
|||
|
|
|
|||
|
|
## 安全注意事项
|
|||
|
|
|
|||
|
|
### 1. 公钥管理
|
|||
|
|
- 公钥已硬编码在代码中(与后端保持一致)
|
|||
|
|
- 生产环境建议通过API动态获取公钥
|
|||
|
|
- 定期轮换密钥对
|
|||
|
|
|
|||
|
|
### 2. HTTPS传输
|
|||
|
|
```typescript
|
|||
|
|
// 确保API请求使用HTTPS
|
|||
|
|
const API_BASE_URL = process.env.NODE_ENV === 'production'
|
|||
|
|
? 'https://your-domain.com/api'
|
|||
|
|
: 'http://localhost:9900';
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 内存安全
|
|||
|
|
```typescript
|
|||
|
|
// 避免在控制台输出敏感信息
|
|||
|
|
if (process.env.NODE_ENV !== 'development') {
|
|||
|
|
console.log = () => {};
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 输入验证
|
|||
|
|
```typescript
|
|||
|
|
// 在加密前验证输入
|
|||
|
|
if (!password || password.trim() === '') {
|
|||
|
|
throw new Error('密码不能为空');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (password.length < 6) {
|
|||
|
|
throw new Error('密码长度不能少于6位');
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 常见问题
|
|||
|
|
|
|||
|
|
### Q: 加密失败怎么办?
|
|||
|
|
A: 检查输入数据是否为空,确认jsencrypt库已正确安装
|
|||
|
|
|
|||
|
|
### Q: 前后端加密解密不匹配?
|
|||
|
|
A: 确认公钥私钥对是否一致,检查Base64编码格式
|
|||
|
|
|
|||
|
|
### Q: 性能优化建议?
|
|||
|
|
A:
|
|||
|
|
- RSA加密较慢,只加密敏感字段
|
|||
|
|
- 考虑使用Web Worker处理大量数据加密
|
|||
|
|
- 合理使用缓存
|
|||
|
|
|
|||
|
|
## 测试
|
|||
|
|
|
|||
|
|
```typescript
|
|||
|
|
// 简单测试
|
|||
|
|
const testData = 'test123';
|
|||
|
|
const encrypted = RSAUtil.encrypt(testData);
|
|||
|
|
console.log('加密结果:', encrypted);
|
|||
|
|
console.log('是否已加密:', RSAUtil.isEncrypted(encrypted));
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 升级指南
|
|||
|
|
|
|||
|
|
如需更新加密算法或密钥:
|
|||
|
|
|
|||
|
|
1. 与后端协调更新时间
|
|||
|
|
2. 更新 `rsaUtil.ts` 中的公钥
|
|||
|
|
3. 测试前后端对接
|
|||
|
|
4. 发布更新
|