Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟
前言
在我們進(jìn)行登錄的時(shí)候,為了保護(hù)系統(tǒng)的安全性,也是防止惡意進(jìn)行登錄,進(jìn)入到后臺(tái)盜取數(shù)據(jù),因此我們?cè)诘卿浀沫h(huán)節(jié)進(jìn)行一定的安全保護(hù),提升系統(tǒng)安全等級(jí)。在此我們運(yùn)用阿拉伯?dāng)?shù)字和英文字母進(jìn)行4位數(shù)的驗(yàn)證碼校驗(yàn),當(dāng)然也可以進(jìn)行多位字符進(jìn)行校驗(yàn),在此我使用生成4位驗(yàn)證碼進(jìn)行校驗(yàn)
驗(yàn)證碼:前端綁定后端生成得驗(yàn)證碼圖片,前端提交表單信息到后端進(jìn)行驗(yàn)證,后端驗(yàn)證碼存入session
一、環(huán)境準(zhǔn)備:
我這里由于本機(jī)上的還是vue 2 所以這邊還是vue2開發(fā)的,而且主要是好嫖組件,畢竟不是專業(yè)的前端。這里的話由于某些原因,我這里使用的包管理是 cnpm不為別的就為了安裝的時(shí)候不會(huì)被雷到。這里先裝一個(gè) vuex 用來管理狀態(tài),怎么裝一條命令的事情。
二、功能展示:

三、項(xiàng)目結(jié)構(gòu):

四、驗(yàn)證碼生成:

<template>
<div class="ValidCode disabled-select"
:style="`width:${width}; height:${height}`"
@click="refreshCode">
<span v-for="(item, index) in codeList"
:key="index"
:style="getStyle(item)">
{{item.code}}
</span>
</div>
</template>
<script>
export default {
name: "ValidCode",
model: {
prop: 'value',
event: 'input'
},
props: {
width: {
type: String,
default: '100px'
},
height: {
type: String,
default: '34px'
},
length: {
type: Number,
default: 4
}
},
data () {
return {
codeList: []
}
},
mounted () {
this.createdCode()
},
methods: {
refreshCode () {
this.createdCode()
},
createdCode () {
const len = this.length
const codeList = []
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789'
const charsLen = chars.length
// 生成
for (let i = 0; i < len; i++) {
const rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
codeList.push({
code: chars.charAt(Math.floor(Math.random() * charsLen)),
color: `rgb(${rgb})`,
fontSize: `1${[Math.floor(Math.random() * 10)]}px`,
padding: `${[Math.floor(Math.random() * 10)]}px`,
transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
})
}
// 指向
this.codeList = codeList
// 將當(dāng)前數(shù)據(jù)派發(fā)出去
this.$emit('input', codeList.map(item => item.code).join(''))
},
getStyle (data) {
return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
}
}
}
</script>
<style scoped >
.ValidCode{
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
span{
display: inline-block;
}
}
</style>五、驗(yàn)證碼使用:
1.首先需要解決的是登錄的跨域問題,使用我們的axios進(jìn)行解決:

import axios from 'axios'
import {serverIp} from "../../public/config";
const request = axios.create({
baseURL: `http://localhost:9090`, // 注意?。?這里是全局統(tǒng)一加上了 '/api' 前綴,也就是說所有接口都會(huì)加上'/api'前綴在,頁面里面寫接口的時(shí)候就不要加 '/api'了,否則會(huì)出現(xiàn)2個(gè)'/api',類似 '/api/api/user'這樣的報(bào)錯(cuò),切記?。。?
timeout: 5000
})
// request 攔截器
// 可以自請(qǐng)求發(fā)送前對(duì)請(qǐng)求做一些處理
// 比如統(tǒng)一加token,對(duì)請(qǐng)求參數(shù)統(tǒng)一加密
request.interceptors.request.use(config => {
config.headers['Content-Type'] = 'application/json;charset=utf-8';
let user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
if (user) {
config.headers['token'] = user.token; // 設(shè)置請(qǐng)求頭
}
return config
}, error => {
return Promise.reject(error)
});
// response 攔截器
// 可以在接口響應(yīng)后統(tǒng)一處理結(jié)果
request.interceptors.response.use(
response => {
let res = response.data;
// 如果是返回的文件
if (response.config.responseType === 'blob') {
return res
}
// 兼容服務(wù)端返回的字符串?dāng)?shù)據(jù)
if (typeof res === 'string') {
res = res ? JSON.parse(res) : res
}
return res;
},
error => {
console.log('err' + error) // for debug
return Promise.reject(error)
}
)
export default request
2.在需要的頁面進(jìn)行引入

