vue对于路由参数进行加密处理
实现方式:stringifyQuery 和 parseQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | // src\utils\encryption.js // 按需引入 CryptoJS 需要的组件 const CryptoJS = require( "crypto-js/core" ); const Latin1 = require( "crypto-js/enc-latin1" ); const AES = require( "crypto-js/aes" ); const ZeroPadding = require( "crypto-js/pad-zeropadding" ); const Utf8 = require( "crypto-js/enc-utf8" ); const Base64 = require( "crypto-js/enc-base64" ); /* * 加密 解密 */ const baseCryptoCode = "********" ; // 这个私钥每个项目指定一个唯一。更换密钥,请确认16位 const getKeyHex = (cryptoCode) => Latin1.parse(cryptoCode || baseCryptoCode); const getIvHex = () => Latin1.parse(baseCryptoCode); /** * 加密 * @param {String} key * @param {String} cryptoCode * @returns {string} */ export const getEncrypt = (key, cryptoCode) => { let keyHex = getKeyHex(cryptoCode); let ivHex = getIvHex(); try { key = JSON.stringify(key); } catch (e) { console.warn(e); } return AES.encrypt(key, keyHex, { mode: CryptoJS.mode.CBC, padding: ZeroPadding, iv: ivHex, }).toString(); }; /** * 加密后转base64 * @param {String}} key * @param {String} cryptoCode */ export const getEncryptToBase64 = (key, cryptoCode) => { let encryptStr = getEncrypt(key, cryptoCode); let wordArray = Utf8.parse(encryptStr); return Base64.stringify(wordArray); }; /** * 解密 * @param data * @returns {string} */ export const getDecrypt = (data) => { let keyHex = getKeyHex(); let ivHex = getIvHex(); let decrypted = AES.decrypt( { ciphertext: Base64.parse(data), }, keyHex, { mode: CryptoJS.mode.CBC, padding: ZeroPadding, iv: ivHex, } ).toString(Utf8); try { decrypted = JSON.parse(decrypted); } catch (e) { console.warn(e); } return decrypted; }; /** * 对base64数据解密 先解析base64,在做解密 * @param {String} data * @returns {string} */ export const getDecryptByBase64 = (data) => { let parsedWordArray = Base64.parse(data); let decryptStr = parsedWordArray.toString(Utf8); return getDecrypt(decryptStr); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | // src\utils\query.js import { getEncryptToBase64 as encrypt, getDecryptByBase64 as decrypt, } from "./encryption" ; const encodeReserveRE = /[!'()*]/g; const encodeReserveReplacer = (c) => "%" + c.charCodeAt(0).toString(16); const commaRE = /%2C/g; const encode = (str) => encodeURIComponent(str) .replace(encodeReserveRE, encodeReserveReplacer) .replace(commaRE, "," ); const decode = decodeURIComponent; /** * 判断字符串是否是base64 * @param { string } str * @returns { boolean } */ function isBase64(str) { if (str === "" || str.trim() === "" ) { return false ; } try { return btoa(atob(str)) == str; } catch (err) { return false ; } } /** * 序列化对象 并加密 * @param {Object} obj */ export const stringifyQuery = (obj) => { const res = obj ? Object.keys(obj) .map((key) => { const val = obj[key]; if (val === undefined) { return "" ; } if (val === null ) { return encode(key); } if (Array.isArray(val)) { const result = []; val.forEach((val2) => { if (val2 === undefined) { return ; } if (val2 === null ) { result.push(encode(key)); } else { result.push(encode(key) + "=" + encode(val2)); } }); return result.join( "&" ); } return encode(key) + "=" + encode(val); }) .filter((x) => x.length > 0) .join( "&" ) : null ; return res ? `?${encrypt(res)}` : "" ; }; /** * 解密 反序列化字符串参数 * @param {String}} query */ export const parseQuery = (query) => { const res = {}; query = query.trim().replace(/^(\?| #|&)/, ""); if (!query) { return res; } // 解密 query = isBase64(query) ? decrypt(query) : query; query.split( "&" ).forEach((param) => { const parts = param.replace(/\+/g, " " ).split( "=" ); const key = decode(parts.shift()); const val = parts.length > 0 ? decode(parts.join( "=" )) : null ; if (res[key] === undefined) { res[key] = val; } else if (Array.isArray(res[key])) { res[key].push(val); } else { res[key] = [res[key], val]; } }); return res; }; |
在router/index.js添加一下代码:
1 2 3 4 5 6 7 8 9 | import { stringifyQuery, parseQuery } from "@/utils/query" ; const routes = []; const router = new VueRouter({ mode: "history" , stringifyQuery: stringifyQuery, // 序列化query参数 parseQuery: parseQuery, // 反序列化query参数 routes, }); export default router; |
以下加密的效果:
加密:
解密后:
希望大佬看到有不对的地方,提出博主予以改正!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2020-01-09 使用vue-baidu-map解析geojson
2020-01-09 vue-router mode 'history' 模式 nginx配置