title: Angular 2, Keycloak: securing certain routes
body: <p>I have <strong>Angular 2</strong> application secured by <strong>Keycloak</strong>, done by example <a href="https://github.com/keycloak/keycloak/tree/fc6d6ff7f7dae7fb25edf052659d18cd8de55a5f/examples/demo-template/angular2-product-app" rel="nofollow noreferrer">here</a>. </p>
  
  <p><strong>I wonder, how can I secure just certain routes.</strong> </p>
  
  <p><em>main.ts:</em> </p>
  
  <pre><code> KeycloakService.init()
   .then(() =&gt; {
     const platform = platformBrowserDynamic();
     platform.bootstrapModule(AppModule);
   })
   .catch(() =&gt; window.location.reload());
  </code></pre>
  
  <p><em>keycloak.service.ts:</em></p>
  
  <pre><code>@Injectable()
  export class KeycloakService {
  
    static auth: any = {};
  
    static loginUser: KeycloakUser;
  
    static init(): Promise&lt;any&gt; {
  
    let keycloakAuth: Keycloak.KeycloakInstance = Keycloak('keycloak/keycloak.json');
  
    KeycloakService.auth.loggedIn = false;
  
    return new Promise((resolve, reject) =&gt; {
      keycloakAuth.init({ onLoad: 'login-required' })
      .success(() =&gt; {
        KeycloakService.auth.loggedIn = true;
        KeycloakService.auth.authz = keycloakAuth;
  
        KeycloakService.auth.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/protocol/openid-connect/logout?redirect_uri=/angular2-product/index.html";
        resolve();
      })
      .error(() =&gt; {
        reject();
      });
     });
   }
   ....
  </code></pre>