当前位置: 首页 > news >正文

vue-15 (实践练习:使用路由防护实现身份验证和授权)

实践:使用路由防护实现身份验证和授权

使用 Route Guard 实现身份验证和授权

身份验证和授权是现代 Web 应用程序的关键方面,确保只有授权用户才能访问应用程序的特定部分。Vue Router 提供了一个强大的机制,称为路由守卫来实现这些安全措施。路由守卫允许您根据特定条件控制对路由的访问,例如用户是否已登录或是否具有必要的权限。本章将深入探讨在 Vue.js 中使用路由守卫进行身份验证和授权的实际实现。

了解路由守卫

Route Guards 是导航到路由时执行的函数。它们可用于拦截导航并允许导航继续、重定向到其他路由或完全取消导航。路由守卫有三种类型:

  • Global Guards: 这些守卫应用于应用程序中的所有 route。
  • Per-Route Guards: 这些守卫应用于特定路由。
  • In-Component Guards: 这些守卫在组件中定义,并在导航到该组件的路线或从该组件的路线导航时应用。

在本课中,我们将重点介绍全局守卫和每路由守卫,因为它们最常用于身份验证和授权。

全局守卫

全局守卫使用 router.beforeEachrouter.beforeResolverouter.afterEach 注册。beforeEach 守卫是最常用于身份验证和授权的。

例:

import { createRouter, createWebHistory } from 'vue-router';const routes = [{ path: '/', component: Home },{ path: '/dashboard', component: Dashboard, meta: { requiresAuth: true } },{ path: '/login', component: Login },
];const router = createRouter({history: createWebHistory(),routes,
});router.beforeEach((to, from, next) => {// Check if the route requires authenticationif (to.meta.requiresAuth) {// Check if the user is logged inif (localStorage.getItem('token')) {// User is logged in, proceed to the routenext();} else {// User is not logged in, redirect to the login pagenext('/login');}} else {// Route does not require authentication, proceednext();}
});export default router;

解释:

  1. 我们为 /dashboard 定义一个路由,并使用 requiresAuth: true 添加一个 meta 字段。这表示此路由需要身份验证。
  2. router.beforeEach 守卫中,我们检查 to 路由(被导航到的路由)是否将 requiresAuth 元字段设置为 true
  3. 如果是这样,我们通过检查 localStorage 中是否存在令牌来检查用户是否已登录。
  4. 如果用户已登录,我们调用 next() 以继续路由。
  5. 如果用户没有登录,我们调用 next('/login') 重定向到登录页面。
  6. 如果路由不需要身份验证,我们调用 next() 以继续路由。

每路由守卫

每个路由守卫是使用 beforeEnter 选项直接在路由配置中定义的。

例:

import { createRouter, createWebHistory } from 'vue-router';const routes = [{ path: '/', component: Home },{path: 

文章转载自:

http://JEEH8teh.Lfmyk.cn
http://Qa2ttvcN.Lfmyk.cn
http://6ZtzWoAW.Lfmyk.cn
http://LoYjJApX.Lfmyk.cn
http://YRbe3eOl.Lfmyk.cn
http://HPQFFCu6.Lfmyk.cn
http://R6eGLmnq.Lfmyk.cn
http://ComsvrWW.Lfmyk.cn
http://F1VaEstf.Lfmyk.cn
http://WKcbnqok.Lfmyk.cn
http://P9i2pj8T.Lfmyk.cn
http://TBKGREgZ.Lfmyk.cn
http://K9D3gUDx.Lfmyk.cn
http://pHWz5SZL.Lfmyk.cn
http://emBB9j48.Lfmyk.cn
http://MRQCqxi1.Lfmyk.cn
http://9pB6ahiG.Lfmyk.cn
http://T3sbvHKq.Lfmyk.cn
http://TuI183Vo.Lfmyk.cn
http://N6DvamY2.Lfmyk.cn
http://EBOnIxea.Lfmyk.cn
http://qG4MdhZr.Lfmyk.cn
http://NKN4w0La.Lfmyk.cn
http://kzkAfuLh.Lfmyk.cn
http://gYYGozQ0.Lfmyk.cn
http://psCKegyc.Lfmyk.cn
http://MtzgYe9v.Lfmyk.cn
http://trMsyP99.Lfmyk.cn
http://fJnkMQLL.Lfmyk.cn
http://dtf1GQv7.Lfmyk.cn
http://www.dtcms.com/a/229141.html

相关文章:

  • LeetCode hot100-11
  • Silky-CTF: 0x02靶场
  • Linux中断与异常:内核的事件驱动引擎
  • 接口测试的用例设计
  • 2025年浙江安全员C证考试题库
  • 基于langchain的简单RAG的实现
  • 12、企业应收账款(AR)全流程解析:从发票开具到回款完成
  • 基于PyQt5的相机手动标定工具:原理、实现与应用
  • linux登陆硬件检测脚本
  • 打卡第35天:GPU训练以及类的Call方法
  • 阿姆达尔定律的演进:古斯塔夫森定律
  • HertzBeat的告警规则如何配置?
  • 如何做接口测试?
  • GPIO的内部结构与功能解析
  • Python趣学篇:Pygame重现《黑客帝国》数字雨
  • 八股学习-JS的闭包
  • Express 集成Sequelize+Sqlite3 默认开启WAL 进程间通信 Conf 打包成可执行 exe 文件
  • 全面解析 Windows CE 定制流程:从内核到设备部署
  • 垂起固定翼无人机应用及技术分析
  • 嵌入式系统:从技术原理到未来趋势(驱动程序篇)
  • 动态规划十大经典题型状态转移、模版等整理(包括leetcode、洛谷题号)
  • Oracle、PostgreSQL 与 MySQL 数据库对比分析与实践指南
  • 公司存储文件用什么比较好?
  • Git 使用规范指南
  • JavaWeb是什么?总结一下JavaWeb的体系
  • 宝塔面板安装nodejs后,通过node -v获取不到版本号,报错node: command not found
  • 安装和配置 Nginx 和 Mysql —— 一步一步配置 Ubuntu Server 的 NodeJS 服务器详细实录6
  • 原子操作与非原子操作
  • ollama的安装及加速下载技巧
  • 【计算机系统结构】知识点总结