当前位置:网站首页>crypto-js加密算法库【安装教程、缓存加密】
crypto-js加密算法库【安装教程、缓存加密】
2022-04-22 04:33:00 【qq_22841387】
问题描述
当前项目直接对缓存直接操作,也就是说,我直接修改缓存内的数据,可以越权去查看不属于本角色的API

当前我只修改了缓存中的一个字段accountTypeId即查看到了所有老师的页面,并且能进行老师的操作
现在我需要将缓存进行加密
解决方法——加密缓存
1.安装crypto-js
npm install crypto-js
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BGLjCFOE-1650551893026)(assets/安装crypto-js-20220421212200-cojvxtu.jpg)]](/img/01/1a13a4b75d5fa69533bbe794d7b672.png)
若需要在ts中使用则安装@types/crypto-js
npm i --save-dev @types/crypto-js
2.封装加密/解密函数
/* eslint-disable */
import CryptoJS from 'crypto-js'
const CRYPTO_SECRET = '加密的密钥(可通过随机生成密码器生成)'
/** * AES加密数据 * @param data 需要加密的数据 * @returns 返回AES加密后的数据 */
export function encrypto(data: any) {
const newData = JSON.stringify(data)
return CryptoJS.AES.encrypt(newData, CRYPTO_SECRET).toString()
}
/** * AES解密数据 * @param encryptedData 加密后的数据 * @returns 解密的数据 */
export function decrypto(encryptedData: any) {
const bytes = CryptoJS.AES.decrypt(encryptedData, CRYPTO_SECRET);
const originText = bytes.toString(CryptoJS.enc.Utf8)
if (originText) {
return JSON.parse(originText);
}
return '解密失败'
}
3.存入缓存时加密
export function setLocal(key: string, value: unknown, expire: number | null = DEFAULT_CACHE_TIME) {
const storeData: StoreData = {
value, expire: expire !== null ? new Date().getTime() + expire * 1000 : null }
const json = encrypto(storeData)
window.localStorage.setItem(key, json);
}
这个函数中,具有对时间戳的处理(用于判断是否过时)
版权声明
本文为[qq_22841387]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_22841387/article/details/124333795
边栏推荐
- An article tells you what analysis as a service (AAAS) is
- Summary of using knowledge points
- JVM shorthand
- Introduction to Intel edge software center
- Cisco packet tracker experiment set
- Revit data processing (III)
- 2022年A特种设备相关管理(电梯)复训题库及答案
- (sip-1-phone registration) analysis of the whole process of IP phone registration to PBX telephone exchange through SIP Protocol - how to see the message in Wireshark
- Jeesite export Excel
- Pgpool II 4.3 Chinese Manual - introductory tutorial
猜你喜欢

Zuo Chengyun - Dachang question brushing class - the minimum number of exchanges of one character on the left and another character on the right

JVM简记

(sip-1-phone registration) analysis of the whole process of IP phone registration to PBX telephone exchange through SIP Protocol - how to see the message in Wireshark

2022T电梯修理考试练习题及在线模拟考试

Mapbox creates multiple draggable marker points

队列第二篇

H7-TOOL发布固件V2.15, 脱机烧录增加瑞萨,合泰以及IS25WP全系列SPI FLASH等(2022-04-14)

2022G2电站锅炉司炉操作证考试题库及在线模拟考试

同行面试分享 联想 winform方向 20220420

Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
随机推荐
spark 安装与使用 educoder
02-SparkSQL
Unity 简单UI前缀树红点系统
02 - sparksql
How do I test the shuttle application? Unit test
How much do you know about the testing methods of software testing?
7-2 symbol pairing | PTA
[logical fallacies in life] right for people, wrong for things and dilemma trap
AT32 MCU F435/437 DFU DEMO
H7-TOOL发布固件V2.15, 脱机烧录增加瑞萨,合泰以及IS25WP全系列SPI FLASH等(2022-04-14)
When the SQL statement is executed by hyperf, the parameter will have two single quotes
[vi] summary
Revit (3) - second opening - create column
[concurrent programming 046] for the synchronization method, how does the processor realize atomic operation?
[taro development] - Global custom navigation bar adaptation message notification box location and other problems (14)
Introduction to Intel edge software center
Target detection - lightweight network (as of April 21, 2022)
How expensive is the "salary" of software testing industry?
队列第二篇
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer