fix(router): 修复路由守卫中未授权访问检查逻辑
处理当 to 对象缺少 matched 属性时的边界情况,确保授权检查逻辑的健壮性
Showing
2 changed files
with
9 additions
and
3 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2026-01-19 14:11:06 | 4 | + * @LastEditTime: 2026-01-20 10:18:33 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -118,7 +118,12 @@ export const checkAuth = (to) => { | ... | @@ -118,7 +118,12 @@ export const checkAuth = (to) => { |
| 118 | return to.path.startsWith(route.path) | 118 | return to.path.startsWith(route.path) |
| 119 | }) | 119 | }) |
| 120 | // 方式二:读取路由元信息 requiresAuth(推荐) | 120 | // 方式二:读取路由元信息 requiresAuth(推荐) |
| 121 | - const needAuthByMeta = to.matched.some(record => record.meta && record.meta.requiresAuth === true) | 121 | + // 注意:axios 拦截器中调用时,to 可能不是完整的 Route 对象,可能缺少 matched 属性 |
| 122 | + // 如果没有 matched 属性,则回退到仅依赖 path 的白名单匹配 | ||
| 123 | + const needAuthByMeta = to.matched | ||
| 124 | + ? to.matched.some(record => record.meta && record.meta.requiresAuth === true) | ||
| 125 | + : false; | ||
| 126 | + | ||
| 122 | const needAuth = needAuthByList || needAuthByMeta | 127 | const needAuth = needAuthByList || needAuthByMeta |
| 123 | 128 | ||
| 124 | if (needAuth && !currentUser) { | 129 | if (needAuth && !currentUser) { | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-12-31 14:33:46 | 4 | + * @LastEditTime: 2026-01-20 10:23:54 |
| 5 | * @FilePath: /mlaj/src/router/routes.js | 5 | * @FilePath: /mlaj/src/router/routes.js |
| 6 | * @Description: 路由地址映射配置 | 6 | * @Description: 路由地址映射配置 |
| 7 | */ | 7 | */ |
| ... | @@ -289,6 +289,7 @@ export const routes = [ | ... | @@ -289,6 +289,7 @@ export const routes = [ |
| 289 | component: () => import('@/views/study/studyDetailPage.vue'), | 289 | component: () => import('@/views/study/studyDetailPage.vue'), |
| 290 | meta: { | 290 | meta: { |
| 291 | title: '学习详情页面', | 291 | title: '学习详情页面', |
| 292 | + requiresAuth: true, | ||
| 292 | } | 293 | } |
| 293 | }, | 294 | }, |
| 294 | { | 295 | { | ... | ... |
-
Please register or login to post a comment