我有一个有两名canActivate守卫的路线(AuthGuard和RoleGuard).first(AuthGuard)检查用户是否已登录,如果没有,则重定向到登录页面.第二个检查用户是否具有允许查看页面的角色定义,如果没有,则重定向到未授权页面.
canActivate: [ AuthGuard, RoleGuard ]
...
export class AuthGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
...
this.router.navigate(['/login']);
resolve(false);
}
export class RoleGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
...
this.router.navigate(['/unauthorized']);
resolve(false);
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我访问路线并且我没有登录时,我点击了AuthGuard,它失败并告诉路由器导航到/login.然而,即使AuthGuard失败RoleGuard了,然后运行然后导航到/unauthorized.
在我看来,如果第一个失败,那么运行下一个后卫是毫无意义的.有没有办法强制执行此行为?
我的应用程序允许基于用户角色访问内容.我为每个角色写了一个Router Guard.某些内容允许访问role1或role2或role3.我应该如何在feature-routing.module.ts文件中编写canActivate声明?据我了解,如果我写
canActivate:[Role1Guard, Role2Guard, Role3Guard]
Run Code Online (Sandbox Code Playgroud)
如果任何警卫返回false,则拒绝访问.但在我的情况下,如果任何一个守卫返回true,我应该允许访问.怎么做?提前谢谢了!
可以在阵列中定义Angular 2路由器防护装置.例如:
<code>
canActivate: ['CanAlwaysActivateGuard','AuthGuard']
</code>
Run Code Online (Sandbox Code Playgroud)
以下是我的问题:
如果我在路线上指定三名警卫,似乎所有警卫都会立即进行评估.
{path: '', component: OptionsComponent, canActivate: [ GuardOne, GuardTwo, GuardThree]}
我遇到的问题是我不希望GuardTwo在GuardOne完成之前运行.有没有办法实现这个目标?