From 542f73f0e64b9cb4963a869e61d53993770912b5 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期一, 04 三月 2024 14:18:21 +0800 Subject: [PATCH] update 优化 支持全局开启或关闭接口加密功能 --- .env.development | 2 ++ src/utils/request.ts | 42 +++++++++++++++++++++++------------------- src/types/env.d.ts | 1 + .env.production | 2 ++ 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_PORT = 80 +# 鎺ュ彛鍔犲瘑鍔熻兘寮�鍏�(濡傞渶鍏抽棴 鍚庣涔熷繀椤诲搴斿叧闂�) +VITE_APP_ENCRYPT = true # 鎺ュ彛鍔犲瘑浼犺緭 RSA 鍏挜涓庡悗绔В瀵嗙閽ュ搴� 濡傛洿鎹㈤渶鍓嶅悗绔竴鍚屾洿鎹� VITE_APP_RSA_PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==' # 鎺ュ彛鍝嶅簲瑙e瘑 RSA 绉侀挜涓庡悗绔姞瀵嗗叕閽ュ搴� 濡傛洿鎹㈤渶鍓嶅悗绔竴鍚屾洿鎹� diff --git a/.env.production b/.env.production index 0e3b9fc..c15308f 100644 --- a/.env.production +++ b/.env.production @@ -21,6 +21,8 @@ VITE_APP_PORT = 80 +# 鎺ュ彛鍔犲瘑鍔熻兘寮�鍏�(濡傞渶鍏抽棴 鍚庣涔熷繀椤诲搴斿叧闂�) +VITE_APP_ENCRYPT = true # 鎺ュ彛鍔犲瘑浼犺緭 RSA 鍏挜涓庡悗绔В瀵嗙閽ュ搴� 濡傛洿鎹㈤渶鍓嶅悗绔竴鍚屾洿鎹� VITE_APP_RSA_PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==' # 鎺ュ彛鍝嶅簲瑙e瘑 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 @@ 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 @@ } } } - // 褰撳紑鍚弬鏁板姞瀵� - 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.response.use( (res: AxiosResponse) => { - // 鍔犲瘑鍚庣殑 AES 绉橀挜 - const keyStr = res.headers[encryptHeader]; - // 鍔犲瘑 - if (keyStr != null && keyStr != '') { - const data = res.data; - // 璇锋眰浣� AES 瑙e瘑 - const base64Str = decrypt(keyStr); - // base64 瑙g爜 寰楀埌璇锋眰澶寸殑 AES 绉橀挜 - const aesKey = decryptBase64(base64Str.toString()); - // aesKey 瑙g爜 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 瑙e瘑 + const base64Str = decrypt(keyStr); + // base64 瑙g爜 寰楀埌璇锋眰澶寸殑 AES 绉橀挜 + const aesKey = decryptBase64(base64Str.toString()); + // aesKey 瑙g爜 data + const decryptData = decryptWithAes(data, aesKey); + // 灏嗙粨鏋� (寰楀埌鐨勬槸 JSON 瀛楃涓�) 杞负 JSON + res.data = JSON.parse(decryptData); + } } // 鏈缃姸鎬佺爜鍒欓粯璁ゆ垚鍔熺姸鎬� const code = res.data.code || HttpStatus.SUCCESS; -- Gitblit v1.9.3