首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 6和keycloak-angular模块:如何从服务器加载keycloak url

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

Stack Overflow用户
提问于 2018-08-27 00:23:38
回答 1查看 1.4K关注 0票数 0

我在一个Angular 6项目中使用keycloak-angular模块(v4.0.0)。现在,我尝试从我的服务器加载要使用的Keycloak的网址(因为根据apiKey的不同,它可能会有所不同)。所以,在应用程序的初始化器函数中,我尝试了这样做:

代码语言:javascript
复制
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还没有初始化,因此拦截器会失败,并显示

代码语言:javascript
复制
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,而且也不能禁用。

有没有人有办法做到这一点?谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-08-27 16:30:50

事实证明,我们有一个自己的KeycloakBearerInterceptor,它是启动并失败的那个。所以这与keycloak-angular库没有任何关系。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52028134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档