Vue實現(xiàn)登錄記住賬號密碼功能的思路與過程
實現(xiàn)思路
用戶登錄時若勾選“記住我”功能選項,則將登錄名和密碼(加密后)保存至本地緩存中,下次登錄頁面加載時自動獲取保存好的賬號和密碼(需解密),回顯到登錄輸入框中。
這里有三種方法來存儲賬號密碼:
1. sessionStorage(不推薦)
1). 僅在當前會話下有效,關(guān)閉瀏覽器窗口后就被清除了
2). 存放數(shù)據(jù)大小一般為5MB
3). 不與服務器進行交互通信
2. localStorage
1). 除非主動清除localStorage里的信息,否則將永遠存在,關(guān)閉瀏覽器窗口后下次啟動任然存在
2). 存放數(shù)據(jù)大小一般為5MB
3). 不與服務器進行交互通信
3. cookies
1). 可以手動設置過期時間,超過有效期則失效。未設置過期時間,關(guān)閉瀏覽器窗口后就被清除了
2). 存放數(shù)據(jù)大小一般為4K
3). 每次請求都會被傳送到服務器
這里主要介紹第二種和第三種的使用方法。
功能界面
<el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="loginForm demo-ruleForm"> <!-- 賬號 --> <el-form-item label="賬號" prop="userId" autocomplete="on"> <el-input v-model="loginForm.userId" placeholder="請輸入賬號"></el-input> </el-form-item> <!-- 密碼 --> <el-form-item label="密碼" prop="password"> <el-input type="password" v-model="loginForm.password" placeholder="請輸入密碼" @keyup.enter="submitForm('loginForm')"></el-input> </el-form-item> <div class="tip"> <!-- 記住我 --> <el-checkbox v-model="checked" class="rememberMe">記住我</el-checkbox> <!-- 找回密碼 --> <el-button type="text" @click="open()" class="forgetPw">忘記密碼?</el-button> </div> <!-- 登錄 --> <el-form-item> <el-button type="primary" @click="submitForm('loginForm')" class="submit-btn">登錄</el-button> </el-form-item> </el-form>
記住賬號密碼功能的具體實現(xiàn)
密碼加密
為提高安全性,密碼存儲前需進行加密處理。目前加密方式有很多種,我這里選用了base64。
npm安裝base64依賴
//安裝 npm install --save js-base64 //引入 const Base64 = require("js-base64").Base64
localStorage
export default { data() { return { loginForm: { userId: "", password: "", }, checked: false, }; }, mounted() { let username = localStorage.getItem("userId"); if (username) { this.loginForm.userId = localStorage.getItem("userId"); this.loginForm.password = Base64.decode(localStorage.getItem("password"));// base64解密 this.checked = true; } }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { /* ------ 賬號密碼的存儲 ------ */ if (this.checked) { let password = Base64.encode(this.loginForm.password); // base64加密 localStorage.setItem("userId", this.loginForm.userId); localStorage.setItem("password", password); } else { localStorage.removeItem("userId"); localStorage.removeItem("password"); } /* ------ http登錄請求 ------ */ } else { console.log("error submit!!"); return false; } }); }, }, };
cookies
export default { data() { return { loginForm: { userId: "", password: "", }, checked: false, }; }, mounted() { this.getCookie(); }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { /* ------ 賬號密碼的存儲 ------ */ if (this.checked) { let password = Base64.encode(this.loginForm.password); // base64加密 this.setCookie(this.loginForm.userId, password, 7); } else { this.setCookie("", "", -1); // 修改2值都為空,天數(shù)為負1天就好了 } /* ------ http登錄請求 ------ */ } else { console.log("error submit!!"); return false; } }); }, // 設置cookie setCookie(userId, password, days) { let date = new Date(); // 獲取時間 date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * days); // 保存的天數(shù) // 字符串拼接cookie window.document.cookie = "userId" + "=" + userId + ";path=/;expires=" + date.toGMTString(); window.document.cookie = "password" + "=" + password + ";path=/;expires=" + date.toGMTString(); }, // 讀取cookie 將用戶名和密碼回顯到input框中 getCookie() { if (document.cookie.length > 0) { let arr = document.cookie.split("; "); //分割成一個個獨立的“key=value”的形式 for (let i = 0; i < arr.length; i++) { let arr2 = arr[i].split("="); // 再次切割,arr2[0]為key值,arr2[1]為對應的value if (arr2[0] === "userId") { this.loginForm.userId = arr2[1]; } else if (arr2[0] === "password") { this.loginForm.password = Base64.decode(arr2[1]);// base64解密 this.checked = true; } } } }, }, };
總結(jié)
到此這篇關(guān)于Vue實現(xiàn)登錄記住賬號密碼功能的思路與過程的文章就介紹到這了,更多相關(guān)Vue實現(xiàn)登錄記住賬號密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue實現(xiàn)用戶沒有登陸時,訪問后自動跳轉(zhuǎn)登錄頁面的實現(xiàn)思路
- Vue項目如何保持用戶登錄狀態(tài)(localStorage+vuex刷新頁面后狀態(tài)依然保持)
- vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼
- vue 實現(xiàn)用戶登錄方式的切換功能
- VuePress 中如何增加用戶登錄功能
- VUE:vuex 用戶登錄信息的數(shù)據(jù)寫入與獲取方式
- vue同一個瀏覽器登錄不同賬號數(shù)據(jù)覆蓋問題解決方案
- vue?+elementui?項目登錄通過不同賬號切換側(cè)邊欄菜單的顏色
- vue項目實現(xiàn)表單登錄頁保存賬號和密碼到cookie功能
- vue同一瀏覽器登錄多賬號處理方案
相關(guān)文章
vue-router實現(xiàn)組件間的跳轉(zhuǎn)(參數(shù)傳遞)
這篇文章主要為大家詳細介紹了vue-router實現(xiàn)組件間的跳轉(zhuǎn),參數(shù)傳遞方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11vue watch深度監(jiān)聽對象實現(xiàn)數(shù)據(jù)聯(lián)動效果
這篇文章主要介紹了vue watch深度監(jiān)聽對象實現(xiàn)數(shù)據(jù)聯(lián)動的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08關(guān)于Vue中echarts響應式頁面變化resize()的用法介紹
Vue項目中開發(fā)數(shù)據(jù)大屏,使用echarts圖表根據(jù)不同尺寸的屏幕進行適配,resize()可以調(diào)用echarts中內(nèi)置的resize函數(shù)進行自適應縮放,本文將給大家詳細介紹resize()的用法,需要的朋友可以參考下2023-06-06