-1

在我的 Angular 5.1.1 应用程序中,不会触发单个组件上的 canActivate 防护。当我路由到“用户”时,永远不会触发 AuthenticationGuard,但它适用于所有其他路径。

我认为这与“用户”用于重定向到路径为空时有关。所以我将 redirectTo 属性更改为客户端。第一次没有触发 AuthGuard,但下一次总是。对于“用户”,它仍然没有被触发。

路线

const routes: Routes = [
    { path: 'error', component: ErrorComponent /* , canActivate: [AuthenticationGuard] */  },
    { path: 'id_token', component: OAuthCallbackComponent, canActivate: [OAuthCallbackHandler] },                   // reply URL AAD
    { path: 'client', component: ClientsComponent, canActivate: [AuthenticationGuard] },
    { path: 'colleagues', component: ColleaguesComponent, canActivate: [AuthenticationGuard]},
    { path: 'user', component: UserComponent, canActivate: [AuthenticationGuard] },
    { path: 'useroverview', component: UserOverviewComponent, canActivate: [AuthenticationGuard] },
    { path: 'login', component: LoginComponent },
    { path: '', redirectTo: 'user', pathMatch: 'full', canActivate: [AuthenticationGuard] },
    { path: 'loading', component: OAuthCallbackComponent },
    { path: 'noaccess', component: NoAccessComponent },
    { path: '**', component: NoAccessComponent }
];

AuthGuard

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        const navigationExtras: NavigationExtras = {
            queryParams: { 'redirectUrl': route.url }
        };

        if (!this.adalService.userInfo || !this.adalService.accessToken) {
            sessionStorage.clear();
            sessionStorage.setItem('redirect', this.setRedirectUrl(route.url));
            this.router.navigate(['login'], navigationExtras);
            return false;
        }

        if (((route.routeConfig.path === "client" || route.routeConfig.path === "useroverview") && UserRights[sessionStorage.getItem('Role')] < 3) ||
            ((sessionStorage.getItem('UserType') === '66' || sessionStorage.getItem('UserType') === '74') && route.routeConfig.path === "colleagues") && UserRights[sessionStorage.getItem('Role')] === 0)
        {
            this.router.navigate(['404']);
            return false;
        }

        if (sessionStorage.length === 0) {
            this.spinner.show();
            return this.userManService.getUserInfo().switchMap(resp => {
                this.sessionUser(resp);
                return this.userManService.getMyAccountData().map(data => {
                    this.setUserInfo(data);
                    $(document).trigger('Authenticated');
                    this.spinner.hide();
                    this.myMenuOption();
                    return true;
                }).catch(() => {
                    this.spinner.hide();
                    return Observable.of(false);
                });
            }).catch(() => {
                this.spinner.hide();
                return Observable.of(false);
            });
        }
        else
            return Observable.of(true);
    }

没有为该组件触发 AuthGuard 怎么可能?

4

1 回答 1

1

我错过了一个非常明显的事情(太愚蠢了!)。在用户路径上,我还分配了孩子。所以当我在那里设置 canActivate 保护时,它工作得很好。

const routes: Routes = [{
path: 'user',
component: UserComponent,
canActivate: [AuthenticationGuard],
children: [
    { path: 'invite/:key', component: ModalContainerComponent, canActivate: [AuthenticationGuard] }
]
}]
于 2019-07-26T07:17:31.000 回答