angular - Angular 中 MSAL 2 的动态配置
问题描述
对于我的 Angular (v12) 应用程序,我使用 MSAL 进行身份验证,但我遇到了配置问题。
在配置中,我有tenantId 和app Id 来初始化MSAL。
我不能使用标准的 Angular 配置切换,environment.ts
因为它不能在后期构建步骤中更改。
相反,我尝试加载一个 json 文件并在应用程序启动时使用 APP_INITIALIZER 回调调用,如本文所述:如何使用 APP_INITIALIZER 初始化 msal 配置
我对其进行了一些修改,因为MsalAngularConfiguration
MSAL v2 中不再存在。
但它不起作用,在调用MSALInstanceFactory
ConfigService.init 之前完成调用(由 AppComponent 因为它依赖于MsalGuardConfiguration
)。
我也尝试过传入ConfigService
函数msalConfigFactory
,但参数未定义。
我也尝试将代码作为 json 导入,但它不起作用,因为我在构建它时得到了值。
任何想法?
解决方案
看来我已经在提供程序中双重使用了我的 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 文件设置环境对象的值。
推荐阅读
- php - PDO 返回结果,但 else 语句不起作用
- python - 在我的 DRF 视图中使用 list() 会引发 AttributeError
- kotlin - 对同一 ViewModel 中的 LiveData 更改做出反应
- javascript - graphQL 查询中的变量
- javascript - SCRIPT5009:未定义“搜索”
- php - Laravel 5.7 迁移:密钥太长
- bash - 为什么直接运行的 bash 脚本不需要用户权限,而运行脚本的 .app 文件需要用户权限?
- php - Laravel 5.6 中的迁移 - 找不到驱动程序
- c++ - vim 自动替换。与 -> 在 cpp
- sql - 删除日期较旧/较新的重复行