Skip to content
A simple library to provide keycloak support to GWT
Java Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src/main/java/org/realityforge/gwt/keycloak
tasks Patch release process Feb 4, 2020
.gitattributes
.gitignore Add standard git configuration Oct 17, 2016
.ruby-version Move to ruby 2.3.1 Apr 18, 2017
.travis.yml
CHANGELOG.md Update CHANGELOG.md in preparation for next development iteration Feb 4, 2020
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
LICENSE Add LICENSE Oct 17, 2016
README.md Update README to reflect the 0.5 release Feb 4, 2020
build.yaml Reorder repositories so the jfrog cache is accessed first to avoid in… Feb 5, 2020
buildfile

README.md

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.5</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

You can’t perform that action at this time.