feat(权限): 实现基于角色的路由权限控制

- 新增路由元信息类型定义 (requiresAuth, roles, title)
- 实现路由守卫中的角色权限校验逻辑
- 新增 403 禁止访问页面
- 提取权限校验函数 checkRoutePermission,提高可测试性
- 修复 JSON.parse 异常处理,增强健壮性
- 优化页面标题动态设置

测试优化:
- 重构 global-setup.ts,支持 JAR 文件启动后端服务
- 优化测试用例等待逻辑,减少硬编码延迟
- 简化 playwright 配置,移除多浏览器支持
- 新增路由权限守卫单元测试

关联需求:权限系统完善
This commit is contained in:
张翔
2026-04-08 15:29:03 +08:00
parent 9b2c8a47a4
commit 7420afa380
23 changed files with 933 additions and 349 deletions
+72 -16
View File
@@ -1,71 +1,98 @@
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
declare module 'vue-router' {
interface RouteMeta {
requiresAuth?: boolean
roles?: string[]
title?: string
}
}
const routes: RouteRecordRaw[] = [
{
path: '/login',
name: 'Login',
component: () => import('@/views/system/Login.vue')
component: () => import('@/views/system/Login.vue'),
meta: { title: '登录' }
},
{
path: '/403',
name: 'Forbidden',
component: () => import('@/views/system/Forbidden.vue'),
meta: { title: '无权限' }
},
{
path: '/',
component: () => import('@/layouts/DefaultLayout.vue'),
redirect: '/dashboard',
meta: { requiresAuth: true },
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: () => import('@/views/system/Dashboard.vue')
component: () => import('@/views/system/Dashboard.vue'),
meta: { title: '仪表盘' }
},
{
path: 'users',
name: 'UserManagement',
component: () => import('@/views/system/UserManagement.vue')
component: () => import('@/views/system/UserManagement.vue'),
meta: { title: '用户管理' }
},
{
path: 'roles',
name: 'RoleManagement',
component: () => import('@/views/system/RoleManagement.vue')
component: () => import('@/views/system/RoleManagement.vue'),
meta: { title: '角色管理' }
},
{
path: 'menus',
name: 'MenuManagement',
component: () => import('@/views/system/MenuManagement.vue')
component: () => import('@/views/system/MenuManagement.vue'),
meta: { title: '菜单管理' }
},
{
path: 'sys/config',
name: 'ConfigManagement',
component: () => import('@/views/config/ConfigManagement.vue')
component: () => import('@/views/config/ConfigManagement.vue'),
meta: { title: '参数配置' }
},
{
path: 'dict',
name: 'DictManagement',
component: () => import('@/views/config/DictManagement.vue')
component: () => import('@/views/config/DictManagement.vue'),
meta: { title: '字典管理' }
},
{
path: 'files',
name: 'FileManagement',
component: () => import('@/views/file/FileManagement.vue')
component: () => import('@/views/file/FileManagement.vue'),
meta: { title: '文件管理' }
},
{
path: 'notice',
name: 'NoticeManagement',
component: () => import('@/views/notify/NoticeManagement.vue')
component: () => import('@/views/notify/NoticeManagement.vue'),
meta: { title: '通知公告' }
},
{
path: 'loginlog',
name: 'LoginLog',
component: () => import('@/views/audit/LoginLog.vue')
component: () => import('@/views/audit/LoginLog.vue'),
meta: { title: '登录日志' }
},
{
path: 'oplog',
name: 'OperationLog',
component: () => import('@/views/audit/OperationLog.vue')
component: () => import('@/views/audit/OperationLog.vue'),
meta: { title: '操作日志' }
},
{
path: 'exceptionlog',
name: 'ExceptionLog',
component: () => import('@/views/audit/ExceptionLog.vue')
component: () => import('@/views/audit/ExceptionLog.vue'),
meta: { title: '异常日志' }
}
]
}
@@ -76,9 +103,29 @@ const router = createRouter({
routes
})
router.beforeEach((to, from, next) => {
function checkRoutePermission(route: RouteLocationNormalized, userRoles: string[]): boolean {
if (!route.meta.roles || !Array.isArray(route.meta.roles) || route.meta.roles.length === 0) {
return true
}
return route.meta.roles.some((role: string) => userRoles.includes(role))
}
router.beforeEach((to, _from, next) => {
try {
const token = localStorage.getItem('token')
const rolesStr = localStorage.getItem('roles')
let userRoles: string[] = []
try {
userRoles = rolesStr ? JSON.parse(rolesStr) : []
} catch (e) {
console.warn('解析用户角色失败,将使用空数组:', e)
userRoles = []
}
if (to.meta.title) {
document.title = `${to.meta.title} - Novalon 管理系统`
}
if (to.path === '/login') {
if (token) {
@@ -86,12 +133,21 @@ router.beforeEach((to, from, next) => {
} else {
next()
}
} else if (to.path === '/403') {
next()
} else {
if (token) {
next()
} else {
if (to.meta.requiresAuth !== false && !token) {
next('/login')
return
}
if (!checkRoutePermission(to, userRoles)) {
console.warn(`用户角色 ${userRoles} 无权访问路由 ${to.path},需要角色: ${to.meta.roles}`)
next('/403')
return
}
next()
}
} catch (error) {
console.error('路由守卫错误:', error)