Vue+Express實現登錄狀態權限控制


前提須知

  • 對Vue全家桶有基本的認知.
  • 擁有node環境
  • 了解express
  • 本篇只是介紹登錄狀態的權限驗證,以及登錄,注銷的前后端http交互.前端布局,后端密碼驗證等以后有時間再對這些內容進行補充.

一丶業務分析

1.什么情況下進行權限驗證?

  • 訪問敏感接口
    • 前端向后端敏感接口發送ajax
    • 后端進行session驗證,並返回信息
    • 前端axios攔截返回信息,根據返回信息進行操作
  • 進行頁面切換
    • 頁面切換,觸發vue-router的路由守衛
    • 路由守衛根據跳轉地址進行驗證,如需權限,則發送ajax至后端驗證接口
    • 后端驗證接口進行session驗證,返回信息
    • 前端根據后端返回信息進行操作

2.前后端進行了怎么的交互?

  • 登錄
  • 注銷

 

腦圖

 

二丶項目環境

三丶項目開始前

1.創建項目目錄,配置路由,創建頁面跳轉組件

  • 項目目錄:
    • 創建components/route_list.vue進行頁面跳轉
  <template>
    <div>
      <p><router-link :to="{name:'index'}">主頁</router-link></p> <p><router-link :to="{name:'login'}">登錄</router-link></p> <p><router-link :to="{name:'logout'}">注銷</router-link></p> <p><router-link :to="{name:'me'}">個人信息</router-link></p> <p>登錄狀態:{{this.$store.state.me.login}}</p> </div> </template> 復制代碼
  • 創建stores/me.js倉庫,存放登錄狀態
import Vue from 'vue' import router from '../router'; export default{ namespaced:true, state:{ login :false }, mutations:{ changeLogin(state,{result}){ state.login = result; } }, actions:{ async checkMe({commit}){ const result = await Vue.prototype.$http.get('/me').then(data=>data.data); if(!result){ router.push({name:'login'}) return } commit('changeLogin',{result}) } } } 復制代碼
  • views中 新建Login,Logout,Signin,me組件
  • 路由信息寫在router.js中
  • 2.配置路由: 引入各個頁面,進行路由跳轉配置

后端配置express-session

 //serve/app.js文件  express服務器  
const express = require('express') //中間件--用於下發session const session = require('express-session') const app = express() //使用express-session下發session app.set('trust proxy', 1) app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: true, })) 復制代碼

四丶權限驗證 - 敏感接口

1.主頁Index.vue--訪問敏感接口,展示敏感接口數據

<template>
    <div>
        //請求后台數據
        <Button @click="getTest">敏感接口</Button> //請求信息展示 {{result}} //頁面跳轉組件 <route_list></route_list> </div> </template> <script> import route_list from '../components/route_list' export default { components:{ route_list }, data(){ return{ result:"" } }, methods:{ //請求敏感接口 async getTest(){ this.result = await this.$http.get("/").then(res=>res.data) } } } </script> 復制代碼

2.后端的敏感接口接收到請求,進行判斷

app.get('/', function (req, res) { if(req.session.login){ res.send("hello world") }else{ res.send(403) } }) 復制代碼

3.axios--發送ajax后,對后端返回數據進行攔截,判斷

import axios from 'axios'; import url from 'url'; import router from '../src/router' //創建axios實例 var instance = axios.create({ baseURL: '/api' }); //攔截器 instance.interceptors.response.use( function(response){ return response; },function(error){ //敏感接口.如果沒有session跳轉登錄界面 if(error.response.status==403){ router.push({name:"login"}) } }) export default instance; 復制代碼

如果返回結果為true,登錄狀態,就可以進行訪問敏感接口了.

五丶權限驗證 - 頁面跳轉

1.路由守衛,對跳轉頁面進行監視

//路由守衛
   router.beforeEach((to,from,next)=>{
  if(to.name != 'login' && to.name != 'index'){ store.dispatch('me/checkMe') } next() }) 復制代碼

2.store中checkme,當跳轉敏感頁面時進行驗證

actions:{
        async checkMe({commit}){
            //請求/me接口,對登錄信息進行判斷,並保留狀態
            const result = await Vue.prototype.$http.get('/me').then(data=>data.data); //返回值為false,跳轉至login if(!result){ router.push({name:'login'}) return } commit('changeLogin',{result}) } } 復制代碼

3.后端的檢測登錄接口接收到請求,進行判斷

//驗證是否登錄
app.get('/me', function (req, res) { //判斷session是否為true if(req.session.login){ res.send(true) }else{ res.send(false) } }) 復制代碼

如果返回結果為true,登錄狀態,就可以進行頁面跳轉了.

六丶登錄

1.登錄頁Login.vue - 請求登錄接口,登錄成功后將信息保存到store

<template>
    <div>
<h1>登錄</h1>
<Button @click="login">登錄</Button> <route_list></route_list> </div> </template> <script> import route_list from '../components/route_list' export default { components:{ route_list }, methods:{ async login(){ //請求登錄接口 const result = await this.$http.get("/login").then(data=>data.data); //記錄登錄狀態 this.$store.commit('me/changeLogin',{result}) } }, } </script> 復制代碼

2.后端登錄接口接收到請求,生成session

//登錄接口,更改session狀態
app.get('/login', function (req, res) { req.session.login = true, res.send(true) }) 復制代碼

現在就是登錄狀態了

七丶注銷

1.登錄頁Logout.vue - 請求注銷接口,注銷成功后將信息保存到store

<template>
    <div>
        <h1>注銷</h1>
<Button @click="login">注銷</Button> <route_list></route_list> </div> </template> <script> import route_list from '../components/route_list' export default { components:{ route_list }, data(){ return{ } }, methods:{ async login(){ const result = await this.$http.get("/logout").then(data=>data.data); this.$store.commit('me/changeLogin',{result}) } }, } </script> 復制代碼

2.后端注銷接口接收到請求,更改session狀態

//登錄接口,更改session狀態
app.get('/login', function (req, res) { req.session.login = false, res.send(false) }) 復制代碼

現在就是注銷狀態了

完成展示(gif,可能加載不出來):

 

done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM