From 542f73f0e64b9cb4963a869e61d53993770912b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Mon, 4 Mar 2024 14:18:21 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=85=A8=E5=B1=80=E5=BC=80=E5=90=AF=E6=88=96=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=8E=A5=E5=8F=A3=E5=8A=A0=E5=AF=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 ++ .env.production | 2 ++ src/types/env.d.ts | 1 + src/utils/request.ts | 42 +++++++++++++++++++++++------------------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/.env.development b/.env.development index 53ae057..ad47825 100644 --- a/.env.development +++ b/.env.development @@ -18,6 +18,8 @@ VITE_APP_POWERJOB_ADMIN = 'http://localhost:7700/' VITE_APP_PORT = 80 +# 接口加密功能开关(如需关闭 后端也必须对应关闭) +VITE_APP_ENCRYPT = true # 接口加密传输 RSA 公钥与后端解密私钥对应 如更换需前后端一同更换 VITE_APP_RSA_PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==' # 接口响应解密 RSA 私钥与后端加密公钥对应 如更换需前后端一同更换 diff --git a/.env.production b/.env.production index 0e3b9fc..c15308f 100644 --- a/.env.production +++ b/.env.production @@ -21,6 +21,8 @@ VITE_BUILD_COMPRESS = gzip VITE_APP_PORT = 80 +# 接口加密功能开关(如需关闭 后端也必须对应关闭) +VITE_APP_ENCRYPT = true # 接口加密传输 RSA 公钥与后端解密私钥对应 如更换需前后端一同更换 VITE_APP_RSA_PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==' # 接口响应解密 RSA 私钥与后端加密公钥对应 如更换需前后端一同更换 diff --git a/src/types/env.d.ts b/src/types/env.d.ts index 6667d05..5b18a8e 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -14,6 +14,7 @@ interface ImportMetaEnv { VITE_APP_MONITRO_ADMIN: string; VITE_APP_POWERJOB_ADMIN: string; VITE_APP_ENV: string; + VITE_APP_ENCRYPT: string VITE_APP_RSA_PUBLIC_KEY: string; VITE_APP_RSA_PRIVATE_KEY: string; VITE_APP_CLIENT_ID: string; diff --git a/src/utils/request.ts b/src/utils/request.ts index 3de2076..f2a69f0 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -76,12 +76,14 @@ service.interceptors.request.use( } } } - // 当开启参数加密 - if (isEncrypt && (config.method === 'post' || config.method === 'put')) { - // 生成一个 AES 密钥 - const aesKey = generateAesKey(); - config.headers[encryptHeader] = encrypt(encryptBase64(aesKey)); - config.data = typeof config.data === 'object' ? encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey); + if (import.meta.env.VITE_APP_ENCRYPT === 'true') { + // 当开启参数加密 + if (isEncrypt && (config.method === 'post' || config.method === 'put')) { + // 生成一个 AES 密钥 + const aesKey = generateAesKey(); + 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 if (config.data instanceof FormData) { @@ -97,19 +99,21 @@ 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); + if (import.meta.env.VITE_APP_ENCRYPT === 'true') { + // 加密后的 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;