How to initialize several UserProviders in Keycloak

Viewed 742

I'm having big difficulties when trying to make Keycloak to use default JPA provider that is implemented in Keycloak (package: org.keycloak.models.jpa.JpaUserProviderFactory) alongside with my custom UserProvider.

My user provider is implemented like this:

public class MyCustomUserProviderFactory extends JpaUserProviderFactory {
  @Override
  public String getId() {
    return "demo";
  }

  @Override
  public UserProvider create(KeycloakSession session) {
    EntityManager em = session.getProvider(JpaConnectionProvider.class).getEntityManager();
    return new MyCustomUserProvider(session, em);
  }
}

and MyCustomUserProvider

public class MyCustomUserProvider extends JpaUserProvider {
  private final KeycloakSession session;

  public MyCustomUserProvider(KeycloakSession session, EntityManager em) {
    super(session, em);
    this.session = session;
  }

}

startup.cli

/subsystem=keycloak-server/spi=user:add(default-provider=demo)
/subsystem=keycloak-server/spi=user/provider=jpa:add(enabled=true)
/subsystem=keycloak-server/spi=user/provider=demo:add(enabled=true)

also in META-INF/service i have

org.keycloak.models.UserProviderFactory

with com.demo.keycloak.providers.user.MyCustomUserProviderFactory

So with this implementation and settings, keycloak is started successfully but when I need to access both demo and jpa UserProvider I only get default provider that is in this case demo.

I access UserProvider with:

UserProvider demoProvider = session.getProvider(UserProvider.class, "demo");

but if I execute

UserProvider jpaProvider = session.getProvider(UserProvider.class, "jpa");

I only get null. If I change the default provider in the startup.cli (default-provider=demo) I would then get the jpa but not the demo provider.

Can somebody explan to me how could I have several UserProvider implementation and how can I access to both of them?

1 Answers

Sorry for not answering on my question after quite a long time.

Unfortunately there is no way of having multiple JPAUserProvider at the same time.

The only way to add custom implementation in JpaUserProvider methods is to override them add the additional call and in the configuration itself add the custom version as a default one /subsystem=keycloak-server/spi=user:add(default-provider=demo).

Related