【问题标题】:NullInjectorError: No provider for InjectionToken MSAL_GUARD_CONFIGNullInjectorError:没有 InjectionToken MSAL_GUARD_CONFIG 的提供程序
【发布时间】:2021-09-08 22:08:13
【问题描述】:

在为 MSAL 2.0 使用自定义包装器时出现错误

这里是包装 ts 文件

import { APP_INITIALIZER, InjectionToken, NgModule } from '@angular/core';
import { LogLevel, Configuration, BrowserCacheLocation, InteractionType, IPublicClientApplication, PublicClientApplication } from '@azure/msal-browser';
import { ConfigService } from './shared/services/config.service';
import jsonconfig from '../assets/environment/conf.json'
import { MatDialogRef } from '@angular/material/dialog';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { MsalBroadcastService, MsalGuard, MsalGuardConfiguration, MsalInterceptor, MsalInterceptorConfiguration, MsalModule, MsalService, MSAL_GUARD_CONFIG, MSAL_INSTANCE, MSAL_INTERCEPTOR_CONFIG } from '@azure/msal-angular';
 const isIE = window.navigator.userAgent.indexOf("MSIE ") > -1 || window.navigator.userAgent.indexOf("Trident/") > -1;
 
 const AUTH_CONFIG_URL_TOKEN = new InjectionToken<string>('AUTH_CONFIG_URL');

 export function initializerFactory(env: ConfigService): any {
     // APP_INITIALIZER, except a function return which will return a promise
     // APP_INITIALIZER, angular doesnt starts application untill it completes
     const promise = env.init().then((value) => {
         console.log(env.getSettings('clientID'));
     });
     return () => promise;
 }


 
 export function MSALInstanceFactory(conf:ConfigService): IPublicClientApplication {
     const configuration:Configuration={
     auth: {
         clientId: conf.getSettings("clientId"), // This is the ONLY mandatory field that you need to supply.
         authority: 'https://login.microsoftonline.com/'+ conf.getSettings("tenentId"), // Defaults to "https://login.microsoftonline.com/common"
         redirectUri: conf.getSettings("redirectUri"), // Points to window.location.origin. You must register this URI on Azure portal/App Registration.
         postLogoutRedirectUri: '/', // Indicates the page to navigate after logout.
         navigateToLoginRequestUrl: true, // If "true", will navigate back to the original request location before processing the auth code response.
     },
     cache: {
         cacheLocation: BrowserCacheLocation.LocalStorage, // Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO between tabs.
         storeAuthStateInCookie: isIE, // Set this to "true" if you are having issues on IE11 or Edge
     },
     system: {
         loggerOptions: {
             loggerCallback(logLevel: LogLevel, message: string) {
                 console.log(message);
             },
             logLevel: LogLevel.Verbose,
             piiLoggingEnabled: false
         }
     }
    }
    return new PublicClientApplication(configuration);
 }

 
 export const silentRequest = {
     scopes: ["openid", "profile"],
     loginHint: "example@domain.net"
 };

 
export const loginRequest = {
    scopes: []
  };

  export function MSALInterceptorConfigFactory(conf:ConfigService): MsalInterceptorConfiguration {
    const protectedResources:Map<string, Array<string>>=new Map([
        ['https://graph.microsoft.com/v1.0/me', ['user.read']],
        [
          'api',
          [conf.getSettings("apiClientId") + '/user_impersonation'],
        ],
      ]);
    
    return {
      interactionType: InteractionType.Redirect,
      protectedResourceMap: protectedResources
    };
  }
  
  export function MSALGuardConfigFactory(): MsalGuardConfiguration {
    return { 
      interactionType: InteractionType.Redirect,
      authRequest: loginRequest
    };
  }
