From c08d9af5e3ddbabfc8e439d50a83b544e36cf5d1 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 7 Oct 2026 10:22:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=8C=89=20requiresAuth=20=E8=87=AA=E5=8A=A8=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=20#3391?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原 protectedRoutes 数组只覆盖 8 项,9 个显式标 requiresAuth 的路由只有 2 个被守卫。改成 to.matched.some(r => r.meta.requiresAuth) 自动收口,anon 白名单按 name 反向匹配避免误伤登录/注册/找回密码页。注意 dict/management 等路由当前未标 requiresAuth,按 DoD 不动路由表,本 PR 不覆盖它们;完整修复需后续给这些路由补标记。 --- main/manager-web/src/router/index.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/main/manager-web/src/router/index.js b/main/manager-web/src/router/index.js index bb45b9110..c85ac9eee 100644 --- a/main/manager-web/src/router/index.js +++ b/main/manager-web/src/router/index.js @@ -228,20 +228,19 @@ VueRouter.prototype.push = function push(location) { }) } -// 需要登录才能访问的路由 -const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig', 'KnowledgeBaseManagement', 'KnowledgeFileUpload', 'AddressBookManagement'] +// 任何路由段上 meta.requiresAuth === true 都视为需要登录 +// 匿名白名单按 name 匹配,避免被误标时也拦住 anon 路由 +const anonRouteNames = new Set(['login', 'Register', 'RetrievePassword']) -// 路由守卫 router.beforeEach((to, from, next) => { - // 检查是否是需要保护的路由 - if (protectedRoutes.includes(to.name)) { - // 从localStorage获取token - const token = localStorage.getItem('token') - if (!token) { - // 未登录,跳转到登录页 - next({ name: 'login', query: { redirect: to.fullPath } }) - return - } + if (anonRouteNames.has(to.name)) { + next() + return + } + const requiresAuth = to.matched.some(r => r.meta && r.meta.requiresAuth === true) + if (requiresAuth && !localStorage.getItem('token')) { + next({ name: 'login', query: { redirect: to.fullPath } }) + return } next() })