最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟

 更新時(shí)間:2023年09月17日 16:22:24   作者:Blet-  
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的超詳細(xì)步驟,我們?cè)谑褂胿ue進(jìn)行前端開發(fā)時(shí)都需要登錄驗(yàn)證,文中通過(guò)代碼示例介紹的非常詳細(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 用來(lái)管理狀態(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.首先需要解決的是登錄的跨域問(wèn)題,使用我們的axios進(jìn)行解決:

import axios from 'axios'
import {serverIp} from "../../public/config";
const request = axios.create({
    baseURL: `http://localhost:9090`,  // 注意!! 這里是全局統(tǒng)一加上了 '/api' 前綴,也就是說(shuō)所有接口都會(huì)加上'/api'前綴在,頁(yè)面里面寫接口的時(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.在需要的頁(yè)面進(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: "長(zhǎng)度在3到10個(gè)字符", trigger: "blur"},
        ],
        password: [
          {required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
          {min: 1, max: 20, message: '長(zhǎng)度在 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: "長(zhǎng)度在3到10個(gè)字符", trigger: "blur"},
        ],
        password: [
          {required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
          {min: 1, max: 20, message: '長(zhǎng)度在 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: "長(zhǎng)度在3到10個(gè)字符", trigger: "blur"},
        ],
        password: [
          {required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'},
          {min: 1, max: 20, message: '長(zhǎng)度在 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)航簡(jiǎn)單的概述,使得我們的項(xiàng)目更加的趨于完美,也提升了我們對(duì)于編程的能力和思維!

到此這篇關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的文章就介紹到這了,更多相關(guān)Vue驗(yàn)證碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring設(shè)置攔截器代碼實(shí)例

    spring設(shè)置攔截器代碼實(shí)例

    這篇文章主要介紹了spring設(shè)置攔截器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 基于spring boot實(shí)現(xiàn)一個(gè)全局異常處理器

    基于spring boot實(shí)現(xiàn)一個(gè)全局異常處理器

    在項(xiàng)目開發(fā)中,我們可以基于spring boot提供的切面特性,來(lái)很輕松的實(shí)現(xiàn)全局異常的處理,所以本文主要為大家介紹了如何基于spring boot實(shí)現(xiàn)一個(gè)全局異常處理器,有需要的可以參考下
    2023-09-09
  • Java中的OkHttp使用教程

    Java中的OkHttp使用教程

    OkHttp是目前非?;鸬木W(wǎng)絡(luò)庫(kù),OKHttp與HttpClient類似,也是一個(gè)Http客戶端,提供了對(duì)?HTTP/2?和?SPDY?的支持,并提供了連接池,GZIP?壓縮和?HTTP?響應(yīng)緩存功能,本文重點(diǎn)給大家介紹Java?OkHttp使用,感興趣的朋友一起看看吧
    2022-04-04
  • Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法實(shí)例分析

    Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法實(shí)例分析

    這篇文章主要介紹了Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法,以實(shí)例形式較為詳細(xì)的分析了Java Spring開發(fā)中針對(duì)數(shù)據(jù)庫(kù)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 基于Spring Boot的Environment源碼理解實(shí)現(xiàn)分散配置詳解

    基于Spring Boot的Environment源碼理解實(shí)現(xiàn)分散配置詳解

    這篇文章主要給大家介紹了基于Spring Boot的Environment源碼理解實(shí)現(xiàn)分散配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • java如何實(shí)現(xiàn)批量修改文件類型

    java如何實(shí)現(xiàn)批量修改文件類型

    在文件管理,媒體處理,數(shù)據(jù)遷移等各種業(yè)務(wù)場(chǎng)景中,經(jīng)常會(huì)遇到“批量修改文件類型”這一需求,本文將詳細(xì)介紹如何使用Java實(shí)現(xiàn)批量修改文件類型,有需要的小伙伴可以參考一下
    2025-06-06
  • Java獲取文件路徑的幾種方式匯總

    Java獲取文件路徑的幾種方式匯總

    本文介紹了使用Java處理文件路徑的方法,包括使用java.io.File、java.nio.file.Paths類處理本地文件和類路徑資源,以及在Web應(yīng)用中獲取路徑的方法,它還提供了最佳實(shí)踐和注意事項(xiàng),如使用現(xiàn)代化的路徑處理方式和跨平臺(tái)兼容性,需要的朋友可以參考下
    2026-04-04
  • 詳解Spring中Bean后置處理器(BeanPostProcessor)的使用

    詳解Spring中Bean后置處理器(BeanPostProcessor)的使用

    BeanPostProcessor 接口也被稱為Bean后置處理器,通過(guò)該接口可以自定義調(diào)用初始化前后執(zhí)行的操作方法。本文將詳細(xì)講講它的使用,需要的可以參考一下
    2022-06-06
  • SpringMVC中RequestBody注解的List參數(shù)傳遞方式

    SpringMVC中RequestBody注解的List參數(shù)傳遞方式

    這篇文章主要介紹了SpringMVC中RequestBody注解的List參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • idea插件訪問(wèn)瀏覽器web地址實(shí)現(xiàn)方式

    idea插件訪問(wèn)瀏覽器web地址實(shí)現(xiàn)方式

    用戶分享了在IntelliJ IDEA開發(fā)插件的簡(jiǎn)要流程:新建項(xiàng)目、配置plugin.xml、創(chuàng)建工廠類、構(gòu)建打包、安裝并重啟IDE,最后運(yùn)行插件,重點(diǎn)在于通過(guò)Idea平臺(tái)實(shí)現(xiàn)插件開發(fā),與Eclipse流程類似但需注意配置細(xì)節(jié)
    2025-09-09

最新評(píng)論

库伦旗| 信丰县| 新兴县| 龙陵县| 西充县| 伊金霍洛旗| 辉县市| 阿合奇县| 安图县| 澜沧| 彩票| 彝良县| 石阡县| 阿坝县| 六盘水市| 姚安县| 长沙市| 大埔县| 德昌县| 延吉市| 高碑店市| 梁河县| 青田县| 新竹市| 福安市| 麻栗坡县| 绥中县| 台江县| 浮山县| 郯城县| 扶余县| 伊川县| 三河市| 金川县| 彭阳县| 日喀则市| 嘉定区| 徐闻县| 土默特左旗| 临城县| 济南市|