Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用
Angular中的AuthGuard
Angular 中的 AuthGuard 是一個(gè)路由守衛(wèi),它用于保護(hù)某些路由,只有當(dāng)用戶經(jīng)過身份驗(yàn)證并具有訪問權(quán)限時(shí),才允許他們?cè)L問。AuthGuard 通常與路由配置一起使用,以確保用戶無(wú)法直接訪問需要身份驗(yàn)證的頁(yè)面。
創(chuàng)建
AuthGuard 是一個(gè) Angular 服務(wù),可以使用以下命令來創(chuàng)建它:
ng g guard auth
上面的命令將生成一個(gè)名為“auth”的 AuthGuard,并將其添加到 src/app/auth.guard.ts 文件中。
auth.guard.ts 文件中的代碼如下所示:
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (this.authService.isLoggedIn()) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
}
}在上面的代碼中,AuthGuard 是通過實(shí)現(xiàn) CanActivate 接口來定義的。CanActivate 接口包含一個(gè)名為 canActivate() 的方法,該方法決定了當(dāng)用戶嘗試訪問受保護(hù)的路由時(shí)應(yīng)該執(zhí)行哪些操作。
在 AuthGuard 中,我們注入了一個(gè)名為 AuthService 的服務(wù)和 Router 對(duì)象。AuthService 用于檢查用戶是否已經(jīng)登錄,而 Router 用于導(dǎo)航到登錄頁(yè)面或者用戶試圖訪問的頁(yè)面。在 canActivate() 方法中,我們首先調(diào)用 this.authService.isLoggedIn() 方法來檢查用戶是否已經(jīng)登錄。如果用戶已經(jīng)登錄,我們返回 true,表示用戶可以訪問該路由。否則,我們調(diào)用 this.router.navigate(['/login']) 方法,將用戶重定向到登錄頁(yè)面,然后返回 false,表示用戶無(wú)法訪問該路由。
AuthGuard保護(hù)一個(gè)路由
要使用 AuthGuard 來保護(hù)一個(gè)路由,我們需要在路由配置中將 AuthGuard 添加到 canActivate 屬性中。例如:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { LoginComponent } from './login/login.component';
import { AuthGuard } from './auth.guard';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }在上面的代碼中,我們將 AuthGuard 添加到了 /dashboard 路由的 canActivate 屬性中,這意味著只有當(dāng)用戶已經(jīng)登錄時(shí)才能訪問 /dashboard 路由。
以上就是Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用的詳細(xì)內(nèi)容,更多關(guān)于Angular AuthGuard創(chuàng)建使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Angularjs分頁(yè)查詢的實(shí)現(xiàn)
本文給大家分享angularjs實(shí)現(xiàn)分頁(yè)查詢功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-02-02
后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
這篇文章主要介紹了后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-07-07
快速學(xué)習(xí)AngularJs HTTP響應(yīng)攔截器
任何時(shí)候,如果我們想要為請(qǐng)求添加全局功能,例如身份認(rèn)證、錯(cuò)誤處理等,在請(qǐng)求發(fā)送給服務(wù)器之前或服務(wù)器返回時(shí)對(duì)其進(jìn)行攔截,是比較好的實(shí)現(xiàn)手段2015-12-12
angular 服務(wù)的單例模式(依賴注入模式下)詳解
這篇文章主要介紹了angular 服務(wù)的單例模式(依賴注入模式下)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
AngularJS使用ocLazyLoad實(shí)現(xiàn)js延遲加載
這篇文章主要介紹了AngularJS使用ocLazyLoad實(shí)現(xiàn)js延遲加載的相關(guān)資料,需要的朋友可以參考下2017-07-07
AngularJS控制器controller給模型數(shù)據(jù)賦初始值的方法
這篇文章主要介紹了AngularJS控制器controller給模型數(shù)據(jù)賦初始值的方法,涉及AngularJS控制器controller簡(jiǎn)單賦值操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-01-01

