Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程
實(shí)現(xiàn)思路
用戶登錄時(shí)若勾選“記住我”功能選項(xiàng),則將登錄名和密碼(加密后)保存至本地緩存中,下次登錄頁(yè)面加載時(shí)自動(dòng)獲取保存好的賬號(hào)和密碼(需解密),回顯到登錄輸入框中。
這里有三種方法來存儲(chǔ)賬號(hào)密碼:
1. sessionStorage(不推薦)
1). 僅在當(dāng)前會(huì)話下有效,關(guān)閉瀏覽器窗口后就被清除了
2). 存放數(shù)據(jù)大小一般為5MB
3). 不與服務(wù)器進(jìn)行交互通信
2. localStorage
1). 除非主動(dòng)清除localStorage里的信息,否則將永遠(yuǎn)存在,關(guān)閉瀏覽器窗口后下次啟動(dòng)任然存在
2). 存放數(shù)據(jù)大小一般為5MB
3). 不與服務(wù)器進(jìn)行交互通信
3. cookies
1). 可以手動(dòng)設(shè)置過期時(shí)間,超過有效期則失效。未設(shè)置過期時(shí)間,關(guān)閉瀏覽器窗口后就被清除了
2). 存放數(shù)據(jù)大小一般為4K
3). 每次請(qǐng)求都會(huì)被傳送到服務(wù)器
這里主要介紹第二種和第三種的使用方法。
功能界面
<el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="loginForm demo-ruleForm"> <!-- 賬號(hào) --> <el-form-item label="賬號(hào)" prop="userId" autocomplete="on"> <el-input v-model="loginForm.userId" placeholder="請(qǐng)輸入賬號(hào)"></el-input> </el-form-item> <!-- 密碼 --> <el-form-item label="密碼" prop="password"> <el-input type="password" v-model="loginForm.password" placeholder="請(qǐng)輸入密碼" @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>
記住賬號(hào)密碼功能的具體實(shí)現(xiàn)
密碼加密
為提高安全性,密碼存儲(chǔ)前需進(jì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) { /* ------ 賬號(hào)密碼的存儲(chǔ) ------ */ 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登錄請(qǐng)求 ------ */ } 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) { /* ------ 賬號(hào)密碼的存儲(chǔ) ------ */ if (this.checked) { let password = Base64.encode(this.loginForm.password); // base64加密 this.setCookie(this.loginForm.userId, password, 7); } else { this.setCookie("", "", -1); // 修改2值都為空,天數(shù)為負(fù)1天就好了 } /* ------ http登錄請(qǐng)求 ------ */ } else { console.log("error submit!!"); return false; } }); }, // 設(shè)置cookie setCookie(userId, password, days) { let date = new Date(); // 獲取時(shí)間 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("; "); //分割成一個(gè)個(gè)獨(dú)立的“key=value”的形式 for (let i = 0; i < arr.length; i++) { let arr2 = arr[i].split("="); // 再次切割,arr2[0]為key值,arr2[1]為對(duì)應(yīng)的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實(shí)現(xiàn)登錄記住賬號(hào)密碼功能的思路與過程的文章就介紹到這了,更多相關(guān)Vue實(shí)現(xiàn)登錄記住賬號(hào)密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue實(shí)現(xiàn)用戶沒有登陸時(shí),訪問后自動(dòng)跳轉(zhuǎn)登錄頁(yè)面的實(shí)現(xiàn)思路
- Vue項(xiàng)目如何保持用戶登錄狀態(tài)(localStorage+vuex刷新頁(yè)面后狀態(tài)依然保持)
- vue實(shí)現(xiàn)用戶長(zhǎng)時(shí)間不操作自動(dòng)退出登錄功能的實(shí)現(xiàn)代碼
- vue 實(shí)現(xiàn)用戶登錄方式的切換功能
- VuePress 中如何增加用戶登錄功能
- VUE:vuex 用戶登錄信息的數(shù)據(jù)寫入與獲取方式
- vue同一個(gè)瀏覽器登錄不同賬號(hào)數(shù)據(jù)覆蓋問題解決方案
- vue?+elementui?項(xiàng)目登錄通過不同賬號(hào)切換側(cè)邊欄菜單的顏色
- vue項(xiàng)目實(shí)現(xiàn)表單登錄頁(yè)保存賬號(hào)和密碼到cookie功能
- vue同一瀏覽器登錄多賬號(hào)處理方案
相關(guān)文章
Vue2路由動(dòng)畫效果的實(shí)現(xiàn)代碼
本篇文章主要介紹了Vue2路由動(dòng)畫效果的實(shí)現(xiàn)代碼,可以根據(jù)不同的路徑去改變動(dòng)畫的效果,有興趣的可以了解一下2017-07-07vue父組件向子組件動(dòng)態(tài)傳值的兩種方法
這篇文章主要介紹了vue父組件向子組件動(dòng)態(tài)傳值的兩種方法 ,需要的朋友可以參考下2017-11-11一份超級(jí)詳細(xì)的Vue-cli3.0使用教程【推薦】
這篇文章主要介紹了一份超級(jí)詳細(xì)的Vue-cli3.0使用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive)
在使用服務(wù)器端渲染時(shí),除了服務(wù)端的接口緩存、頁(yè)面緩存、組建緩存等,瀏覽器端也避免不了要使用緩存,減少頁(yè)面的重繪。這篇文章主要介紹了詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive),感興趣的小伙伴們可以參考一下2018-10-10基于vue3+threejs實(shí)現(xiàn)可視化大屏效果
本文主要主要講述對(duì)threejs的一些api進(jìn)行基本的封裝,在vue3項(xiàng)目中來實(shí)現(xiàn)一個(gè)可視化的3d項(xiàng)目,包含了一些常用的功能,場(chǎng)景、燈光、攝像機(jī)初始化,模型、天空盒的加載,以及鼠標(biāo)點(diǎn)擊和懸浮的事件交互,感興趣的朋友可以參考下2023-06-06vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn)(參數(shù)傳遞)
這篇文章主要為大家詳細(xì)介紹了vue-router實(shí)現(xiàn)組件間的跳轉(zhuǎn),參數(shù)傳遞方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11vue如何實(shí)現(xiàn)簡(jiǎn)易的彈出框
這篇文章主要介紹了vue如何實(shí)現(xiàn)簡(jiǎn)易的彈出框,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue watch深度監(jiān)聽對(duì)象實(shí)現(xiàn)數(shù)據(jù)聯(lián)動(dòng)效果
這篇文章主要介紹了vue watch深度監(jiān)聽對(duì)象實(shí)現(xiàn)數(shù)據(jù)聯(lián)動(dòng)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08關(guān)于Vue中echarts響應(yīng)式頁(yè)面變化resize()的用法介紹
Vue項(xiàng)目中開發(fā)數(shù)據(jù)大屏,使用echarts圖表根據(jù)不同尺寸的屏幕進(jìn)行適配,resize()可以調(diào)用echarts中內(nèi)置的resize函數(shù)進(jìn)行自適應(yīng)縮放,本文將給大家詳細(xì)介紹resize()的用法,需要的朋友可以參考下2023-06-06