vue中使用cookies和crypto-js實現(xiàn)記住密碼和加密的方法
使用crypto-js加解密
第一步,安裝
npm install crypto-js
第二步,在你需要的vue組件內(nèi)import
import CryptoJS from "crypto-js";
第三步,使用
// Encrypt 加密 var cipherText = CryptoJS.AES.encrypt( "my message", "secretkey123" ).toString(); console.log(cipherText) // Decrypt 解密 var bytes = CryptoJS.AES.decrypt(cipherText, "secretkey123"); var originalText = bytes.toString(CryptoJS.enc.Utf8); console.log(originalText); // 'my message'
注意這個mymessage是字符串,如果你要加密的用戶id(number類型)得先轉(zhuǎn)成字符串
更多使用請訪問官方文檔
記住密碼
- 實現(xiàn)原理是登錄的時候,如果勾選了記住密碼(把‘記住密碼'狀態(tài)保存到localstorage)就保存賬號密碼到cookies;
- 之后進(jìn)入登錄頁面的時候,判斷是否記住了密碼(從localstorage判斷),如果記住密碼則導(dǎo)出cookies到表單;
其中保存使用setcookie方法,取出則使用getcookie方法。
ok,我們來編寫方法
//設(shè)置cookie setCookie(portId, psw, exdays) { // Encrypt,加密賬號密碼 var cipherPortId = CryptoJS.AES.encrypt( portId+'', "secretkey123" ).toString(); var cipherPsw = CryptoJS.AES.encrypt(psw+'', "secretkey123").toString(); console.log(cipherPortId+'/'+cipherPsw)//打印一下看看有沒有加密成功 var exdate = new Date(); //獲取時間 exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天數(shù) //字符串拼接cookie,為什么這里用了==,因為加密后的字符串也有個=號,影響下面getcookie的字符串切割,你也可以使用更炫酷的符號。 window.document.cookie = "currentPortId" + "==" + cipherPortId + ";path=/;expires=" + exdate.toGMTString(); window.document.cookie = "password" + "==" + cipherPsw + ";path=/;expires=" + exdate.toGMTString(); }, //讀取cookie getCookie: function() { if (document.cookie.length > 0) { var arr = document.cookie.split("; "); //這里顯示的格式請根據(jù)自己的代碼更改 for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split("=="); //根據(jù)==切割 //判斷查找相對應(yīng)的值 if (arr2[0] == "currentPortId") { // Decrypt,將解密后的內(nèi)容賦值給賬號 var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123"); this.currentPortId = bytes.toString(CryptoJS.enc.Utf8)-0; } else if (arr2[0] == "password") { // Decrypt,將解密后的內(nèi)容賦值給密碼 var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123"); this.password = bytes.toString(CryptoJS.enc.Utf8); } } } }, //清除cookie clearCookie: function() { this.setCookie("", "", -1); }
登錄的方法如下:
login() { this.$http //請根據(jù)實際情況修改該方法 .post(...) .then(res => { if (res.data.code == "success") { if (this.rememberPsw == true) { //判斷用戶是否勾選了記住密碼選項rememberPsw,傳入保存的賬號currentPortId,密碼password,天數(shù)30 this.setCookie(this.currentPortId, this.password, 30); }else{ this.clearCookie(); } //這里是因為要在created中判斷,所以使用了localstorage比較簡單,當(dāng)然你也可以直接根據(jù)cookie的長度or其他騷操作來判斷有沒有記住密碼。 localStorage.setItem("rememberPsw", this.rememberPsw); } else { //---- } }) .catch(err => { //---- }); },
最后要在created狗子函數(shù)內(nèi)判斷用戶是否記住了密碼來執(zhí)行相關(guān)的操作
//判斷是否記住密碼 //**注意這里的true是字符串格式,因為Boolean存進(jìn)localstorage中會變成String** created() { //判斷是否記住密碼 if (localStorage.getItem("rememberPsw") == 'true') { this.getCookie(); } }
最后,界面貼上,其中rememberPsw是記住密碼按鈕的v-model值,currentPortId是第一個框的v-model值,password就是第二個框的v-model值啦。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+elementplus 樹節(jié)點過濾功能實現(xiàn)
樹節(jié)點所展示的街道是讀取的成都市金牛區(qū)的范圍邊界線的json文件,街道下對應(yīng)內(nèi)容市通過fetch調(diào)用接口獲取的內(nèi)容,通過mapTreeData函數(shù)循環(huán)遍歷,對數(shù)據(jù)進(jìn)行處理,這篇文章主要介紹了vue3+elementplus 樹節(jié)點過濾功能實現(xiàn),需要的朋友可以參考下2024-05-05vue實現(xiàn)echarts餅圖/柱狀圖點擊事件實例
echarts原生提供了相應(yīng)的API,只需要在配置好echarts之后綁定相應(yīng)的事件即可,下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)echarts餅圖/柱狀圖點擊事件的相關(guān)資料,需要的朋友可以參考下2023-06-06淺析Vue下的components模板使用及應(yīng)用
這篇文章主要介紹了Vue下的components模板的使用及應(yīng)用,本文通過代碼介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11簡單的vue-resourse獲取json并應(yīng)用到模板示例
本篇文章主要介紹了簡單的vue-resourse獲取json并應(yīng)用到模板示例,非常具有實用價值,需要的朋友可以參考下。2017-02-02