@azure/msal-angularを使用する前に、アプリケーションをMicrosoft Entra IDに登録してclientIdを取得します。
アプリ モジュールに MSAL モジュールを含め、初期化する
app.module.tsに MsalModule をインポートします。 MSAL モジュールを初期化するには、アプリケーションの clientId を渡します。アプリケーションの登録から取得します。
import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { MsalModule, MsalService, MsalGuard, MsalInterceptor, MsalBroadcastService, MsalRedirectComponent } from "@azure/msal-angular";
import { PublicClientApplication, InteractionType, BrowserCacheLocation } from "@azure/msal-browser";
@NgModule({
imports: [
MsalModule.forRoot( new PublicClientApplication({ // MSAL Configuration
auth: {
clientId: "Your client ID",
authority: "Your authority",
redirectUri: "Your redirect Uri",
},
cache: {
cacheLocation : BrowserCacheLocation.LocalStorage,
},
system: {
loggerOptions: {
loggerCallback: () => {},
piiLoggingEnabled: false
}
}
}), {
interactionType: InteractionType.Redirect, // MSAL Guard Configuration
}, {
interactionType: InteractionType.Redirect, // MSAL Interceptor Configuration
})
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
},
MsalService,
MsalGuard,
MsalBroadcastService
],
bootstrap: [AppComponent, MsalRedirectComponent]
})
export class AppModule {}
アプリケーション内のルートをセキュリティで保護する
ルート定義に canActivate: [MsalGuard] を追加して、アプリケーション内の特定のルートをセキュリティで保護するための認証を追加します。 親ルートまたは子ルートに追加します。 ユーザーがこれらのルートにアクセスすると、ライブラリはユーザーに認証を求めます。
追加のインターフェイスの使用など、構成と考慮事項の詳細については、 MsalGuard ドキュメント を参照してください。
MsalGuardで定義されたルートの例を次に示します。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProfileComponent } from './profile/profile.component';
import { MsalGuard } from '@azure/msal-angular';
const routes: Routes = [
{
path: 'profile',
component: ProfileComponent,
canActivate: [MsalGuard]
},
{
path: '',
component: HomeComponent
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Web API 呼び出しのトークンを取得する
@azure/msal-angular では、次のように http インターセプター (MsalInterceptor) を app.module.ts に追加できます。
MsalInterceptorはトークンを取得し、protectedResourceMapに基づいて API 呼び出し内のすべての Http 要求に追加します。 構成と使用の詳細については、 MsalInterceptor のドキュメント を参照してください。
import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from './app.component';
import { MsalModule, MsalService, MsalGuard, MsalInterceptor, MsalBroadcastService, MsalRedirectComponent } from "@azure/msal-angular";
import { PublicClientApplication, InteractionType, BrowserCacheLocation } from "@azure/msal-browser";
@NgModule({
imports: [
MsalModule.forRoot( new PublicClientApplication({ // MSAL Configuration
auth: {
clientId: "Your client ID",
authority: "Your authority",
redirectUri: "Your redirect Uri",
},
cache: {
cacheLocation : BrowserCacheLocation.LocalStorage,
},
system: {
loggerOptions: {
loggerCallback: () => {},
piiLoggingEnabled: false
}
}
}), {
interactionType: InteractionType.Redirect, // MSAL Guard Configuration
}, {
interactionType: InteractionType.Redirect, // MSAL Interceptor Configuration
protectedResourceMap: new Map([
['https://graph.microsoft.com/v1.0/me', ['user.read']],
['https://api.myapplication.com/users/*', ['customscope.read']],
['http://localhost:4200/about/', null]
])
})
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
},
MsalService,
MsalGuard,
MsalBroadcastService
],
bootstrap: [AppComponent, MsalRedirectComponent]
})
export class AppModule {}
MsalInterceptorの使用は省略可能です。 代わりに acquireToken API を使用してトークンを明示的に取得することもできます。
MsalInterceptorは便宜上提供されており、すべてのユース ケースに適合しない場合があることに注意してください。
MsalInterceptorで対処されていない特定のニーズがある場合は、独自のインターセプターを記述します。
イベントを購読する
MSAL は、認証と MSAL に関連するイベントを出力するイベント システムを提供します。 イベントを使用するには、コンポーネントまたはサービスのコンストラクターに MsalBroadcastService を追加します。
1. イベントを購読する方法
import { EventMessage, EventType } from '@azure/msal-browser';
import { filter } from 'rxjs/operators';
this.msalBroadcastService.msalSubject$
.pipe(
filter((msg: EventMessage) => msg.eventType === EventType.LOGIN_SUCCESS)
)
.subscribe((result) => {
// do something here
});
2. 利用可能なイベント
MSAL で使用できるイベントの一覧については、 @azure/msal-browser イベントのドキュメントを参照してください。
3. サブスクライブを解除する
サブスクリプションの解除は重要です。 コンポーネントに ngOnDestroy() を実装してサブスクリプションを解除します。
import { EventMessage, EventType } from '@azure/msal-browser';
import { filter, Subject, takeUntil } from 'rxjs';
private readonly _destroying$ = new Subject<void>();
this.msalBroadcastService.msalSubject$
.pipe(
filter((msg: EventMessage) => msg.eventType === EventType.LOGIN_SUCCESS),
takeUntil(this._destroying$)
)
.subscribe((result) => {
this.checkAccount();
});
ngOnDestroy(): void {
this._destroying$.next(null);
this._destroying$.complete();
}
次のステップ
@azure/msal-angular
パブリック API を使用する準備ができました。