add 新增 请求加密传输 合并优化 !pr377
| | |
| | | "fuse.js": "6.6.2", |
| | | "js-cookie": "3.0.1", |
| | | "jsencrypt": "3.3.1", |
| | | "crypto-js": "^4.1.1", |
| | | "nprogress": "0.2.0", |
| | | "path-browserify": "1.0.1", |
| | | "path-to-regexp": "6.2.0", |
| | |
| | | "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", |
| | |
| | | return request({ |
| | | url: '/auth/login', |
| | | headers: { |
| | | isToken: false |
| | | isToken: false, |
| | | isEncrypt: true |
| | | }, |
| | | method: 'post', |
| | | data: params |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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()); |
| | | }; |
| | | |
| | | /** |
| | | * éæºçæaes å¯é¥ |
| | | * @returns {string} |
| | | */ |
| | | export const encryptBase64 = (str: string) => { |
| | | return CryptoJS.enc.Base64.stringify(str); |
| | | }; |
| | | |
| | | /** |
| | | * 使ç¨å¯é¥å¯¹æ°æ®è¿è¡å å¯ |
| | | * @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(); |
| | | }; |
| | |
| | | // å¯é¥å¯¹çæ http://web.chacuo.net/netrsakeypair |
| | | |
| | | const publicKey = |
| | | 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' + 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='; |
| | | 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' + |
| | | 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='; |
| | | |
| | | const privateKey = |
| | | 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' + |
| | |
| | | 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'; |
| | | |
| | | let downloadLoadingInstance: LoadingInstance; |
| | | // æ¯å¦æ¾ç¤ºéæ°ç»å½ |
| | |
| | | const isToken = (config.headers || {}).isToken === false; |
| | | // æ¯å¦éè¦é²æ¢æ°æ®éå¤æäº¤ |
| | | const isRepeatSubmit = (config.headers || {}).repeatSubmit === false; |
| | | // æ¯å¦éè¦å å¯ |
| | | const isEncrypt = (config.headers || {}).isEncrypt === 'true'; |
| | | if (getToken() && !isToken) { |
| | | config.headers['Authorization'] = 'Bearer ' + getToken(); // 让æ¯ä¸ªè¯·æ±æºå¸¦èªå®ä¹token è¯·æ ¹æ®å®é
æ
åµèªè¡ä¿®æ¹ |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | // å½å¼å¯åæ°å å¯ |
| | | if (isEncrypt && (config.method === 'post' || config.method === 'put')) { |
| | | // çæä¸ä¸ª AES å¯é¥ |
| | | const aesKey = generateAesKey(); |
| | | config.headers['encrypt-key'] = 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) { |
| | | delete config.headers['Content-Type']; |