enhance user storage jpa example

This commit is contained in:
Bill Burke 2016-12-03 13:33:16 -05:00
parent 88d08c4f38
commit 8b4bb72fb3
4 changed files with 75 additions and 7 deletions

View file

@ -1,14 +1,28 @@
Example User Storage Provider with EJB and JPA
===================================================
This is an example of the User Storage SPI implemented using EJB and JPA. To deploy this provider you must have Keycloak
running in standalone or standalone-ha mode. Then type the follow maven command:
This is an example of the User Storage SPI implemented using EJB and JPA. You must first deploy the datasource it uses.
Start up the Keycloak server. Then in the directory of this example type the following maven command:
mvn -Padd-datasource install
You only need to execute this maven command once. If you execute this again, then you will get an error message that the datasource
already exists.
If you open the pom.xml file you'll see that the add-datasource profile creates an XA datasource using the built
in H2 database that comes with the server. An XA datasource is required because you cannot use two non-xa datasources
in the same transaction. The Keycloak database is non-xa.
Another thing to note is that the xa-datasource created is in-memory only. If you reboot the server, any users you've
added or changes you've made to users loaded by this provider will be wiped clean.
To deploy the provider, run the following maven command:
mvn clean install wildfly:deploy
You can run as many times as you want and the provider will be redeployed.
Login and go to the User Federation tab and you should now see your deployed provider in the add-provider list box.
Add the provider, save it, then any new user you create will be stored and in the custom store you implemented. You
can modify the example and hot deploy it using the above maven command again.
This example uses the built in in-memory datasource that comes with keycloak: ExampleDS. NOTE!!! You'll have
to change this to be an xa datasource for this to work.

View file

@ -84,4 +84,58 @@
</plugin>
</plugins>
</build>
<profiles>
<!-- this profile deploys an XA H2 datasource connection. It is in-memory only, so rebooting the server will lose
your changes. Run type: mvn -Padd-datasource install. THis can only be run once. If you run it again
you'll get an error that the datasource already exists -->
<profile>
<id>add-datasource</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<force>true</force>
</configuration>
<executions>
<execution>
<id>add-datasource</id>
<phase>install</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<force/>
<address>subsystem=datasources</address>
<resources>
<resource>
<address>xa-data-source=java:jboss/datasources/ExampleXADS</address>
<properties>
<jndi-name>java:jboss/datasources/ExampleXADS</jndi-name>
<enabled>true</enabled>
<driver-name>h2</driver-name>
</properties>
<resources>
<resource>
<address>
xa-datasource-properties=URL
</address>
<properties>
<value>jdbc:h2:mem:test</value>
</properties>
</resource>
</resources>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View file

@ -5,7 +5,7 @@
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="user-storage-jpa-example" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/KeycloakDS</jta-data-source>
<jta-data-source>java:jboss/datasources/ExampleXADS</jta-data-source>
<class>org.keycloak.examples.storage.user.UserEntity</class>

View file

@ -117,7 +117,7 @@
<jmeter.analysis.plugin.version>1.0.4</jmeter.analysis.plugin.version>
<minify.plugin.version>1.7.2</minify.plugin.version>
<osgi.bundle.plugin.version>2.3.7</osgi.bundle.plugin.version>
<wildfly.plugin.version>1.0.1.Final</wildfly.plugin.version>
<wildfly.plugin.version>1.1.0.Beta1</wildfly.plugin.version>
<nexus.staging.plugin.version>1.6.5</nexus.staging.plugin.version>
<!-- Surefire Settings -->