namespace HawtioKeycloak {

  const TOKEN_UPDATE_INTERVAL = 5; // 5 sec.

  export class KeycloakService {

    constructor(
      public readonly enabled: boolean,
      public readonly keycloak: Keycloak.KeycloakInstance) {
    }

    updateToken(onSuccess: (token: string) => void, onError?: () => void): void {
      this.keycloak.updateToken(TOKEN_UPDATE_INTERVAL)
        .success(() => {
          let token = this.keycloak.token;
          onSuccess(token);
        })
        .error(() => {
          log.error("Couldn't update token");
          if (onError) {
            onError();
          }
        });
    }

    setupJQueryAjax(userDetails: Core.AuthService): void {
      log.debug("Setting authorization header to token");
      $.ajaxSetup({
        beforeSend: (xhr: JQueryXHR, settings: JQueryAjaxSettings) => {
          if (this.keycloak.authenticated && !this.keycloak.isTokenExpired(TOKEN_UPDATE_INTERVAL)) {
            // hawtio uses BearerTokenLoginModule on server side
            xhr.setRequestHeader('Authorization', Core.getBasicAuthHeader(keycloak.subject, keycloak.token));
          } else {
            log.debug("Skipped request", settings.url, "for now.");
            this.updateToken(
              (token) => {
                if (token) {
                  log.debug('Keycloak token refreshed. Set new value to userDetails');
                  userDetails.token = token;
                }
                log.debug("Re-sending request after successfully update keycloak token:", settings.url);
                $.ajax(settings);
              },
              () => userDetails.logout());
            return false;
          }
        }
      });
    }

  }

}

namespace HawtioKeycloak {

  const TOKEN_UPDATE_INTERVAL = 5; // 5 sec.

  export class KeycloakService {

    constructor(
      public readonly enabled: boolean,
      public readonly keycloak: Keycloak.KeycloakInstance) {
    }

    updateToken(onSuccess: (token: string) => void, onError?: () => void): void {
      this.keycloak.updateToken(TOKEN_UPDATE_INTERVAL)
        .success(() => {
          let token = this.keycloak.token;
          onSuccess(token);
        })
        .error(() => {
          log.error("Couldn't update token");
          if (onError) {
            onError();
          }
        });
    }

    setupJQueryAjax(userDetails: Core.AuthService): void {
      log.debug("Setting authorization header to token");
      $.ajaxSetup({
        beforeSend: (xhr: JQueryXHR, settings: JQueryAjaxSettings) => {
          if (this.keycloak.authenticated && !this.keycloak.isTokenExpired(TOKEN_UPDATE_INTERVAL)) {
            // hawtio uses BearerTokenLoginModule on server side
            xhr.setRequestHeader('Authorization', Core.getBasicAuthHeader(keycloak.profile.username, keycloak.token));
          } else {
            log.debug("Skipped request", settings.url, "for now.");
            this.updateToken(
              (token) => {
                if (token) {
                  log.debug('Keycloak token refreshed. Set new value to userDetails');
                  userDetails.token = token;
                }
                log.debug("Re-sending request after successfully update keycloak token:", settings.url);
                $.ajax(settings);
              },
              () => userDetails.logout());
            return false;
          }
        }
      });
    }

  }

}

Change Keycloak Authorization header from cryptic subject to profile.username
Change Keycloak Authorization header from cryptic subject to profile.username
