Keycloak provider and user storage

2019-05-22 05:03发布

问题:

I have a running java ee application and now i want to integrate keycloak as authentication server. The only thing i have troubles is the user storage. I want to have all the user data in my java application.

The problem now: If the user registers on the keycloak frontend, my java application doesn´t know that the user has registered, so i cannot create a new entity. I found out that keycloak is able to load some custom modules (https://keycloak.github.io/docs/userguide/keycloak-server/html/providers.html) but i haven´t found any examples.

Is there any solution where keycloak notifys my java application when the user registered?

回答1:

I have had the same problem and I have resolved it using a filter. I just check if the principal exist and if not I insert it into my application DB:

KeycloakSecurityContext ctx = (KeycloakSecurityContext)request.getAttribute(KeycloakSecurityContext.class.getName()); User userEntity = em.find(User.class, ctx.getToken().getSubject()); if (userEntity == null) { ....create user... } You can also use an event listener (keycloak events listener) as shown in Example Event Listener that prints events to System.out, but for this exact use case that solution was easier and faster.



回答2:

You have to implement a custom Authentication SPI (inside success() method you create user on your app), deployed it , and add it to the registration flow

Keycloak documentation : Link