如何解决当使用过期令牌调用api时,页面会重新加载角9,@ azure / msal角1.1.1
使用过期的令牌调用api时,令牌拦截器试图 acquireTokenSilently(environment.securityScopes) ,但会导致页面重新加载。我在此MS Docs to avoid page reloads上遵循了指示。如果你们中的任何一个可以分享一个可行的例子或任何建议,那将大有帮助。
这是我的 app.module.ts
@NgModule({
declarations: [AppComponent,MSALComponent,declarations],imports: [
BrowserModule,BrowserAnimationsModule,HttpClientModule,SharedModule,AppSecurityModule,// code snippet added below appSecurity.module.ts
AppRoutingModule,],providers: [{
provide: APP_INITIALIZER,useFactory: initApp,multi: true,deps: [HttpClient]
},httpInterceptorProviders
],entryComponents: [AppComponent,MSALComponent]
})
export class AppModule {
constructor() {
console.log('APP Module Constructor!');
}
/*
Avoid page reloads when acquiring and renewing tokens silently using MSAL.js
https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-avoid-page-reloads
*/
ngDoBootstrap(ref: ApplicationRef) {
debugger;
if (window !== window.parent && !window.opener) {
console.log("Bootstrap: MSAL");
ref.bootstrap(MSALComponent);
} else {
// this.router.resetConfig(RouterModule);
console.log("Bootstrap: App");
ref.bootstrap(AppComponent);
}
}
}
这是我的 appSecurity.module.ts
const routes: Routes = [
{ path: "authorized",component: MSALComponent,pathMatch: "full" },];
function MSALConfigFactory(): Configuration {
return {
auth: {
clientId: environment.securityClientId,authority: environment.securityAuthority,validateAuthority: false,redirectUri: ( () => (window.location.origin + ((window !== window.parent && !window.opener) ? "/authorized" : "/home")) ),// Full URL (no partial urls,null or undefined!). Important! This must be a registered URL in B2C within the Azure Portal.
postLogoutRedirectUri: window.location.origin + "/home",null or undefined ... if the property exists on the object it is used!)
navigateToLoginRequestUrl: false,},cache: {
cacheLocation: "localStorage",storeAuthStateInCookie: isIE,// set to true for IE 11
},system: {
// loadFrameTimeout: 300000,};
}
function MSALAngularConfigFactory(): MsalAngularConfiguration {
return {
popUp: !isIE,consentScopes: environment.securityScopes,protectedResourceMap: protectedResourceMap,unprotectedResources: unprotectedResources,extraQueryParameters: {},};
}
@NgModule({
declarations: [declarations],imports: [
CommonModule,RouterModule.forRoot(routes),MsalModule,exports: [declarations],providers: [
// i'm using my own token interceptor
// {
// provide: HTTP_INTERCEPTORS,// useClass: MsalInterceptor,// multi: true,// },{
provide: MSAL_CONFIG,useFactory: MSALConfigFactory,{
provide: MSAL_CONFIG_ANGULAR,useFactory: MSALAngularConfigFactory,MsalService,})
export class AppSecurityModule {}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。