Angular 6 и keycloak-angular module: как загрузить URL-адрес keycloak с сервера - PullRequest
0 голосов
/ 26 августа 2018

Я использую модуль keycloak-angular (v 4.0.0) в проекте Angular 6.Теперь я пытаюсь загрузить 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 еще не инициализирован, и, следовательно, перехватчик завершается с ошибкой

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, и это также можетне быть отключенным.

У кого-нибудь есть идеи, как этого добиться?Спасибо

1 Ответ

0 голосов
/ 27 августа 2018

Оказалось, что у нас есть собственный KeycloakBearerInterceptor, который срабатывает и не работает. Так что это никак не связано с библиотекой keycloak-angular.

...