身份验证保护重定向在子路由上不起作用

时间:2020-04-13 19:02:44

标签: angular

我是angular的新手,我开发了一个应用程序并按如下方式处理我的登录服务:

export class AfterLoginService implements CanActivate {

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean>
{
    if (!this._tokenService.loggedIn())
        this.router.navigate(['login']);

    return this._tokenService.loggedIn();
}

我的路线是这样的:

import {BeforeLoginService} from './services/before-login.service';
import {AfterLoginService} from './services/after-login.service';

export const routes: Routes = [
{
 path: '',
 redirectTo: 'dashboard',
 pathMatch: 'full',
 canActivate:[AfterLoginService]
},
{
 path: 'login',
 component: LoginComponent,
 canActivate:[BeforeLoginService]
},
{
 path: '',
 component: DefaultLayoutComponent,
 canActivate:[AfterLoginService],
 data: {
  title: 'Home'
 },
 children: [
  {
    path: 'users',
    loadChildren: () => import('./views/users/users.module').then(m => m.UsersModule)
   },
   {
     path: 'buttons',
     loadChildren: () => import('./views/buttons/buttons.module').then(m => m.ButtonsModule)
    }
    // others child routes
   ]
  },
  { path: '**', component: P404Component }
];

现在,如果我尝试不登录而访问受保护的路径(例如,/ buttons),它会将我重定向到登录名,但是如果我输入“ / buttons / any”之类的内容,则会显示以下内容:

enter image description here

当我尝试不登录就访问子路由时,中间件未重定向我,子路由如下:

import { ButtonsComponent } from './buttons.component';
import { DropdownsComponent } from './dropdowns.component';
import { BrandButtonsComponent } from './brand-buttons.component';

const routes: Routes = [
  {
    path: '',
    data: {
      title: 'Buttons'
    },
    children: [
      {
        path: '',
        redirectTo: 'buttons'
      },
      {
        path: 'buttons',
        component: ButtonsComponent,
        data: {
          title: 'Buttons'
        }
      },
      {
        path: 'dropdowns',
        component: DropdownsComponent,
        data: {
          title: 'Dropdowns'
        }
      },
      {
        path: 'brand-buttons',
        component: BrandButtonsComponent,
        data: {
          title: 'Brand buttons'
        }
      }
    ]
  }
];

儿童路线应该遵守中间件并将我重定向到登录名,但是我不知道自己缺少什么,就像我尝试访问一些不存在的路线(/ bad_path / any)一样,它显示出相同的内容的东西,并且没有将我重定向到通过通配符路径设置的404页面,我丢失了什么?

2 个答案:

答案 0 :(得分:1)

对于必须使用 CanActivateChild 的孩子,请尝试以下操作:

df_f = (df.set_index(['ID', 'SUBID', 'date'])
          .rename_axis(columns='col').stack()
          .reset_index('date', name='val')
          .groupby(['ID', 'SUBID', 'col'])
          .agg(date=('date','last'), prev=('val','first'), new=('val','last'))
          .query('prev != new').reset_index())

print (df_f)
   ID  SUBID   col      date  prev  new
0   1      1  val3  20200414     3    4
1   2      1  val1  20200416     7    8
2   2      1  val2  20200416     8    9

caActivateChild在angular.io上的定义

一个类可以实现作为确定是否孩子的守卫的接口 路线可以被激活。如果所有守卫都返回true,导航将 继续。如果任何防护措施返回假,导航将被取消。如果 任何后卫都会返回UrlTree,当前导航将被取消, 新的导航将启动到从返回的UrlTree 警卫队。

答案 1 :(得分:0)

有点晚了,错误是我网站的基本URL不像/(标记包含href以及另一个我不记得的URL),因此角度定位策略不起作用,这是唯一的我所做的就是将基本网址更改为/,以便所有相对网址都可以通过pathLocationStrategy

处理