这是我的配置设置的一部分,目的是让我的应用程序通过构建管道并花费了我数天时间。我最终找到了一个使用APP_INITIALIZER 加载REST 服务并为我的应用程序构建AppConfigService 的解决方案。我正在使用相同的 angular-oauth2-oidc 库。
我对此问题的解决方案不是在其 forRoot() 方法中设置 OAuthModule。在任何通过APP_INITIALIZER 的配置可用之前调用它 - 这会在应用于forRoot() 方法的配置对象时导致未定义的值。
但我们需要在 http 标头中添加一个标记。所以我使用http拦截器来附加令牌,如here所述。诀窍是在工厂中设置 OAuthModuleConfig。显然,这是在应用程序初始化后调用的。
配置模块
@NgModule({
imports: [
// no config here
OAuthModule.forRoot(),
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useFactory: authenticationInterceptorFactory,
deps: [OAuthStorage, AuthenticationErrorHandler, OAuthModuleConfig],
multi: true
}
]
})
拦截器工厂
const authenticationInterceptorFactory = (oAuthService: OAuthStorage, authenticationErrorHandler: AuthenticationErrorHandler, oAuthModuleConfig: OAuthModuleConfig) => {
const config = {
resourceServer: {
allowedUrls: [
// Include config settings here instead
AppConfigService.settings.apiURL,
AppConfigService.settings.anotherApiURL,
]
sendAccessToken: true
},
}
return new AuthenticationInterceptor(oAuthService, authenticationErrorHandler, config);
};