1.下载加密插件,终端运行
yarn add jsencrypt
2.配置加密信息文件rsaEncrypt.js放到utils中,代码如下
import JSEncrypt from 'jsencrypt/bin/jsencrypt'
// 密钥对生成网址 http://web.chacuo.net/netrsakeypair
const publicKey = 'MIGfMA0GCSqGSIb3DQEBxxxxxxxxxxxx'
const privateKey = 'MIICdgIBADANBgxxxxxxxxx'
// 加密方法
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)
}
3.登陆 ,修改密码等地方使用,注意后端也要对应配置
import { encrypt } from "@utils/rsaEncrypt";
const toLogin = ()=>{
proxy.$refs.loginFormRef.validate((valid) => {
const form = {
account: loginForm.account,
password: encrypt(loginForm.password),
rememberMe: loginForm.rememberMe,
code: loginForm.code,
uuid: loginForm.uuid,
};
if (valid) {
state.loading = true;
userStore
.login(form)
.then(() => {
state.loading = false;
router.push({ path: proxy.$route.query?.redirect || "/index" });
})
.catch(() => {
state.loading = false;
getCode();
});
}
});
}