新聞中心
vue-koa2-token

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


咨詢(xún)
建站咨詢(xún)
