[keycloak-user] ClassNotFoundException: Custom UserStorageProvider

Danny Trunk dt at zyres.com
Thu Mar 2 05:30:52 EST 2017


I'm using the HibernatePersistenceProvider to create an 
EntityManagerFactory in order to configure url, username, password, ... 
as ProviderConfigProperty. This way I can omit the persistence.xml file 
and configure the connection properties in the admin console, which is 
better than editing XML files on the server to add datasources.

Before I already tried the user-storage-jpa example and failed at using 
another datasource in the provider than keycloak internally uses. 
Keycloak should use it's own local datasource and the user storage 
provider should use another, external postgres db.

Am 02.03.2017 um 11:02 schrieb Marek Posolda:
> Hi,
>
> it seems that it is Hibernate, which is not able to find your classes. 
> I guess that you are trying to configure JDBC URL, user and password 
> directly in persistence.xml and that's maybe an issue.
>
> I suggest to rather configure the datasource in standalone.xml and 
> then use the property "jta-data-source" in your persistence.xml 
> pointing to that. Besides classloading issues, another advantage is, 
> that you will automatically have connection-pooling, connection 
> liveness checks (optional) etc. See our example 
> "providers/user-storage-jpa" for inspiration, which is doing the same.
>
> Marek
>
> On 02/03/17 10:42, Danny Trunk wrote:
>> Hello,
>>
>> I've implemented a custom User Storage Provider to connect to a
>> configurable (external) database through Hibernate/JDBC:
>>
>> public class MyUserStorageProviderFactory implements
>> UserStorageProviderFactory<MyUserStorageProvider> {
>>       // ...
>>       public MyUserStorageProvider create(KeycloakSession session,
>> ComponentModel model) {
>>           logger.info(">>>>>> Creating factory");
>>           PersistenceConfig config = new
>> PersistenceConfig(model.getConfig());
>>           entityManagerFactory = new
>> HibernatePersistenceProvider().createContainerEntityManagerFactory(getPersistenceUnitInfo(),
>> config.asProperties());
>>           entityManager = entityManagerFactory.createEntityManager();
>>           return new MyUserStorageProvider(entityManager, session, model);
>>       }
>>       // ...
>> }
>>
>> In src/main/resources/META-INF I've placed a file named
>> jboss-deployment-structure.xml:
>> <?xml version="1.0" ?>
>> <jboss-deployment-structure>
>>       <deployment>
>>           <dependencies>
>>               <module name="org.hibernate" />
>>               <module name="org.jboss.logging" />
>>               <module name="org.keycloak.keycloak-core" />
>>               <module name="org.keycloak.keycloak-server-spi" />
>>               <module name="org.postgresql" />
>>           </dependencies>
>>       </deployment>
>> </jboss-deployment-structure>
>>
>> Although there's a dependency for org.postgresql I'm getting a
>> ClassNotFoundException when trying to authenticate:
>> WARN  [org.keycloak.services] (default task-6) KC-SERVICES0013: Failed
>> authentication: org.hibernate.service.spi.ServiceException: Unable to
>> create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
>>           ...
>> Caused by:
>> org.hibernate.boot.registry.classloading.spi.ClassLoadingException:
>> Unable to load class [org.postgresql.Driver]
>>           ...
>> Caused by: java.lang.ClassNotFoundException: Could not load requested
>> class : org.postgresql.Driver
>>
>> PostgreSQL is deployed as module as described here:
>> https://keycloak.gitbooks.io/server-installation-and-configuration/content/topics/database/jdbc.html
>> _______________________________________________
>> keycloak-user mailing list
>> keycloak-user at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/keycloak-user
>
>


More information about the keycloak-user mailing list