Angular 6和keycloak-angular模块:如何从服务器加载keycloak url

时间:2018-08-26 16:23:38

标签: angular keycloak keycloak-services

我正在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呼叫,因为enableBearerInterceptor的默认值为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-module进行虚拟初始化,但是它始终执行onload-function,并且也不能将其禁用。

有人对实现这一目标有任何想法吗? 谢谢

1 个答案:

答案 0 :(得分:1)

原来,我们有一个自己的KeycloakBearerInterceptor,它可以启动并失败。 因此,这与keycloak-angleular库无关。