欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue 權(quán)限認(rèn)證token的實現(xiàn)方法

 更新時間:2018年07月17日 14:17:54   作者:明媚jm靜好  
這篇文章主要介紹了vue 權(quán)限認(rèn)證token的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近搞一個vue的項目,接口帶了權(quán)限驗證,于是乎稍微研究了一下,中間遇到的各種坑都來源于自己概念的不熟悉。

主要呢是分兩步:

一是vue路由層的控制,由于項目的路由有規(guī)律可循,所以沒有采用網(wǎng)上requireAuth那種在需要加驗證的路由上配置meta(具體見:http://www.dbjr.com.cn/article/143928.htm)

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const router = new Router({...})

router.beforeEach((to, from, next) => {
 if(/^\/[S|B|V]/.test(to.path)){
  if (isLogin()) {//判斷token信息的自寫方法
   next();
  }
  else {
   next({ name: 'login' })//跳轉(zhuǎn)到登錄頁
  }
 }
 else {
  next();
 }
})

二是http 攔截器 ,統(tǒng)一處理所有http請求和響應(yīng),就得用上 axios 的攔截器。

import axios from 'axios'
// http request 攔截器
axios.interceptors.request.use(function (config) {
  config.headers.token = sessionStorage.getItem("user_token")//將接口返回的token信息配置到接口請求中
  return config;
}, function (error) {
  return Promise.reject(error);
});
// http response 攔截器
axios.interceptors.response.use(function(response){
  if(response.data.code=='1001'||response.data.code=='1002'){//具體的判斷token失效的參數(shù)
    sessionStorage.setItem("user_token",'')
    sessionStorage.setItem("LoginUser",'{}')
    alert(response.data.msg);
    window.location.href='/#/login'//需求方要求一旦出錯立即跳轉(zhuǎn)登錄,所以采取這種侵入式的手段。
  }else{
    return response
  }
}, function (error) {
  return Promise.reject(error);
});

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論