Skip to content

realityforge/gwt-keycloak

Repository files navigation

gwt-keycloak

Build Status

A simple library to provide keycloak support to GWT. The library wraps and adapts the keycloak adapter that is deployed on the keycloak server.

Quick Start

The simplest way to use the library is to add the following dependency into the build system. i.e.

<dependency>
   <groupId>org.realityforge.gwt.keycloak</groupId>
   <artifactId>gwt-keycloak</artifactId>
   <version>0.14</version>
   <scope>provided</scope>
</dependency>

Then you add the following snippet into the .gwt.xml file.

<module rename-to='myapp'>
  ...

  <!-- Enable the keycloak library -->
  <inherits name="org.realityforge.gwt.keycloak.Keycloak"/>
</module>

Then you can interact with the Keycloak object from within the browser.

First you need to setup a Keycloak instance. Assuming your keycloak server has an application client named "MyApp" and the configuration json is at http://127.0.0.1:8080/myapp/keycloak.json then you end up with code like:

final Keycloak keycloak = new Keycloak( "MyApp", "http://127.0.0.1:8080/myapp/keycloak.json" );

keycloak.setListener( new KeycloakListenerAdapter()
{
  @Override
  public void onReady( @Nonnull final Keycloak keycloak, final boolean authenticated )
  {
    if( authenticated )
    {
      //Already authenticated, start app here
    }
    else
    {
      keycloak.login();
    }
  }
} );

keycloak.init();

When the token is needed to interact with a keycloak protected resource you can simply use the following code to access the current token, updating it if it is stale and needs to be refreshed.

final int minTokenValiditySeconds = 30;
keycloak.updateToken( minTokenValiditySeconds, () -> remoteCallUsingToken( keycloak.getToken() ) );

This should be sufficient to put together a simple Keycloak application.

Quick Start using token caching

You can also use the library to cache tokens local in web storage apis (i.e. local storage or session storage if available). These tokens will be revalidated locally and updated (assuming you use the correct listener) when necessary. This results in a much better user experience as the network overhead is significantly reduced.

First you add dependency as above, then you add a snippet like the following into the .gwt.xml file.

<module rename-to='myapp'>
  ...

  <!-- Enable the keycloak library -->
  <inherits name="org.realityforge.gwt.keycloak.cache.TokenCache"/>
</module>

The code to interact with the backend looks something like:

final Keycloak keycloak = new Keycloak( "MyApp", "http://127.0.0.1:8080/myapp/keycloak.json" );
TokenCache.configure( keycloak );

keycloak.setListener( new TokenCachingListener()
{
  @Override
  public void onReady( @Nonnull final Keycloak keycloak, final boolean authenticated )
  {
    super.onReady( keycloak, authenticated );
    if( authenticated )
    {
      //Already authenticated, start app here
    }
    else
    {
      keycloak.login();
    }
  }
} );

keycloak.init();

Appendix

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •