Angular 重定向到登录页面

Angular redirect to login page

我来自 Asp.Net MVC 世界,在该世界中,试图访问未经授权的页面的用户会自动重定向到登录页面。

我正在尝试在 Angular 上重现此行为。我遇到了 @CanActivate 装饰器,但它导致组件根本不呈现,没有重定向。

我的问题如下:

更新: 我在 Github 上发布了一个完整的框架 Angular 2 project with OAuth2 integration,其中显示了下面提到的指令的实际操作。

一种方法是使用 directive。与 Angular 2 components 不同,它们基本上是您插入到页面中的新 HTML 标签(带有相关代码),属性指令是您放入标签中的属性,它会导致一些要发生的行为。 Docs here.

您的自定义属性的存在会导致您放置指令的组件(或 HTML 元素)出现问题。考虑一下我在当前 Angular2/OAuth2 应用程序中使用的指令:

import {Directive, OnDestroy} from 'angular2/core';
import {AuthService} from '../services/auth.service';
import {ROUTER_DIRECTIVES, Router, Location} from "angular2/router";

@Directive({
    selector: '[protected]'
})
export class ProtectedDirective implements OnDestroy {
    private sub:any = null;

    constructor(private authService:AuthService, private router:Router, private location:Location) {
        if (!authService.isAuthenticated()) {
            this.location.replaceState('/'); // clears browser history so they can't navigate with back button
            this.router.navigate(['PublicPage']);
        }

        this.sub = this.authService.subscribe((val) => {
            if (!val.authenticated) {
                this.location.replaceState('/'); // clears browser history so they can't navigate with back button
                this.router.navigate(['LoggedoutPage']); // tells them they've been logged out (somehow)
            }
        });
    }

    ngOnDestroy() {
        if (this.sub != null) {
            this.sub.unsubscribe();
        }
    }
}

这利用我编写的身份验证服务来确定用户是否已经登录并且还订阅身份验证事件以便它可以将用户踢出如果他或她退出或超时。

你也可以做同样的事情。您可以像我一样创建一个指令来检查是否存在必要的 cookie 或其他表明用户已通过身份验证的状态信息。如果他们没有您正在寻找的那些标志,请将用户重定向到您的主 public 页面(就像我一样)或您的 OAuth2 服务器(或其他)。您可以将该指令属性放在需要保护的任何组件上。在这种情况下,它可能被称为 protected 就像我在上面粘贴的指令中一样。

<members-only-info [protected]></members-only-info>

那么您可能希望 navigate/redirect 用户在您的应用程序中进入登录视图,并在那里处理身份验证。您必须将当前路线更改为您想要执行的路线。因此,在这种情况下,您将使用依赖注入在指令的 constructor() 函数中获取 Router object,然后使用 navigate() 方法将用户发送到您的登录页面(如我的示例所示以上)。

这假设您在某处有一系列路由控制 <router-outlet> 标签,看起来像这样,也许:

@RouteConfig([
    {path: '/loggedout', name: 'LoggedoutPage', component: LoggedoutPageComponent, useAsDefault: true},
    {path: '/public', name: 'PublicPage', component: PublicPageComponent},
    {path: '/protected', name: 'ProtectedPage', component: ProtectedPageComponent}
])

相反,如果您需要将用户重定向到 外部 URL,例如您的 OAuth2 服务器,那么您可以让指令执行类似以下:

window.location.href="https://myserver.com/oauth2/authorize?redirect_uri=http://myAppServer.com/myAngular2App/callback&response_type=code&client_id=clientId&scope=my_scope

与最终路由器的用法

随着新路由器的引入,保护路由变得更加容易。你必须定义一个守卫,它充当一个服务,并将其添加到路由中。

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { UserService } from '../../auth';

@Injectable()
export class LoggedInGuard implements CanActivate {
  constructor(user: UserService) {
    this._user = user;
  }

  canActivate() {
    return this._user.isLoggedIn();
  }
}

现在将 LoggedInGuard 传递给路由,并将其添加到模块的 providers 数组中。

import { LoginComponent } from './components/login.component';
import { HomeComponent } from './components/home.component';
import { LoggedInGuard } from './guards/loggedin.guard';

const routes = [
    { path: '', component: HomeComponent, canActivate: [LoggedInGuard] },
    { path: 'login', component: LoginComponent },
];

模块声明:

@NgModule({
  declarations: [AppComponent, HomeComponent, LoginComponent]
  imports: [HttpModule, BrowserModule, RouterModule.forRoot(routes)],
  providers: [UserService, LoggedInGuard],
  bootstrap: [AppComponent]
})
class AppModule {}

详细博客 post 关于它如何与最终版本一起工作:https://medium.com/@blacksonic86/angular-2-authentication-revisited-611bf7373bf9

使用已弃用的路由器

一个更强大的解决方案是扩展 RouterOutlet 并在激活路由时检查用户是否已登录。这样您就不必将指令复制并粘贴到每个组件。此外,基于子组件的重定向可能会产生误导。

@Directive({
  selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet {
  publicRoutes: Array;
  private parentRouter: Router;
  private userService: UserService;

  constructor(
    _elementRef: ElementRef, _loader: DynamicComponentLoader,
    _parentRouter: Router, @Attribute('name') nameAttr: string,
    userService: UserService
  ) {
    super(_elementRef, _loader, _parentRouter, nameAttr);

    this.parentRouter = _parentRouter;
    this.userService = userService;
    this.publicRoutes = [
      '', 'login', 'signup'
    ];
  }

  activate(instruction: ComponentInstruction) {
    if (this._canActivate(instruction.urlPath)) {
      return super.activate(instruction);
    }

    this.parentRouter.navigate(['Login']);
  }

  _canActivate(url) {
    return this.publicRoutes.indexOf(url) !== -1 || this.userService.isLoggedIn()
  }
}

UserService 代表您的业务逻辑所在的位置,无论用户是否登录。您可以在构造函数中使用 DI 轻松添加它。

当用户导航到您网站上的新 url 时,将使用当前指令调用激活方法。您可以从中获取 url 并决定是否允许它。如果不只是重定向到登录页面。

让它工作的最后一件事是将它传递给我们的主要组件而不是内置组件。

@Component({
  selector: 'app',
  directives: [LoggedInRouterOutlet],
  template: template
})
@RouteConfig(...)
export class AppComponent { }

此解决方案不能与 @CanActive 生命周期装饰器一起使用,因为如果传递给它的函数解析为 false,则不会调用 RouterOutlet 的激活方法。

还写了一篇详细的博客post关于它:https://medium.com/@blacksonic86/authentication-in-angular-2-958052c64492

请不要覆盖路由器插座!这是最新路由器版本(3.0 beta)的噩梦。

改为使用 CanActivate 和 CanDeactivate 接口,并在路由定义中将 class 设置为 canActivate / canDeactivate。

像那样:

{ path: '', component: Component, canActivate: [AuthGuard] },

Class:

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(protected router: Router, protected authService: AuthService)
    {

    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {

        if (state.url !== '/login' && !this.authService.isAuthenticated()) {
            this.router.navigate(['/login']);
            return false;
        }

        return true;
    }
}

另请参阅: https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard

这是一个使用 Angular 4 (也兼容 Angular 5 - 8)

的更新示例

具有受 AuthGuard 保护的主路由的路由

import { Routes, RouterModule } from '@angular/router';

import { LoginComponent } from './login/index';
import { HomeComponent } from './home/index';
import { AuthGuard } from './_guards/index';

const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },

    // home route protected by auth guard
    { path: '', component: HomeComponent, canActivate: [AuthGuard] },

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];

export const routing = RouterModule.forRoot(appRoutes);

如果用户未登录,AuthGuard 将重定向到登录页面

已更新以将查询参数中的原始 url 传递到登录页面

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        if (localStorage.getItem('currentUser')) {
            // logged in so return true
            return true;
        }

        // not logged in so redirect to login page with the return url
        this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
        return false;
    }
}

有关完整示例和工作演示,您可以查看 this post

根据上面的精彩回答,我还想 CanActivateChild:保护子路由。它可用于将 guard 添加到对 ACL

等情况有帮助的子路由

是这样的

src/app/auth-guard.service.ts (excerpt)

import { Injectable }       from '@angular/core';
import {
  CanActivate, Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  CanActivateChild
}                           from '@angular/router';
import { AuthService }      from './auth.service';

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
  constructor(private authService: AuthService, private router:     Router) {}

  canActivate(route: ActivatedRouteSnapshot, state:    RouterStateSnapshot): boolean {
    let url: string = state.url;
    return this.checkLogin(url);
  }

  canActivateChild(route: ActivatedRouteSnapshot, state:  RouterStateSnapshot): boolean {
    return this.canActivate(route, state);
  }

/* . . . */
}

src/app/admin/admin-routing.module.ts (excerpt)

const adminRoutes: Routes = [
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: '',
        canActivateChild: [AuthGuard],
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          { path: 'heroes', component: ManageHeroesComponent },
          { path: '', component: AdminDashboardComponent }
        ]
      }
    ]
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(adminRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class AdminRoutingModule {}

这取自 https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard

参考这段代码, auth.ts 文件

import { CanActivate } from '@angular/router';
import { Injectable } from '@angular/core';
import {  } from 'angular-2-local-storage';
import { Router } from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
constructor(public localStorageService:LocalStorageService, private router: Router){}
canActivate() {
// Imaginary method that is supposed to validate an auth token
// and return a boolean
var logInStatus         =   this.localStorageService.get('logInStatus');
if(logInStatus == 1){
    console.log('****** log in status 1*****')
    return true;
}else{
    console.log('****** log in status not 1 *****')
    this.router.navigate(['/']);
    return false;
}


}

}
// *****And the app.routes.ts file is as follow ******//
      import {  Routes  } from '@angular/router';
      import {  HomePageComponent   } from './home-page/home- page.component';
      import {  WatchComponent  } from './watch/watch.component';
      import {  TeachersPageComponent   } from './teachers-page/teachers-page.component';
      import {  UserDashboardComponent  } from './user-dashboard/user- dashboard.component';
      import {  FormOneComponent    } from './form-one/form-one.component';
      import {  FormTwoComponent    } from './form-two/form-two.component';
      import {  AuthGuard   } from './authguard';
      import {  LoginDetailsComponent } from './login-details/login-details.component';
      import {  TransactionResolver } from './trans.resolver'
      export const routes:Routes    =   [
    { path:'',              component:HomePageComponent                                                 },
    { path:'watch',         component:WatchComponent                                                },
    { path:'teachers',      component:TeachersPageComponent                                         },
    { path:'dashboard',     component:UserDashboardComponent,       canActivate: [AuthGuard],   resolve: { dashboardData:TransactionResolver } },
    { path:'formone',       component:FormOneComponent,                 canActivate: [AuthGuard],   resolve: { dashboardData:TransactionResolver } },
    { path:'formtwo',       component:FormTwoComponent,                 canActivate: [AuthGuard],   resolve: { dashboardData:TransactionResolver } },
    { path:'login-details', component:LoginDetailsComponent,            canActivate: [AuthGuard]    },

]; 

1. Create a guard as seen below. 2. Install ngx-cookie-service to get cookies returned by external SSO. 3. Create ssoPath in environment.ts (SSO Login redirection). 4. Get the state.url and use encodeURIComponent.

import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from 
  '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { environment } from '../../../environments/environment.prod';

@Injectable()
export class AuthGuardService implements CanActivate {
  private returnUrl: string;
  constructor(private _router: Router, private cookie: CookieService) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (this.cookie.get('MasterSignOn')) {
      return true;
    } else {
      let uri = window.location.origin + '/#' + state.url;
      this.returnUrl = encodeURIComponent(uri);      
      window.location.href = environment.ssoPath +  this.returnUrl ;   
      return false;      
    }
  }
}