vue3封裝AES(CryptoJS)前端加密解密通信代碼實(shí)現(xiàn)
項(xiàng)目場(chǎng)景:
防止數(shù)據(jù)被爬取,前后端傳參接收參數(shù)需要加密處理,使用AES加密。主要使用CryptoJS庫(kù)中的函數(shù)方法,加密:CryptoJS.AES.encrypt(), 解密:CryptoJS.AES.decrypt()。
代碼實(shí)現(xiàn)
- 安裝CryptoJS庫(kù):
npm install crypto-js
- 創(chuàng)建文件夾,@/utils/secret,引入CryptoJS庫(kù)并封裝加密解密函數(shù)方法:
import CryptoJS from 'crypto-js/crypto-js';
const key = CryptoJS.enc.Utf8.parse('123321'); // 密鑰 后端提供
const iv = CryptoJS.enc.Utf8.parse(''); // 偏移量
/**
* AES加密 :字符串 key iv 返回base64
*/
export function Encrypt(word) {
const srcs = CryptoJS.enc.Utf8.parse(word);
const encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
* AES 解密 :字符串 key iv 返回base64
* */
export function Decrypt(word) {
const base64 = CryptoJS.enc.Base64.parse(word);
const src = CryptoJS.enc.Base64.stringify(base64);
const decrypt = CryptoJS.AES.decrypt(src, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
return CryptoJS.enc.Utf8.stringify(decrypt);
}- 引入加密解密方法,對(duì)axios中封裝的request 請(qǐng)求前的統(tǒng)一處理做加密:
// 引入
import { Encrypt, Decrypt } from '/@/utils/secret';
this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
// If cancel repeat request is turned on, then cancel repeat request is prohibited
// @ts-ignore
const { ignoreCancelToken } = config.requestOptions;
const ignoreCancel =
ignoreCancelToken !== undefined
? ignoreCancelToken
: this.options.requestOptions?.ignoreCancelToken;
!ignoreCancel && axiosCanceler.addPending(config);
if (requestInterceptors && isFunction(requestInterceptors)) {
config = requestInterceptors(config, this.options);
}
// 關(guān)鍵代碼:
// JSON加密,formData不加密(對(duì)需要處理的數(shù)據(jù)格式做判斷)
if (Object.prototype.toString.call(config.data) != '[object FormData]') {
config.data = { encryptedData: Encrypt(JSON.stringify(config.data)) }; // 加密傳參,后端要求的傳參:encryptedData:加密參數(shù)
}
return config;
}, undefined);
- response 響應(yīng)前的統(tǒng)一處理做解密:
this.axiosInstance.interceptors.response.use(async (res: AxiosResponse<any>) => {
// 關(guān)鍵代碼:
// 導(dǎo)出時(shí)數(shù)據(jù)格式為blob不解密(對(duì)需要處理的數(shù)據(jù)格式做判斷)
if (Object.prototype.toString.call(res.data) != '[object Blob]') {
res.data = JSON.parse(Decrypt(res.data)); // 解密返回參數(shù)
}
const config = res.config;
if (res.data.code === 401) {
// 如果未認(rèn)證,并且未進(jìn)行刷新令牌,說(shuō)明可能是訪問(wèn)令牌過(guò)期了
if (!isRefreshToken) {
isRefreshToken = true;
// 1. 獲取到刷新token
if (getRefreshToken()) {
// 2. 進(jìn)行刷新訪問(wèn)令牌
try {
const refreshTokenRes = await this.refreshToken();
// 2.1 刷新成功,則回放隊(duì)列的請(qǐng)求 + 當(dāng)前請(qǐng)求
setToken('Bearer ' + refreshTokenRes.data.data.accessToken);
(config as Recordable).headers.Authorization = getToken();
requestList.forEach((cb: any) => {
cb();
});
requestList = [];
return new Promise((resolve) => {
resolve(this.axiosInstance(config));
});
// res = await Promise.all([this.axiosInstance(config)])[0]
} catch (e) {
requestList.forEach((cb: any) => {
cb();
});
} finally {
requestList = [];
isRefreshToken = false;
}
}
} else {
// 添加到隊(duì)列,等待刷新獲取到新的令牌
return new Promise((resolve) => {
requestList.push(() => {
(config as Recordable).headers.Authorization = getToken(); // 讓每個(gè)請(qǐng)求攜帶自定義token 請(qǐng)根據(jù)實(shí)際情況自行修改
resolve(this.axiosInstance(config));
});
});
}
}
res && axiosCanceler.removePending(res.config);
if (responseInterceptors && isFunction(responseInterceptors)) {
res = responseInterceptors(res);
}
return res;
}, undefined);
最終效果
加密傳參:

后端返回參數(shù)加密:

在線加密解密工具:https://www.mklab.cn/utils/aes 或者 http://tools.jb51.net/password/aes_encode

總結(jié)
到此這篇關(guān)于vue3封裝AES(CryptoJS)前端加密解密通信代碼實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue3封裝AES加密解密通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 中如何將函數(shù)作為 props 傳遞給組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue 中如何將函數(shù)作為 props 傳遞給組件的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Vue裝飾器中的vue-property-decorator?和?vux-class使用詳解
這篇文章主要介紹了Vue裝飾器中的vue-property-decorator?和?vux-class使用詳解,通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)vue-property-decorator?和?vux-class的使用感興趣的朋友一起看看吧2022-08-08
vue v-on:click傳遞動(dòng)態(tài)參數(shù)的步驟
這篇文章主要介紹了vue v-on:click傳遞動(dòng)態(tài)參數(shù)的步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
echarts3如何清空上一次加載的series數(shù)據(jù)
這篇文章主要介紹了echarts3如何清空上一次加載的series數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue全家桶實(shí)踐項(xiàng)目總結(jié)(推薦)
本篇文章主要介紹了Vue全家桶實(shí)踐項(xiàng)目總結(jié)(推薦),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
vue如何根據(jù)網(wǎng)站路由判斷頁(yè)面主題色詳解
這篇文章主要給大家介紹了關(guān)于vue如何根據(jù)網(wǎng)站路由判斷頁(yè)面主題色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
深入探討如何在Vue中使用EventBus實(shí)現(xiàn)組件間的高效通信
在現(xiàn)代前端開(kāi)發(fā)中,Vue.js?作為一種流行的漸進(jìn)式框架,廣泛應(yīng)用于各類?Web?項(xiàng)目的構(gòu)建中,本文將深入探討如何在?Vue?中使用?EventBus,實(shí)現(xiàn)組件間的高效通信,需要的可以了解下2024-11-11
vuex 如何動(dòng)態(tài)引入 store modules
這篇文章主要介紹了vuex 如何動(dòng)態(tài)引入 store modules,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue實(shí)現(xiàn)注冊(cè)頁(yè)面的用戶交互詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)注冊(cè)頁(yè)面的用戶交互的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們深入掌握vue有一定的幫助,需要的小伙伴可以參考下2023-12-12

