Files
gofly-platform-services/zlt-web/react-web-next/src/utils/securityUtil.ts
T

172 lines
4.3 KiB
TypeScript

import RSAUtil from './rsaUtil';
/**
* 安全工具类 - 提供常用的安全操作
*/
export class SecurityUtil {
/**
* 安全登录请求处理
* @param loginData 登录数据
* @returns 处理后的登录数据(密码已加密)
*/
public static processLoginData(loginData: {
username: string;
password: string;
[key: string]: any;
}) {
const processedData = { ...loginData };
// 加密密码
if (processedData.password) {
const encryptedPassword = RSAUtil.encryptPassword(processedData.password);
if (encryptedPassword) {
processedData.password = encryptedPassword;
} else {
throw new Error('密码加密失败,请重试');
}
}
return processedData;
}
/**
* 安全表单数据处理
* @param formData 表单数据
* @param sensitiveFields 敏感字段列表
* @returns 处理后的表单数据
*/
public static processFormData<T extends Record<string, any>>(
formData: T,
sensitiveFields: (keyof T)[] = ['password', 'confirmPassword', 'oldPassword', 'newPassword']
): T {
return RSAUtil.encryptSensitiveFields(formData, sensitiveFields);
}
/**
* 验证密码强度
* @param password 密码
* @returns 密码强度等级和提示信息
*/
public static validatePasswordStrength(password: string): {
level: 'weak' | 'medium' | 'strong';
score: number;
suggestions: string[];
} {
const suggestions: string[] = [];
let score = 0;
// 长度检查
if (password.length >= 8) {
score += 25;
} else {
suggestions.push('密码长度至少8位');
}
// 包含小写字母
if (/[a-z]/.test(password)) {
score += 15;
} else {
suggestions.push('包含小写字母');
}
// 包含大写字母
if (/[A-Z]/.test(password)) {
score += 15;
} else {
suggestions.push('包含大写字母');
}
// 包含数字
if (/\d/.test(password)) {
score += 15;
} else {
suggestions.push('包含数字');
}
// 包含特殊字符
if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
score += 20;
} else {
suggestions.push('包含特殊字符(!@#$%^&*等)');
}
// 长度奖励
if (password.length >= 12) {
score += 10;
}
let level: 'weak' | 'medium' | 'strong';
if (score >= 70) {
level = 'strong';
} else if (score >= 50) {
level = 'medium';
} else {
level = 'weak';
}
return { level, score, suggestions };
}
/**
* 安全的输入过滤(基础XSS防护)
* @param input 用户输入
* @returns 过滤后的输入
*/
public static sanitizeInput(input: string): string {
if (!input) return '';
return input
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#x27;')
.replace(/\//g, '&#x2F;');
}
/**
* 生成随机盐值(用于额外安全性)
* @param length 盐值长度
* @returns 随机盐值
*/
public static generateSalt(length: number = 16): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let salt = '';
for (let i = 0; i < length; i++) {
salt += chars.charAt(Math.floor(Math.random() * chars.length));
}
return salt;
}
/**
* 敏感信息脱敏显示
* @param data 敏感数据
* @param type 数据类型
* @returns 脱敏后的数据
*/
public static maskSensitiveData(data: string, type: 'phone' | 'email' | 'idcard' | 'bankcard'): string {
if (!data) return '';
switch (type) {
case 'phone':
return data.length >= 11 ? data.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : data;
case 'email':
const [username, domain] = data.split('@');
if (username && domain) {
const maskedUsername = username.length > 2
? username[0] + '*'.repeat(username.length - 2) + username[username.length - 1]
: username;
return `${maskedUsername}@${domain}`;
}
return data;
case 'idcard':
return data.length >= 15 ? data.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2') : data;
case 'bankcard':
return data.length >= 16 ? data.replace(/(\d{4})\d{8}(\d{4})/, '$1********$2') : data;
default:
return data;
}
}
}
export default SecurityUtil;