新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
基于vue實現(xiàn)token驗證的實例代碼
vue-koa2-token
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了德陽免費建站歡迎大家使用!
基于vue的 做了token驗證
前端部分(對axios設(shè)置Authorization)
import axios from 'axios' import store from '../store' import router from '../router' //設(shè)置全局axios默認值 axios.defaults.timeout = 6000; //6000的超時驗證 axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; //創(chuàng)建一個axios實例 const instance = axios.create(); instance.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; axios.interceptors.request.use = instance.interceptors.request.use; //request攔截器 instance.interceptors.request.use( config => { //每次發(fā)送請求之前檢測都vuex存有token,那么都要放在請求頭發(fā)送給服務(wù)器 if(store.state.token){ config.headers.Authorization = `token ${store.state.token}`; } return config; }, err => { return Promise.reject(err); } ); //respone攔截器 instance.interceptors.response.use( response => { return response; }, error => { //默認除了2XX之外的都是錯誤的,就會走這里 if(error.response){ switch(error.response.status){ case 401: store.dispatch('UserLogout'); //可能是token過期,清除它 router.replace({ //跳轉(zhuǎn)到登錄頁面 path: 'login', query: { redirect: router.currentRoute.fullPath } // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由 }); } } return Promise.reject(error.response); } ); export default instance;
然后在路由文件中
//注冊全局鉤子用來攔截導航 router.beforeEach((to, from, next) => { //獲取store里面的token let token = store.state.token; //判斷要去的路由有沒有requiresAuth if(to.meta.requiresAuth){ if(token){ next(); }else{ next({ path: '/login', query: { redirect: to.fullPath } // 將剛剛要去的路由path(卻無權(quán)限)作為參數(shù),方便登錄成功后直接跳轉(zhuǎn)到該路由 }); } }else{ next();//如果無需token,那么隨它去吧 } });
后端(node) 我們封裝了一個中間件 在需要驗證token的路由,加上這個中間件
router.get('/dosh',checkToken,User.dosh) const jwt = require('jsonwebtoken');
1、使用jsonwebtoken 創(chuàng)建token
const jwt = require('jsonwebtoken'); //登錄時:核對用戶名和密碼成功后,應(yīng)用將用戶的id(圖中的user_id)作為JWT Payload的一個屬性 module.exports = function(user_id){ const token = jwt.sign({ user_id: user_id }, 'sinner77', { expiresIn: '3600s' //過期時間設(shè)置為60妙。那么decode這個token的時候得到的過期時間為 : 創(chuàng)建token的時間 + 設(shè)置的值 }); return token; };
2、對于前端的請求,校驗接口
//檢查token是否過期 module.exports = async ( ctx, next ) => { if(ctx.request.header['authorization']){ let token = ctx.request.header['authorization'].split(' ')[1]; //解碼token let decoded = jwt.decode(token, 'sinner77'); //console.log(decoded);的輸出 :{ user_id: '123123123', iat: 1494405235, exp: 1494405235 } if(token && decoded.exp <= new Date()/1000){ ctx.status = 401; ctx.body = { message: 'token過期' }; }else{ //如果權(quán)限沒問題,那么交個下一個控制器處理 return next(); } }else{ ctx.status = 401; ctx.body = { message: '沒有token' } } };
代碼托管github 歡迎star
https://github.com/yxl720/vue-koa2-token
總結(jié)
以上所述是小編給大家介紹的基于vue 實現(xiàn)token驗證的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
當前文章:基于vue實現(xiàn)token驗證的實例代碼
當前路徑:http://fisionsoft.com.cn/article/jgdhhc.html