亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

主要呢是分兩步:

一是vue路由層的控制,由于項(xiàng)目的路由有規(guī)律可循,所以沒有采用網(wǎng)上requireAuth那種在需要加驗(yàn)證的路由上配置meta(具體見:http://chabaoo.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請(qǐng)求和響應(yīng),就得用上 axios 的攔截器。

import axios from 'axios'
// http request 攔截器
axios.interceptors.request.use(function (config) {
  config.headers.token = sessionStorage.getItem("user_token")//將接口返回的token信息配置到接口請(qǐng)求中
  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'//需求方要求一旦出錯(cuò)立即跳轉(zhuǎn)登錄,所以采取這種侵入式的手段。
  }else{
    return response
  }
}, function (error) {
  return Promise.reject(error);
});

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

相關(guān)文章

最新評(píng)論