vue登录账号密码rsa加密
程序员文章站
2024-03-16 18:37:10
...
先在当前页面引入min.js
import jsencrypt from '@/utils/jsencrypt.min.js'
获取公钥的方法,在页面一加载就调用
getkey(){
this.show3=false;
var api='/auth/getKey';
var data={}
this.axios.post(api,Qs.stringify(data),{headers:{
'Content-Type':'application/x-www-form-urlencoded'
}}).then((response)=>{
console.log("返回:"+response)
if(response.status==200){
this.publickey = response.data.key;
}else{
return "error";
}
}).catch((error)=>{
this.show3=true;
this.fullscreenLoading = false;
console.log(error);
})
},
在登录方法里写
if(this.publicKey != null || this.publickey != ""){
var encrypt = new JSEncrypt();
encrypt.setPublicKey(this.publickey);
var userName = encrypt.encrypt(this.userName);
var password = encrypt.encrypt(this.password);
//提交之前,检查是否已经加密。假设用户的密码不超过20位,加密后的密码不小于20位
if(password.length < 20) {
//加密失败提示
alert("登录失败,请稍后重试...");
}else{
//开始配置相关信息准备发送请求
var api='/auth/ajaxLogin';
var data={
username: userName,
password:password,
rememberMe: false
}
this.axios.post(api,Qs.stringify(data),{headers:{
'Content-Type':'application/x-www-form-urlencoded'
}}).then((response)=>{
// 如果成功要跳转至首页, 将token保存到localStorage 如果失败显示账号密码错误不进行跳转
if (response.data.data.tokenId !== '') {
localStorage.setItem('tokenId',response.data.data.tokenId);
//localStorage.setItem('userName',this.userName);
if(response.data.data.classify == "01"){
window.location.href = response.data.data.iscUrl;
}else{
//this.$store.commit('setTokenId',response.data.data.tokenId);
// 请求动态路由接口获取动态导航内容
var api='/main/authzList';
this.axios.get(api).then((response)=>{
var data = response.data.data;
this.menus1=data;
console.log(data)
window.sessionStorage.setItem('user',JSON.stringify(data));
MenuUtils(this.$router.options.routes[1].children,data);
// this.$router不是响应式的,所以手动将路由元注入路由对象
// this.$router.options.routes.push(routers);
this.$router.addRoutes(this.$router.options.routes);
this.$router.push({path: '/home'})
}).catch((error)=>{
console.log(error);
this.fullscreenLoading = false;
this.$message.error('获取菜单信息失败!');
})
}
}else{
this.show2=true;
this.fullscreenLoading = false;
this.$message.error('登录失败!');
}}).catch((error)=>{
this.show2=true;
// this.userName='';
// this.password='';
this.fullscreenLoading = false;
console.log(error);
})
}
}