溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

用代碼實(shí)現(xiàn)vue路由權(quán)限校驗(yàn)功能

發(fā)布時(shí)間:2020-07-18 10:10:33 來源:億速云 閱讀:188 作者:小豬 欄目:web開發(fā)

這篇文章主要為大家展示了用代碼實(shí)現(xiàn)vue路由權(quán)限校驗(yàn)功能,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

引言

做后臺系統(tǒng)的時(shí)候,難免會有用戶權(quán)限的判斷。admin可以查看全部菜單,user只能查看部分菜單。

一開始接觸這個(gè)需求的時(shí)候,完全是純前端做的。在配置路由的時(shí)候,加一個(gè)roles的屬性,通過判斷用戶的roles是否與路由的roles屬性相匹配來作為顯示隱藏的依據(jù)

{
 path: '/router',
 name: 'router',
 meta: {
 title: '標(biāo)題',
 roles: ['admin','user']
 },
 component: index,
 children: [
 {
 path: 'children',
 name: 'children',
 meta: {
 title: '子標(biāo)題',
 roles: ['admin','user']
 },
 component: child
 }
 ]
}
// 過濾路由 menuList-菜單 roles-用戶角色
const checkMenuList = (menuList, roles) => {
 for (let i = 0; i < menuList.length; i++) {
 if (!compareEqual(roles, menuList[i].meta.roles) || menuList[i].meta.noRenderTree) {
 menuList.splice(i, 1)
 i -= 1
 } else {
 if (menuList[i].children) {
 checkMenuList(menuList[i].children, roles)
 }
 }
 }
 return menuList
}

這樣做確實(shí)可以實(shí)現(xiàn)給不同用戶展示不同的菜單。但是如果用戶權(quán)限發(fā)生改變,前端就需要發(fā)版。本著萬物皆可靈活配置的原則。

需求

首先我們要了解,我們要做什么。

我們希望我們可以通過用戶權(quán)限配置功能,達(dá)到靈活配置路由權(quán)限,由服務(wù)器端返回需要展示的路由權(quán)限,前端做展示。

思路

  • 前端配置好項(xiàng)目全部頁面的路由
  • 服務(wù)器端返回該用戶的權(quán)限列表,前端去匹配,最后返回一個(gè)路由列表,作為菜單
  • 為了更好的用戶體驗(yàn),當(dāng)用戶輸入異常的路由時(shí),我們要重定向到一個(gè)404頁面,提示用戶該頁面不存在。
  • 基于第3點(diǎn),我們在每次跳轉(zhuǎn)的時(shí)候,還需要判斷這個(gè)頁面是否存在,該用戶是否有權(quán)限進(jìn)行跳轉(zhuǎn)

實(shí)現(xiàn)

ok 思路整理完了。現(xiàn)在就開始來實(shí)現(xiàn)吧!

首先,routers是需要在前端注冊,我們要先配置整個(gè)頁面的routers。

除了系統(tǒng)的菜單之外,我們還需要配置403錯(cuò)誤頁面,還有l(wèi)ogin、首頁這些基本路由。由于系統(tǒng)菜單還需要與服務(wù)器端返回的路由列表進(jìn)行匹配,暫時(shí)不進(jìn)行注冊

// router.js

 // 基本路由
export const defaultRouter = [
 { path: '/', component: index }, // 首頁
 { path: '/login', name: 'login', component: login } // 登錄頁
 ]
 
 // 項(xiàng)目全部頁面
export const appRouter = [
 {
 path: '/router1',
 name: 'router1',
 redirect: '/router1/test1',
 component: router1,
 meta: { title: '路由1'},
 children: [
 { path: 'test1', name: 'test1', component: test1, meta: { title: '測試1' } },
 { path: 'test2', name: 'test2', component: test1, meta: { title: '測試2' } }
 ]
 },
 ]
 // 這個(gè)是我們頁面初始化時(shí)候,注冊的routes
const routers = [ 
 ...defaultRouter
]
const RouterConfig = {
 routes: routers
}
const router = new VueRouter(RouterConfig)

全部路由都注冊完了,接下來就要匹配用戶可訪問的路由了,這一步需要和服務(wù)器端一起約定規(guī)則。

// 服務(wù)器端返回的鍵值對: 路由名:是否有權(quán)限
authRouter:{
 'test1': false,
 'test2': true
}

拿到服務(wù)器端返回的用戶權(quán)限之后,就開始過濾路由

// 過濾路由 appRouterCopy-項(xiàng)目全部頁面 authRouter-權(quán)限列表
const checkMenuList = (appRouterCopy, authRouter) => {
 for (let i = 0; i < appRouterCopy.length; i++) {
 let {name, children} = appRouterCopy[i]
 if (authRouter[name] === false) {
  appRouterCopy.splice(i, 1)
  i--
 } else if (children && children.length) {
  checkMenuList(children, authRouter)
 }
 }
}

得到過濾后的路由之后,使用addRoutes進(jìn)行注冊。注意404路由配置要在最后加上。

let error404Page = { path: '/*', name: 'error-404', meta: 
{ 
title: '404-頁面不存在'}, component: error404Page
}
router.addRoutes([...appRouterCopy, error404Page])

到此我們就得到了用戶有權(quán)限的頁面了,可以把得到的列表作為系統(tǒng)菜單渲染上去。接下來就要處理一下跳轉(zhuǎn)異常的狀況了。需要用到beforeEach對每次跳轉(zhuǎn)進(jìn)行攔截判斷

router.beforeEach(((to, from, next) => {
 if (isNotLog && to.name !== 'login') {
 // 未登錄 跳轉(zhuǎn)到登錄頁
  next({ name: 'login' })
 } else if (to.name && (to.name === 'login' || to.name.indexOf('error') !== -1)){
 // 跳轉(zhuǎn)異常
 next()
 } else {
 // 校驗(yàn)用戶權(quán)限
 checkUser(next, to ,router)
 }
})

const checkUser = async (next, to ,router) => {
 if (isNotUser) {
 // 首次登陸系統(tǒng),沒有用戶信息的時(shí)候 需要獲取用戶信息做過濾路由的操作
 const authRouter = getAuthRouter() // 獲取用戶權(quán)限
 checkMenuList(appRouterCopy, authRouter)
 const error404Page = { path: '/*', name: 'error-404', meta: { title: '404-頁面不存在'}, component: error404Page}
 router.addRoutes([...appRouterCopy, error404Page])
 if (!appRouterCopy.length) {
  // 用戶沒有有權(quán)限的路由 可以跳轉(zhuǎn)到404或者登錄頁
  next({ ...error404Page, replace: true })
 } else {
  next({ ...to, replace: true })
 }
 } else {
 next()
 }
}

以上就是關(guān)于用代碼實(shí)現(xiàn)vue路由權(quán)限校驗(yàn)功能的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI