限制每个角色的路由
Restrict routing per role
是否可以根据登录用户角色等值限制某些路由和/或更改任何路由的组件?
我想要这样管理员的默认页面不同于普通用户,但如果在浏览器中手动设置路由,也可能限制访问。
或者有一个重定向到另一个路由的基本组件是更好的解决方案吗?
我知道通过路由器限制访问并不能取代真实帐户的安全性,但似乎是防止用户猜测受限路由的第一步。
下面是一个使用 vue router 实现身份验证和授权的好例子:https://scotch.io/tutorials/vue-authentication-and-route-handling-using-vue-router
基本上你可以在让用户打开受保护的组件之前检查权限。实现此目的的最简单方法是使用路由器防护。在您的路由器定义中:
{
path: '/proctected',
beforeEnter(to, from, next) {
if (isAuthenticated()) {
if (!hasPermissionsNeeded(to)) {
next('/page-to-show-for-no-permission');
} else {
next();
}
} else {
next('/page-to-show-for-unauthenticated-users');
}
}
}
这名守卫将防止进入 /proctected
url。在这里你可以检查工作代码笔:https://codepen.io/anon/pen/JwxoMe
下面是所有路由的守卫示例:
router.beforeEach((to, from, next) => {
if (isAuthenticated()) {
if (!hasPermissionsNeeded(to)) {
next('/page-to-show-for-no-permission');
} else {
next();
}
} else {
next('/page-to-show-for-unauthenticated-users');
}
})
有关路由器防护的更多信息:https://router.vuejs.org/guide/advanced/navigation-guards.html#per-route-guard
是否可以根据登录用户角色等值限制某些路由和/或更改任何路由的组件?
我想要这样管理员的默认页面不同于普通用户,但如果在浏览器中手动设置路由,也可能限制访问。
或者有一个重定向到另一个路由的基本组件是更好的解决方案吗?
我知道通过路由器限制访问并不能取代真实帐户的安全性,但似乎是防止用户猜测受限路由的第一步。
下面是一个使用 vue router 实现身份验证和授权的好例子:https://scotch.io/tutorials/vue-authentication-and-route-handling-using-vue-router
基本上你可以在让用户打开受保护的组件之前检查权限。实现此目的的最简单方法是使用路由器防护。在您的路由器定义中:
{
path: '/proctected',
beforeEnter(to, from, next) {
if (isAuthenticated()) {
if (!hasPermissionsNeeded(to)) {
next('/page-to-show-for-no-permission');
} else {
next();
}
} else {
next('/page-to-show-for-unauthenticated-users');
}
}
}
这名守卫将防止进入 /proctected
url。在这里你可以检查工作代码笔:https://codepen.io/anon/pen/JwxoMe
下面是所有路由的守卫示例:
router.beforeEach((to, from, next) => {
if (isAuthenticated()) {
if (!hasPermissionsNeeded(to)) {
next('/page-to-show-for-no-permission');
} else {
next();
}
} else {
next('/page-to-show-for-unauthenticated-users');
}
})
有关路由器防护的更多信息:https://router.vuejs.org/guide/advanced/navigation-guards.html#per-route-guard