首页 文章

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

提问于
浏览
0

我在Angular 6项目中使用了keycloak-angular模块(v 4.0.0) . 现在我正在尝试从我的服务器加载Url到Keycloak使用(因为根据apiKey可能会有所不同) . 所以,在app初始化函数中我尝试过这样的事情:

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-function,也不能禁用 .

有没有人有任何想法如何实现这一目标?谢谢

1 回答

  • 1

    事实证明,我们拥有自己的KeycloakBearerInterceptor,这是一个启动并失败的人 . 所以这与keycloak-angular库无关 .

相关问题