From 5290ab5e94b8cd2979842424cdd5f4d8decfef96 Mon Sep 17 00:00:00 2001 From: wdhcr Date: Wed, 21 Jun 2023 23:12:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8jsencrypt=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=8E=A5=E5=8F=A3=E5=8F=82=E6=95=B0=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E5=8A=9F=E8=83=BD,=20=E5=8F=AF=E5=9C=A8.env=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=BC=80=E5=90=AF,=20=E4=B9=9F=E5=8F=AF=E5=9C=A8?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=8E=A5=E5=8F=A3=E5=8D=95=E7=8B=AC=E5=BC=80?= =?UTF-8?q?=E5=90=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 3 +++ .env.production | 4 ++++ package.json | 2 ++ src/utils/aes.ts | 37 +++++++++++++++++++++++++++++++++++++ src/utils/request.ts | 12 ++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 src/utils/aes.ts diff --git a/.env.development b/.env.development index 4ec5afb..913212b 100644 --- a/.env.development +++ b/.env.development @@ -7,6 +7,9 @@ VITE_APP_ENV = 'development' # 开发环境 VITE_APP_BASE_API = '/dev-api' +# 请求接口是否加密 +VITE_APP_IS_ENCRYPT = false + # 应用访问路径 例如使用前缀 /admin/ VITE_APP_CONTEXT_PATH = '/' diff --git a/.env.production b/.env.production index 6e6510a..e87dfa7 100644 --- a/.env.production +++ b/.env.production @@ -4,6 +4,10 @@ VITE_APP_TITLE = RuoYi-Vue-Plus多租户管理系统 # 生产环境配置 VITE_APP_ENV = 'production' + +# 请求接口是否加密 +VITE_APP_IS_ENCRYPT = false + # 应用访问路径 例如使用前缀 /admin/ VITE_APP_CONTEXT_PATH = '/' diff --git a/package.json b/package.json index d9a587d..ed0e36a 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "animate.css": "4.1.1", "await-to-js": "^3.0.0", "axios": "^1.3.4", + "crypto-js": "^4.1.1", "echarts": "5.4.0", "element-plus": "2.2.27", "file-saver": "2.0.5", @@ -42,6 +43,7 @@ "devDependencies": { "@iconify/json": "^2.2.40", "@intlify/unplugin-vue-i18n": "0.8.2", + "@types/crypto-js": "^4.1.1", "@types/file-saver": "2.0.5", "@types/js-cookie": "3.0.3", "@types/node": "18.14.2", diff --git a/src/utils/aes.ts b/src/utils/aes.ts new file mode 100644 index 0000000..a6c6e43 --- /dev/null +++ b/src/utils/aes.ts @@ -0,0 +1,37 @@ +import CryptoJS from 'crypto-js'; + +/** + * 随机生成32位的字符串 + * @returns {string} + */ +const generateRandomString = () => { + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let result = ''; + const charactersLength = characters.length; + for (let i = 0; i < 32; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +}; + +/** + * 随机生成aes 密钥 + * @returns {string} + */ +export const generateAesKey = () => { + return CryptoJS.enc.Utf8.parse(generateRandomString()); +}; + +/** + * 使用密钥对数据进行加密 + * @param message + * @param aesKey + * @returns {string} + */ +export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => { + const encrypted = CryptoJS.AES.encrypt(message, aesKey, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7 + }); + return encrypted.toString(); +}; diff --git a/src/utils/request.ts b/src/utils/request.ts index d5fac4e..96b1b25 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -8,6 +8,9 @@ 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 { encryptWithAes, generateAesKey } from '@/utils/aes'; +import { encrypt } from '@/utils/jsencrypt'; +import CryptoJS from 'crypto-js'; let downloadLoadingInstance: LoadingInstance; // 是否显示重新登录 @@ -29,6 +32,8 @@ service.interceptors.request.use( const isToken = (config.headers || {}).isToken === false; // 是否需要防止数据重复提交 const isRepeatSubmit = (config.headers || {}).repeatSubmit === false; + // 是否需要加密 + const isEncrypt = (config.headers || {}).isEncrypt === 'true' || import.meta.env.VITE_APP_IS_ENCRYPT === 'true'; if (getToken() && !isToken) { config.headers['Authorization'] = 'Bearer ' + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改 } @@ -63,6 +68,13 @@ service.interceptors.request.use( } } } + // 当开启参数加密 + if (isEncrypt && (config.method === 'post' || config.method === 'put')) { + // 生成一个 AES 密钥 + const aesKey = generateAesKey(); + config.headers['AES'] = encrypt(aesKey.toString(CryptoJS.enc.Base64)); + config.data = typeof config.data === 'object' ? encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey); + } // FormData数据去请求头Content-Type if (config.data instanceof FormData) { delete config.headers['Content-Type'];