- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有延迟加载模块的 Angular 4.3.6 应用程序。这是部分根路由器:
const routes: Routes = [
{ path: '', redirectTo: 'fleet', pathMatch: 'full' },
{
path: '',
component: AppComponent,
canActivate: [AuthenticationGuard],
children: [
{
path: 'fleet',
loadChildren: "./modules/fleet.module",
canActivate: [AuthenticationGuard]
},
{
path: 'password/set',
loadChildren: "./modules/chooseNewPassword.module",
canActivate: [ChoosePasswordGuard]
}
]
}
]
// Exports RouterModule.forRoot(routes, { enableTracing: true });
这两个示例模块中的我的子路由器:
舰队:
RouterModule.forChild([
{
path: '',
component: FleetComponent,
canActivate: [AuthenticationGuard]
}
]);
选择新密码:
RouterModule.forChild([
{
path: '',
component: ChooseNewPasswordComponent,
canActivate: [ChoosePasswordGuard]
}
]);
AuthenticationGuard
调用如下所示的方法:
return this.getUserSession().map((userSession: UserSession) => {
if (userSession && userSession.ok) {
return true;
}
else if (userSession && userSession.expired) {
this.router.navigate(['password/set'])
.catch((e: Error) => console.error(e));
return true;
}
else {
window.location.replace('/');
return false;
}
}
因此,如果用户的 session 正常,就会激活该路由。如果用户的密码已过期,则会将用户重定向到选择新密码模块。如果没有 session ,则重定向到登录。
ChoosePasswordGuard
执行类似的操作,但仅保护选择新密码组件(一般使用不同的工具来设置密码):
return this.getUserSession().map((userSession: UserSession) => {
if (userSession) {
return userSession.expired;
}
else {
return false;
}
});
这在模块分割之前有效。
现在,我陷入了重定向循环。打开路由器跟踪后,我观察到以下顺序。用户登录,AuthenticationGuard
更正重定向到/password/set 模块,并移交给 ChooseNewPasswordGuard
:
并且这个循环会重复。
(如果我将整个 ChooseNewPasswordGuard 替换为 return Observable.of(true);
,它也会重复)
编辑:即使我在 URL 栏中提供 /#/password/set
,我也会被重定向到根页面 (/
)...
问题:
既然模块是延迟加载的,我在路由器或防护装置中做错了什么来强制此循环?我对 shouldActivate: true
后跟 NavigationCancel Reason: ""
感到特别困惑。
这是否与我直接在 AuthenticationGuard 中重定向有关,现在该防护已应用于我的主空根路由 ({ path: '', redirectTo: 'fleet', pathMatch: 'full' }
) 它总是被调用并重定向,即使我设置了路径?
我真的需要在子路由和根路由中重复 canActivate
防护吗?
像往常一样,欢迎提出任何其他意见。
最佳答案
问题是我过度应用了AuthenticationGuard
:它不应该应用于顶级AppComponent,因为它总是会重定向到“选择新密码”模块,即使它是加载该模块。
我的根路线
应该如下所示:
const routes: Routes = [
{ path: '', redirectTo: 'fleet', pathMatch: 'full' },
{
path: '',
component: AppComponent,
// canActivate: [AuthenticationGuard], // <-- Remove this guard
children: [
{
path: 'fleet',
loadChildren: "./modules/fleet.module",
canActivate: [AuthenticationGuard]
},
{
path: 'password/set',
loadChildren: "./modules/chooseNewPassword.module",
canActivate: [ChoosePasswordGuard]
}
]
}
]
(我欢迎并乐意接受更好的解释或更好的AuthenticationGuard模式。)
关于angular - 路由器无限循环,在延迟加载模块上具有第二个 canActivate 防护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058647/
你好,我是 Angular 6 的新手,我遇到了一个关于提取用户的身份验证问题,如果没有登录就不会进入任何页面,我面临的问题是 Class 'NeedAuthGuard' incorrectly im
我写了一个 angular 4.3.0 typescript 库。在构建我的库时,我在 *.d.ts 文件中看到以下错误。 ERROR in [at-loader] ..\myLibrary\lib-
下面是我在 TypeScript 中实现 CanActivate 保护子句,当我编译这段代码时,它显示如下错误 A function whose declared type is neither 'v
我有这个 AuthGuard。在这段代码中,我想创建一个条件。如果我的 resetpasss 不为空,我想在此处的 ResetPassIdComponent 中导航,否则我想在 LoginFirstC
据我了解CanActivate Angular2 中的接口(interface)仅对路由更改有效。因此,如果我有一个要求用户登录的页面 p,我可以创建一个实现 CanActivate 接口(inter
我的路由器中有这些线路: .. canActivate: [MyGuard], path: "search", component: SearchComponent,
我有以下问题。我制作了一个 CanActivate Guard,如果您已登录,您可以“转到”债务组件。我的问题是,当我登录时,我想将用户重定向到债务组件(因为 RegistrationComponen
我正在使用 Observable返回 canActivate() .为测试设置了以下功能,它正确解析,组件显示。 canActivate(route: ActivatedRouteSnapshot,
最近,我一直在思考我正在开发的应用程序的安全性。客户端基于 Angular 构建,后端为 Rails API。据我所知,普遍的共识是,如果它在客户端,假设它可以被破坏。所以这让我想知道我何时以及是否应
我最近被 Angular 路由守卫卡住了。 CanActive 仅在页面加载时运行一次,并且不会在 protected 路由内的路由更改时运行。我认为这已更改,因为它过去常常在每次更改时运行。根据我在
我有这样的路线设置: export const APP_ROUTES: Routes = [ { path: '', resolve: {
我已经为我的 Angular 5.2 应用程序实现了 CanActivate 路由保护。如果它返回 true,则 URL 不会更改,但当它返回 false 时,URL 会更改并将路径附加到 URL。在
使用守卫,我尝试访问服务 但我无法在 canActivate 中返回一个 promise (它具有我无法更改的特定签名) 我的 autService 返回一个 promise ,因为它是异步的 我怎样
我有这样的路线: [ { path: ':orgName', children: [ { path: '', component: Ho
我有这样的路线: [ { path: ':orgName', children: [ { path: '', component: Ho
我有一个路径列表,我想将 CanActivate 应用于除某些路径之外的所有路径。是否有任何 Angular 丢弃某些路径的方法。目前我正在将 canActivate 应用到所有的 URL。如果有很多
I'm loading a books module with the following routing configuration (src/app/index.ts) -- 请注意,这是一个 s
我正在尝试在我的应用程序路由上使用 canActivate 功能,但是每当我编译该应用程序时,日志都会显示不断刷新的失败消息,我已将 console.log() 用于可见性。 我在这里遗漏了什么吗?
我有一个以这种方式使用惰性模块的应用程序: export const routes: Routes = [ { path: '', component: WelcomeCompon
我有一个关于 CanActivate 的问题 import { AuthService } from './auth.service'; import { CanActivate } from '@a
我是一名优秀的程序员,十分优秀!