add 新增响应解密私钥 ;
add 新增 crypto#decryptBase64 Base64解码方法 ; update 更新响应拦截器增加响应解密逻辑 ;
This commit is contained in:
@ -30,6 +30,13 @@ export const encryptBase64 = (str: CryptoJS.lib.WordArray) => {
|
||||
return CryptoJS.enc.Base64.stringify(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* 解密base64
|
||||
*/
|
||||
export const decryptBase64 = (str: string) => {
|
||||
return CryptoJS.enc.Base64.parse(str);
|
||||
};
|
||||
|
||||
/**
|
||||
* 使用密钥对数据进行加密
|
||||
* @param message
|
||||
@ -43,3 +50,17 @@ export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray)
|
||||
});
|
||||
return encrypted.toString();
|
||||
};
|
||||
|
||||
/**
|
||||
* 使用密钥对数据进行解密
|
||||
* @param message
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
export const decryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => {
|
||||
const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
});
|
||||
return decrypted.toString(CryptoJS.enc.Utf8);
|
||||
};
|
||||
|
@ -4,7 +4,7 @@ import JSEncrypt from 'jsencrypt';
|
||||
const publicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;
|
||||
|
||||
// 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
|
||||
const privateKey = '**********';
|
||||
const privateKey = import.meta.env.VITE_APP_RSA_PRIVATE_KEY;
|
||||
|
||||
// 加密
|
||||
export const encrypt = (txt: string) => {
|
||||
|
@ -8,9 +8,10 @@ import { errorCode } from '@/utils/errorCode';
|
||||
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
|
||||
import FileSaver from 'file-saver';
|
||||
import { getLanguage } from '@/lang';
|
||||
import { encryptBase64, encryptWithAes, generateAesKey } from '@/utils/crypto';
|
||||
import { encrypt } from '@/utils/jsencrypt';
|
||||
import { encryptBase64, encryptWithAes, generateAesKey, decryptWithAes, decryptBase64 } from '@/utils/crypto';
|
||||
import { encrypt, decrypt } from '@/utils/jsencrypt';
|
||||
|
||||
const encryptHeader = 'encrypt-key';
|
||||
let downloadLoadingInstance: LoadingInstance;
|
||||
// 是否显示重新登录
|
||||
export const isRelogin = { show: false };
|
||||
@ -78,7 +79,7 @@ service.interceptors.request.use(
|
||||
if (isEncrypt && (config.method === 'post' || config.method === 'put')) {
|
||||
// 生成一个 AES 密钥
|
||||
const aesKey = generateAesKey();
|
||||
config.headers['encrypt-key'] = encrypt(encryptBase64(aesKey));
|
||||
config.headers[encryptHeader] = encrypt(encryptBase64(aesKey));
|
||||
config.data = typeof config.data === 'object' ? encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey);
|
||||
}
|
||||
// FormData数据去请求头Content-Type
|
||||
@ -96,6 +97,20 @@ service.interceptors.request.use(
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(res: AxiosResponse) => {
|
||||
// 加密后的 AES 秘钥
|
||||
const keyStr = res.headers[encryptHeader];
|
||||
// 加密
|
||||
if (keyStr != null && keyStr != '') {
|
||||
const data = res.data;
|
||||
// 请求体 AES 解密
|
||||
const base64Str = decrypt(keyStr);
|
||||
// base64 解码 得到请求头的 AES 秘钥
|
||||
const aesKey = decryptBase64(base64Str.toString());
|
||||
// aesKey 解码 data
|
||||
const decryptData = decryptWithAes(data, aesKey);
|
||||
// 将结果 (得到的是 JSON 字符串) 转为 JSON
|
||||
res.data = JSON.parse(decryptData);
|
||||
}
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || HttpStatus.SUCCESS;
|
||||
// 获取错误信息
|
||||
|
Reference in New Issue
Block a user