<script>
import request from "@/utils/request";
import ValidCode from "@/components/ValidCode";
const Base64 = require('js-base64').Base64
export default {
name: "Login",
components: {
ValidCode
},
data() {
return {
form: {
sex: 1,
username: '',
password: '',
},
checked: false,
rules: {
username: [
{required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur'},
{min: 3, max: 10, message: "長度在3到10個(gè)字符", trigger: "blur"},
],
password: [
{required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
{min: 1, max: 20, message: '長度在 6 到 11 個(gè)字符', trigger: 'blur'}
],
},
validCode: '',
}
},
}
</script>3.驗(yàn)證碼校驗(yàn)


<ek-form-item>
<div style="display: flex">
<el-input v-model="form.validCode" style="width: 60%" placeholder="請(qǐng)輸入驗(yàn)證碼" size="medium"></el-input>
<ValidCode @input="createValidCode" />
</div>
</ek-form-item>export default {
name: "Login",
components: {
ValidCode
},
data() {
return {
form: {
sex: 1,
username: '',
password: '',
},
checked: false,
rules: {
username: [
{required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur'},
{min: 3, max: 10, message: "長度在3到10個(gè)字符", trigger: "blur"},
],
password: [
{required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
{min: 1, max: 20, message: '長度在 6 到 11 個(gè)字符', trigger: 'blur'}
],
},
validCode: '',
}
},
}4.完整代碼
<template>
<div style="height: 100vh; overflow: hidden">
<div style="height: 50px;
line-height: 50px;
border-bottom: 2px solid var(--colorRed);
padding-left: 20px;
color: var(--colorRed)">
<b style="font-size: 24px;">xx宿舍</b>
<i style="margin-left: 20px">-- 只為更好的你</i>
</div>
<div style="width: 50%;
margin: 50px auto;
border-radius: 10px;
box-shadow: 0 0 10px -2px cornflowerblue;
display: flex">
<div style="flex: 1; padding: 50px 50px">
<img src="../assets/images/學(xué)習(xí).png" alt="" style="width: 100%;">
</div>
<div style="flex: 1; padding: 20px">
<div class="form-toggle">
<b >賬號(hào)登錄</b>
</div>
<el-form ref="form" :model="form" size="normal" :rules="rules" >
<el-form-item prop="username" class="props">
<el-input placeholder="賬號(hào)" clearable v-model="form.username" prefix-icon="el-icon-user" />
</el-form-item>
<el-form-item prop="password" class="props">
<el-input placeholder="密碼" v-model="form.password" show-password prefix-icon="el-icon-lock" />
</el-form-item>
<ek-form-item>
<div style="display: flex">
<el-input v-model="form.validCode" style="width: 60%" placeholder="請(qǐng)輸入驗(yàn)證碼" size="medium"></el-input>
<ValidCode @input="createValidCode" />
</div>
</ek-form-item>
<el-form-item style="padding-top:15px">
<el-button type="primary" style="width: 100%" @click="login" round>登 錄</el-button>
</el-form-item>
<!-- <el-form-item>-->
<div style="margin: 10px 0; text-align: right;">
<a href="/register" rel="external nofollow" style="color: var(--colorRed)">立即注冊(cè)</a>
</div>
<!-- <el-button style="width: 100%" @click="register" round>點(diǎn)擊注冊(cè)</el-button>-->
<!-- </el-form-item>-->
</el-form>
</div>
</div>
</div>
</template>
<script>
import request from "@/utils/request";
import ValidCode from "@/components/ValidCode";
const Base64 = require('js-base64').Base64
export default {
name: "Login",
components: {
ValidCode
},
data() {
return {
form: {
sex: 1,
username: '',
password: '',
},
checked: false,
rules: {
username: [
{required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur'},
{min: 3, max: 10, message: "長度在3到10個(gè)字符", trigger: "blur"},
],
password: [
{required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
{min: 1, max: 20, message: '長度在 6 到 11 個(gè)字符', trigger: 'blur'}
],
},
validCode: '',
}
},
mounted () {
let username = localStorage.getItem('username')
if (username) {
this.form.username = localStorage.getItem('username')
this.form.password = Base64.decode(localStorage.getItem('password'))// base64解密
this.checked = true
}
},
created() {
sessionStorage.removeItem("user")
},
methods: {
register:function(){
this.$router.push("/register");
},
//接收驗(yàn)證碼組件提交的4位驗(yàn)證碼
createValidCode(data) {
this.validCode = data
},
login() {
this.$refs['form'].validate((valid) => {
if (valid) {
if (!this.form.validCode) {
this.$message.error("請(qǐng)?zhí)顚戲?yàn)證碼")
return
}
if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
this.$message.error("驗(yàn)證碼錯(cuò)誤")
return
}
request.post("/user/login", this.form).then(res => {
if (res.code === '200'){
// sessionStorage.setItem("user", JSON.stringify(res.data))//緩存用戶信息
localStorage.setItem("user", JSON.stringify(res.data))
if (res.data.role === 'USER') {
this.$router.push("/")
} else {
this.$router.push("/mall/index")
}
this.$message({
type: "success",
message: "登錄成功"
})
} else {
this.$message({
type: "error",
message: res.msg
})
}
})
}
});
},
}
}
</script>
<style scoped>
.form-toggle {
margin: 20px 0;
text-align: center
}
.form-toggle b {
font-size: 20px;
cursor: pointer;
}
</style>附送
全局定義css并在main.js里面引入
import './assets/css/global.css'

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.ml-5 {
margin-left: 5px;
}
.ml-10 {
margin-left: 10px;
}
.mr-5 {
margin-right: 5px;
}
.pd-10 {
padding: 10px;
}
body {
margin: 0;
padding: 0;
font-size: 14px;
color: #666;
--colorRed: orangered;
}
a {
text-decoration: none;
}小結(jié)
以上就是對(duì)Vue實(shí)現(xiàn)路由導(dǎo)航簡單的概述,使得我們的項(xiàng)目更加的趨于完美,也提升了我們對(duì)于編程的能力和思維!
到此這篇關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的文章就介紹到這了,更多相關(guān)Vue驗(yàn)證碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)登錄頁面的驗(yàn)證碼以及驗(yàn)證過程解析(面向新手)
- vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
- Vue 實(shí)現(xiàn)登錄界面驗(yàn)證碼功能
- Vue實(shí)現(xiàn)手機(jī)號(hào)、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))
- vue實(shí)現(xiàn)登錄時(shí)的圖片驗(yàn)證碼
- vue實(shí)現(xiàn)登錄驗(yàn)證碼
- vue實(shí)現(xiàn)登錄時(shí)圖形驗(yàn)證碼
- vue前端實(shí)現(xiàn)login頁登陸驗(yàn)證碼代碼示例
相關(guān)文章
SpringMVC4 + MyBatis3 + SQL Server 2014整合教程(含增刪改查分頁)
這篇文章主要給大家介紹了關(guān)于SpringMVC4 + MyBatis3 + SQL Server 2014整合的相關(guān)資料,文中包括介紹了增刪改查分頁等相關(guān)內(nèi)容,通過示例代碼介紹的非常詳細(xì),分享出來供大家參考學(xué)習(xí),下面來一起看看吧。2017-06-06
詳解SpringBoot中的統(tǒng)一功能處理的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)統(tǒng)一功能處理,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定借鑒價(jià)值,需要的可以參考一下2023-01-01
javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁面重復(fù)登錄問題的解決方法
這篇文章主要為大家詳細(xì)介紹了javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁面重復(fù)登錄問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
java實(shí)現(xiàn)多層級(jí)zip解壓的示例代碼
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多層級(jí)zip解壓的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-12-12
Java實(shí)現(xiàn)分庫分表實(shí)踐指南
在開發(fā)中我們經(jīng)常使用到分庫分表,但是一般是我們前期就已經(jīng)做了規(guī)劃,對(duì)數(shù)據(jù)庫怎么劃分,對(duì)哪些表進(jìn)行分表,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)分庫分表的相關(guān)資料,需要的朋友可以參考下2024-01-01
新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法
這篇文章主要介紹了新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

