angular - Angular 6 和 keycloak-angular 模块:如何从服务器加载 keycloak url
问题描述
我在 Angular 6 项目中使用 keycloak-angular 模块(v 4.0.0)。现在我正在尝试从我的服务器将 Url 加载到 Keycloak 以使用(因为它可能因 apiKey 而异)。所以,在应用初始化函数中,我尝试过这样的事情:
export function initializer(keycloak: KeycloakService, http: HttpClient): () => Promise<any> {
return () => new Promise<boolean>((resolve) => {
const apiKey = 'myapikey;
// TODO: make KeycloakBearerInterceptor not intercept the HTTP-Call
// load configuration from server
const promise = http.get<ApiConfiguration>(environment.apiHost + '/1.0/configuration?apiKey=' + apiKey).toPromise()
.then((data: ApiConfiguration) => {
const keycloakConfig = {
url: data.idpConfig.authority, <-- This comes from the server
realm: 'mediakey',
clientId: 'mediakey'
};
keycloak.init({
config: {
url: data.idpConfig.authority,
realm: 'mediakey',
clientId: 'mediakey'
},
loadUserProfileAtStartUp: true,
initOptions: {
onLoad: 'check-sso',
// onLoad: 'login-required',
checkLoginIframe: false,
flow: 'implicit'
},
enableBearerInterceptor: false,
bearerExcludedUrls: [
'/assets',
'/1.0/configuration'
],
}).catch((reason) => console.log(reason));
return true;
});
return promise;
});
}
但是 KeycloakBearerInterceptor 会拦截 http-call,因为默认值为enableBearerInterceptor
is true
。当然,keycloak-module 还没有初始化,因此拦截器失败了
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'authenticated' of undefined
TypeError: Cannot read property 'authenticated' of undefined
at KeycloakBearerInterceptor.push../shared/core/rest/keycloak-bearer-interceptor.ts.KeycloakBearerInterceptor.intercept (keycloak-bearer-interceptor.ts:42)
我也尝试过对 keycloak 模块进行虚拟初始化,但它始终执行 onload 功能,并且也无法禁用。
有没有人知道如何实现这一目标?谢谢
解决方案
事实证明,我们有一个我们自己的 KeycloakBearerInterceptor,它会启动并失败。所以这与 keycloak-angular 库无关。
推荐阅读
- swiftui - SwiftUI 自定义切换样式问题
- python - numpy switch axes and reshape
- react-native - Expo-Av 一次播放多个声音
- typescript - delayWhen 与特定用例中的延迟差异
- c++ - 在类内初始化变量时,我们是否有静态初始化顺序惨败
- snapshot - 不适用于 Flutter 版本或 dart 版本的 Stream Builder 我该如何解决这个问题?
- r - 如果使用循环在B列和C列中的值相同,R为A列中的所有行分配相同的值
- c++ - `static const int` 数据成员的 odr 使用成功,在调用 `const int &` 时没有定义
- c# - 在编辑视图 ASP.Net MVC 中显示选中的单选按钮的适当方法是什么
- javascript - 为什么当我 console.log() 时它说未定义?