Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/realityforge/gwt-keycloak
https://github.com/realityforge/gwt-keycloak
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/realityforge/gwt-keycloak
- Owner: realityforge
- License: apache-2.0
- Created: 2016-10-17T04:09:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-26T02:53:47.000Z (5 months ago)
- Last Synced: 2024-10-04T17:10:26.697Z (2 months ago)
- Language: Java
- Homepage:
- Size: 346 KB
- Stars: 9
- Watchers: 5
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- gwt-boot-awesome-lili - gwt-keycloak - A keycloak authentication adapter based on (Auth and Security)
- gwt-boot-awesome-lili - gwt-keycloak - A keycloak authentication adapter based on (Auth and Security)
README
# gwt-keycloak
[![Build Status](https://api.travis-ci.com/realityforge/gwt-keycloak.svg?branch=master)](http://travis-ci.com/realityforge/gwt-keycloak)
[](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.realityforge.gwt.keycloak%22%20a%3A%22gwt-keycloak%22)A simple library to provide keycloak support to GWT. The library wraps and adapts the
[keycloak adapter](https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_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.```xml
org.realityforge.gwt.keycloak
gwt-keycloak
0.14
provided```
Then you add the following snippet into the .gwt.xml file.
```xml
...
```
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:```java
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.```java
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.
```xml
...
```
The code to interact with the backend looks something like:
```java
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
* [Keycloak Javascript Adapter](https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter)