✨ 新增功能: - 全局路由守卫模块 (route-guard.js) - 多层次权限验证 (登录状态+Token存在+Token过期检查) - 自动拦截机制 (页面级+跳转级+HTTP级+应用级) - 401响应自动处理和跳转 - 路由守卫测试页面 🔧 技术实现: - checkPageAccess() 全局页面权限检查 - guardedNavigate/guardedSwitchTab 带权限检查的跳转 - handleUnauthorized() HTTP拦截器401处理 - 公开页面配置管理 - 优雅的错误提示和跳转逻辑 📱 页面集成: - App.vue 初始化路由守卫 - profile/workbench 页面添加权限检查 - index 页面优化跳转逻辑 - 新增测试页面验证功能 📚 文档完善: - 移动端路由守卫使用指南 - 详细的变更记录和技术说明 - 最佳实践和注意事项 🎯 核心效果: 用户登录失效或未登录时,访问任何需要权限的页面都自动跳转到登录页,提升安全性和用户体验。
152 lines
4.8 KiB
JavaScript
152 lines
4.8 KiB
JavaScript
// @ts-nocheck
|
|
import CryptoJS from 'crypto-js';
|
|
import JSEncrypt from 'jsencrypt';
|
|
|
|
// 配置信息
|
|
const ENCRYPT_CONFIG = {
|
|
// RSA公钥 - 与后端保持一致 (更新为正确版本,包含ijT7h)
|
|
RSA_PUBLIC_KEY:
|
|
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==',
|
|
CLIENT_ID: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
|
TENANT_ID: '000000',
|
|
};
|
|
|
|
/**
|
|
* 生成随机AES密钥
|
|
* @param {number} length 密钥长度,默认32位
|
|
* @returns {string} AES密钥
|
|
*/
|
|
export function generateRandomAESKey(length = 32) {
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
let result = '';
|
|
for (let i = 0; i < length; i++) {
|
|
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* AES加密数据
|
|
* @param {string} data 待加密的数据
|
|
* @param {string} key AES密钥
|
|
* @returns {string} 加密后的数据
|
|
*/
|
|
export function encryptWithAES(data, key) {
|
|
const keyUtf8 = CryptoJS.enc.Utf8.parse(key);
|
|
const encrypted = CryptoJS.AES.encrypt(data, keyUtf8, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7,
|
|
});
|
|
return encrypted.toString();
|
|
}
|
|
|
|
/**
|
|
* RSA加密AES密钥 - 使用JSEncrypt
|
|
* @param {string} aesKey AES密钥
|
|
* @returns {string|null} RSA加密后的AES密钥
|
|
*/
|
|
export function encryptAESKeyWithRSA(aesKey) {
|
|
try {
|
|
console.log('🔧 开始RSA加密,AES密钥:', aesKey);
|
|
|
|
// 构造完整的PEM格式RSA公钥
|
|
const pemKey = `-----BEGIN PUBLIC KEY-----
|
|
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqEfJl9KxCi6s3Fpjs234+uCvG
|
|
SIDnR585F0jlTmWxbv0yYWe6xQDfnmhpJdwuG7nHEHfxIxLI/Yq+4fejDAMCAwEA
|
|
AQ==
|
|
-----END PUBLIC KEY-----`;
|
|
|
|
console.log('🔧 RSA公钥PEM格式已构造');
|
|
|
|
// 创建JSEncrypt实例
|
|
const encrypt = new JSEncrypt();
|
|
|
|
// 设置公钥
|
|
const keySetResult = encrypt.setPublicKey(pemKey);
|
|
console.log('🔧 公钥设置结果:', keySetResult);
|
|
|
|
// 将AES密钥转为Base64(与custom-login-script.js保持一致)
|
|
const aesKeyBase64 = btoa(unescape(encodeURIComponent(aesKey)));
|
|
console.log('🔧 AES密钥Base64:', aesKeyBase64);
|
|
|
|
// RSA加密
|
|
const encrypted = encrypt.encrypt(aesKeyBase64);
|
|
|
|
console.log('🔧 RSA加密结果:', encrypted);
|
|
|
|
if (!encrypted) {
|
|
throw new Error('JSEncrypt RSA加密返回null,可能是公钥格式错误或加密失败');
|
|
}
|
|
|
|
console.log('✅ RSA加密成功,结果长度:', encrypted.length);
|
|
return encrypted;
|
|
} catch (error) {
|
|
console.error('❌ RSA加密失败:', error.message);
|
|
console.error('❌ 错误堆栈:', error.stack);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 登录数据加密处理
|
|
* @param {Object} loginData 登录数据对象
|
|
* @returns {Object} 包含加密数据和请求头的对象
|
|
*/
|
|
export function encryptLoginData(loginData) {
|
|
try {
|
|
// 1. 生成AES密钥
|
|
const aesKey = generateRandomAESKey(32);
|
|
console.log('🔑 生成AES密钥:', aesKey);
|
|
|
|
// 2. 准备完整的登录数据
|
|
const fullLoginData = {
|
|
tenantId: loginData.tenantId || ENCRYPT_CONFIG.TENANT_ID,
|
|
username: loginData.username,
|
|
password: loginData.password,
|
|
clientId: loginData.clientId || ENCRYPT_CONFIG.CLIENT_ID,
|
|
grantType: loginData.grantType || 'password',
|
|
};
|
|
|
|
console.log('📋 登录数据:', JSON.stringify(fullLoginData, null, 2));
|
|
|
|
// 3. AES加密登录数据
|
|
const encryptedData = encryptWithAES(JSON.stringify(fullLoginData), aesKey);
|
|
console.log('🔐 AES加密后数据长度:', encryptedData.length);
|
|
|
|
// 4. RSA加密AES密钥
|
|
const encryptedKey = encryptAESKeyWithRSA(aesKey);
|
|
if (!encryptedKey) {
|
|
throw new Error('RSA加密AES密钥失败');
|
|
}
|
|
console.log('🔑 RSA加密后密钥长度:', encryptedKey.length);
|
|
|
|
// 5. 返回加密结果和请求头
|
|
return {
|
|
success: true,
|
|
encryptedData: encryptedData,
|
|
headers: {
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
clientid: ENCRYPT_CONFIG.CLIENT_ID,
|
|
'encrypt-key': encryptedKey,
|
|
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
isToken: 'false',
|
|
isEncrypt: 'true',
|
|
repeatSubmit: 'false',
|
|
},
|
|
};
|
|
} catch (error) {
|
|
console.error('登录数据加密失败:', error);
|
|
return {
|
|
success: false,
|
|
error: error.message,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取加密配置
|
|
*/
|
|
export function getEncryptConfig() {
|
|
return ENCRYPT_CONFIG;
|
|
}
|