Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anderstand how to initialize the keycloak-angular service ? (bearerExcludedUrls, etc...)

I'm using Angular 8.0.3, keycloak 8.0.0 and the keycloak-service 7.0.1

Problem

I managed to configure the authentication from my angular app. So every time that I go on my application without authorization header, I'm being redirected towards the keycloak login page.

If I want an unlogged person to access a public page (e. g.'/public/cities') without redirecting to the keycloak login page, should I use'bearerExcludedUrIs'? If so, what should I wear? If not, how to proceed?

from a more general point of view, the keycloak initialization function in the service provided has an initOption part. I don't exactly understand the meaning of all its attributes (onLoad, token, refreshToken, idToken, timeSkew, sheckLoginIframe, checkLoginIframeInterval, responseMode, flow). If an enlightened person willing to share his knowledge with me could enlighten me, I would be grateful...

My code

I'm using ngDoBootstrap to bootstrap the library and configure keycloak-angular. In this example, I tried to allow all the urls with the regex /.*/ (but nothing change).


const keycloakService = new KeycloakService();

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    LoginComponent
  ],
  imports: [
    KeycloakAngularModule,
    BrowserModule,
    AppRoutingModule,
    CoreModule,
    SharedModule,
    PagesModule
  ],
  providers: [
    {
      provide: KeycloakService,
      useValue: keycloakService,
    }
  ],
  entryComponents: [AppComponent]
})
export class AppModule implements DoBootstrap {
  async ngDoBootstrap(app) {
    const { keycloakConfig } = environment;

    try {
      keycloakService.init({
        config: keycloakConfig,
        initOptions: {
          onLoad: 'login-required',
          checkLoginIframe: false
        },
        enableBearerInterceptor: true,
        bearerExcludedUrls: ['.*']
      }).then(() => {
        app.bootstrap(AppComponent);
      });
    } catch (error) {
      console.error('Keycloak init failed', error);
    }
  }
}


below, several links I have already visited

keycloak-angular github examples
A topic that raises a similar error with version 4.0.0 of the keycloak-service

like image 491
Ugo Evola Avatar asked Oct 27 '22 05:10

Ugo Evola


1 Answers

I've been fighting with keycloak for two weeks now and I believe that I had simmilar problem. What I've tried is to comment keycloak init options "onLoad: 'login-required'". After that I only have to login when I try to enter url which is protected by AuthGuard. For me "bearerExcludedUrls" didn't worked if 'login-required' is not commented out.

like image 174
Dainius Java Avatar answered Dec 26 '22 17:12

Dainius Java