我在一个Angular 6项目中使用keycloak-angular模块(v4.0.0)。现在,我尝试从我的服务器加载要使用的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;
});
}但是因为enableBearerInterceptor的缺省值是true,所以KeycloakBearerInterceptor截取该http调用。当然,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-module,但它总是执行onload-function,而且也不能禁用。
有没有人有办法做到这一点?谢谢
发布于 2018-08-27 16:30:50
事实证明,我们有一个自己的KeycloakBearerInterceptor,它是启动并失败的那个。所以这与keycloak-angular库没有任何关系。
https://stackoverflow.com/questions/52028134
复制相似问题