//-------------------------------------------------------------
@NgModule({
    providers: [
    ],
    imports: [MsalModule]
})
export class MsalConfModule{
    staticforroot() {
        return {
            providers: [
                { provide: AUTH_CONFIG_URL_TOKEN },
                { provide: APP_INITIALIZER, useFactory: initializerFactory,
                    deps: [ConfigService, 
                    AUTH_CONFIG_URL_TOKEN], 
                    multi: true 
                },
                {
                  provide: HTTP_INTERCEPTORS,
                  useClass: MsalInterceptor,
                  multi: true,
                  
                },
                {
                  provide: MSAL_INSTANCE,
                  useFactory: MSALInstanceFactory,
                  deps: [ConfigService]
                },
                {
                  provide: MSAL_GUARD_CONFIG,
                  useFactory: MSALGuardConfigFactory
                },
                {
                  provide: MSAL_INTERCEPTOR_CONFIG,
                  useFactory: MSALInterceptorConfigFactory,
                  deps: [ConfigService]
                },
                MsalService,
                MsalGuard,
                MsalBroadcastService
            ],
        }
    }

}

这是应用模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule,AppRoutingComonent } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'

import { MsalConfModule } from './authconfig';


const ngWizardConfig: NgWizardConfig = {
  theme: THEME.default
};


@NgModule({
  declarations: [
    AppComponent,
    AppRoutingComonent,
  
  
  ],
  imports: [
    BrowserModule,
    DataTablesModule,
    HttpClientModule,
    AppRoutingModule,
    MatDialogModule,
    BrowserAnimationsModule, // required animations module
    ToastrModule.forRoot(), // ToastrModule added,
    NgWizardModule.forRoot(ngWizardConfig),
    MsalConfModule
  ],
  providers: [
    {
    provide: MatDialogRef,
    useValue: {}
    }
],
  bootstrap: [AppComponent]
})
export class AppModule { 
  
}

没有编译错误。我在下面收到运行时错误

根据 J.Loscos 的回答,我已经更新了代码,但现在显示此错误

Type '{ provide: InjectionToken<string>; }' is not assignable to type 'Provider'.
  Type '{ provide: InjectionToken<string>; }' is missing the following properties from type 'Type<any>'

【问题讨论】:

    标签: angular msal


    【解决方案1】:

    您的代码的问题是您在 staticforroot 方法中声明了注入令牌,但您没有使用此方法。

    您似乎正在尝试使用 forRoot 模式进行模块导入。 要使用这种模式,模块中的 staticforroot 方法应该返回模块和提供者:

    @NgModule({
        providers: [
        ],
        imports: [MsalModule]
    })
    export class MsalConfModule{
        staticforroot() : ModuleWithProviders<MsalConfModule>  {
            return {
                ngModule: MsalConfModule,
                providers: [
                    { provide: AUTH_CONFIG_URL_TOKEN },
                    { provide: APP_INITIALIZER, useFactory: initializerFactory,
                        deps: [ConfigService, 
                        AUTH_CONFIG_URL_TOKEN], 
                        multi: true 
                    },
                    ...
               ]
            };
         }
    

    在你的 AppModule 中导入这个模块时,你需要调用 staticforroot 方法:

    @NgModule({
      declarations: [
        AppComponent,
        AppRoutingComonent,
      
      
      ],
      imports: [
        BrowserModule,
        ...
        MsalConfModule.staticforroot()
      ],
      providers: [
        {
        provide: MatDialogRef,
        useValue: {}
        }
    ],
      bootstrap: [AppComponent]
    })
    export class AppModule { 
      
    }
    

    【讨论】:

    • 非常感谢您的建议。但我试过了,现在得到这个错误 Type '{ provide: InjectionToken; }' 不可分配给类型 'Provider'。输入'{提供:InjectionToken; }' 缺少类型 'Type' 的以下属性。也将使用此详细信息更新问题
    • 我认为这个错误来自`{提供:AUTH_CONFIG_URL_TOKEN}`,因为它没有useuseClassuseFactory
    • 能否请您建议我可以在那里提及的内容.. :(
    • 根据注入令牌的名称,我想它应该是配置文件的 url。所以你可以做类似{ provide: AUTH_CONFIG_URL_TOKEN, use: "https://yourbackend/config.json" } 的事情但是我看到你声明了这个令牌作为初始化工厂的依赖,但是这个工厂不使用它。因此,如果它没有在其他任何地方使用,您也可以删除提供程序并将其从 initializerFactory 依赖项中删除
    • 在我的回答中,我向 staticforroot 方法返回的对象添加了一个属性 ngModule。您是否将其包含在您的代码中?
    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 2021-09-10
    • 2018-07-16
    • 2018-10-19
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多