登录的时候,如果要记住密码,就要将用户密码加密存入cookies。下次登录的时候要将密码从cookies中取出并解密。
准备工作
npm install jsencrypt --dev //安装加密算法库
npm install --save js-cookie //安装cookies
加解密js
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = '',//生成的公钥
const privateKey='',//生成的私钥
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}
页面
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
//用户勾选了记住密码的场景
if (this.loginForm.rememberMe) {
//设置cookies
Cookies.set("username", this.loginForm.username, { expires: 30 });//expires有效期30天
Cookies.set("password", encrypt(this.loginForm.password), {
expires: 30,
});
Cookies.set("rememberMe", this.loginForm.rememberMe, {
expires: 30,
});
} else {
//删除指定的cookies
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove("rememberMe");
}
//获取指定的cookies const username = Cookies.get("username");
标签:username,Cookies,const,encryptor,cookies,解密,加密
From: https://www.cnblogs.com/sunnybob/p/16779514.html