如何在所有路由上应用canActivate guard?
How to apply canActivate guard on all the routes?
我有一个 angular2 active guard,如果用户未登录,它会处理,将其重定向到登录页面:
import { Injectable } from "@angular/core";
import { CanActivate , ActivatedRouteSnapshot, RouterStateSnapshot, Router} from "@angular/router";
import {Observable} from "rxjs";
import {TokenService} from "./token.service";
@Injectable()
export class AuthenticationGuard implements CanActivate {
constructor (
private router : Router,
private token : TokenService
) { }
/**
* Check if the user is logged in before calling http
*
* @param route
* @param state
* @returns {boolean}
*/
canActivate (
route : ActivatedRouteSnapshot,
state : RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
if(this.token.isLoggedIn()){
return true;
}
this.router.navigate(['/login'],{ queryParams: { returnUrl: state.url }});
return;
}
}
我必须在每条路线上实施它,例如:
const routes: Routes = [
{ path : '', component: UsersListComponent, canActivate:[AuthenticationGuard] },
{ path : 'add', component : AddComponent, canActivate:[AuthenticationGuard]},
{ path : ':id', component: UserShowComponent },
{ path : 'delete/:id', component : DeleteComponent, canActivate:[AuthenticationGuard] },
{ path : 'ban/:id', component : BanComponent, canActivate:[AuthenticationGuard] },
{ path : 'edit/:id', component : EditComponent, canActivate:[AuthenticationGuard] }
];
有没有更好的方法来实现 canActive 选项而不将其添加到每个路径。
我想要的是在主路由上添加它,它应该适用于所有其他路由。我搜索了很多,但找不到任何有用的解决方案
谢谢
你可以引入一个无组件的父路由并在那里应用守卫:
const routes: Routes = [
{path: '', canActivate:[AuthenticationGuard], children: [
{ path : '', component: UsersListComponent },
{ path : 'add', component : AddComponent},
{ path : ':id', component: UserShowComponent },
{ path : 'delete/:id', component : DeleteComponent },
{ path : 'ban/:id', component : BanComponent },
{ path : 'edit/:id', component : EditComponent }
]}
];
我认为你应该实现 "child routing",它允许你有一个 parent(例如路径 "admin")和他的 child。
然后您可以对 parent 应用 canactivate,这将自动限制对他的所有 child 的访问。例如,如果我想访问 "admin/home",我需要通过 canActivate 保护的 "admin"。如果你想要
,你甚至可以定义一个带有空路径 "" 的 parent
您还可以在 app.component 的 ngOnInit 函数中订阅路由器的路由更改,并从那里检查身份验证,例如
this.router.events.subscribe(event => {
if (event instanceof NavigationStart && !this.token.isLoggedIn()) {
this.router.navigate(['/login'],{ queryParams: { returnUrl: state.url}});
}
});
我更喜欢这种在路由更改时进行任何类型的应用范围检查的方式。
我有一个 angular2 active guard,如果用户未登录,它会处理,将其重定向到登录页面:
import { Injectable } from "@angular/core";
import { CanActivate , ActivatedRouteSnapshot, RouterStateSnapshot, Router} from "@angular/router";
import {Observable} from "rxjs";
import {TokenService} from "./token.service";
@Injectable()
export class AuthenticationGuard implements CanActivate {
constructor (
private router : Router,
private token : TokenService
) { }
/**
* Check if the user is logged in before calling http
*
* @param route
* @param state
* @returns {boolean}
*/
canActivate (
route : ActivatedRouteSnapshot,
state : RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
if(this.token.isLoggedIn()){
return true;
}
this.router.navigate(['/login'],{ queryParams: { returnUrl: state.url }});
return;
}
}
我必须在每条路线上实施它,例如:
const routes: Routes = [
{ path : '', component: UsersListComponent, canActivate:[AuthenticationGuard] },
{ path : 'add', component : AddComponent, canActivate:[AuthenticationGuard]},
{ path : ':id', component: UserShowComponent },
{ path : 'delete/:id', component : DeleteComponent, canActivate:[AuthenticationGuard] },
{ path : 'ban/:id', component : BanComponent, canActivate:[AuthenticationGuard] },
{ path : 'edit/:id', component : EditComponent, canActivate:[AuthenticationGuard] }
];
有没有更好的方法来实现 canActive 选项而不将其添加到每个路径。
我想要的是在主路由上添加它,它应该适用于所有其他路由。我搜索了很多,但找不到任何有用的解决方案
谢谢
你可以引入一个无组件的父路由并在那里应用守卫:
const routes: Routes = [
{path: '', canActivate:[AuthenticationGuard], children: [
{ path : '', component: UsersListComponent },
{ path : 'add', component : AddComponent},
{ path : ':id', component: UserShowComponent },
{ path : 'delete/:id', component : DeleteComponent },
{ path : 'ban/:id', component : BanComponent },
{ path : 'edit/:id', component : EditComponent }
]}
];
我认为你应该实现 "child routing",它允许你有一个 parent(例如路径 "admin")和他的 child。
然后您可以对 parent 应用 canactivate,这将自动限制对他的所有 child 的访问。例如,如果我想访问 "admin/home",我需要通过 canActivate 保护的 "admin"。如果你想要
,你甚至可以定义一个带有空路径 "" 的 parent您还可以在 app.component 的 ngOnInit 函数中订阅路由器的路由更改,并从那里检查身份验证,例如
this.router.events.subscribe(event => {
if (event instanceof NavigationStart && !this.token.isLoggedIn()) {
this.router.navigate(['/login'],{ queryParams: { returnUrl: state.url}});
}
});
我更喜欢这种在路由更改时进行任何类型的应用范围检查的方式。