首页 > 解决方案 > Angular 中 MSAL 2 的动态配置

问题描述

对于我的 Angular (v12) 应用程序,我使用 MSAL 进行身份验证,但我遇到了配置问题。

在配置中,我有tenantId 和app Id 来初始化MSAL。

我不能使用标准的 Angular 配置切换,environment.ts因为它不能在后期构建步骤中更改。

相反,我尝试加载一个 json 文件并在应用程序启动时使用 APP_INITIALIZER 回调调用,如本文所述:如何使用 APP_INITIALIZER 初始化 msal 配置

我对其进行了一些修改,因为MsalAngularConfigurationMSAL v2 中不再存在。

但它不起作用,在调用MSALInstanceFactoryConfigService.init 之前完成调用(由 AppComponent 因为它依赖于MsalGuardConfiguration)。

我也尝试过传入ConfigService函数msalConfigFactory,但参数未定义。

我也尝试将代码作为 json 导入,但它不起作用,因为我在构建它时得到了值。

任何想法?

标签: angularconfigurationpost-buildmsal-angular

解决方案


看来我已经在提供程序中双重使用了我的 AppConfigService。我使用本地工厂做了同样的事情,它可以工作:

export function EnvConfigurationFactory(httpBackend: HttpBackend) {

  return () => {
    let http = new HttpClient(httpBackend);
    return http.get('assets/configuration/environment.json')
            .toPromise()
            .then((resp) => {
              for (var key in resp) {
                environment[key] = resp[key];
              }
            })
  }
} 

我使用 HttpBackend 而不是 HttpClient,因为配置了 HTTP_INTERCEPTORS 并使用我尝试设置的设置。我直接使用 environment.json 文件设置环境对象的值。


推荐阅读