Skip to content

Commit

Permalink
Update user-storage-jpa to work with the new distribution
Browse files Browse the repository at this point in the history
Closes #303
  • Loading branch information
pedroigor authored and stianst committed Apr 27, 2022
1 parent eb76620 commit 857775b
Show file tree
Hide file tree
Showing 21 changed files with 902 additions and 153 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
<module>fuse70</module>
<module>user-storage-simple</module>
<module>user-storage-jpa</module>
<module>user-storage-jpa-legacy</module>
<module>event-listener-sysout</module>
<module>event-store-mem</module>
<module>extend-account-console</module>
Expand Down
2 changes: 1 addition & 1 deletion scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ run_tests fuse63 # no tests but at least let's try to compile it
run_tests fuse70 # no tests but at least let's try to compile it
run_tests service-jee-jaxrs -Pwildfly-managed
run_tests service-springboot-rest -Pspring-boot
run_tests user-storage-jpa -Pkeycloak-remote
run_tests user-storage-jpa-legacy -Pkeycloak-remote
run_tests user-storage-simple -Pkeycloak-remote

mvn -f service-springboot-rest spring-boot:run >/dev/null&
Expand Down
81 changes: 81 additions & 0 deletions user-storage-jpa-legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
user-storage-jpa: User Storage Provider with EJB and JPA
========================================================

Level: Beginner
Technologies: JavaEE, EJB, JPA
Summary: User Storage Provider with EJB and JPA
Target Product: <span>Keycloak</span>
Source: <https://github.com/keycloak/keycloak-quickstarts>


What is it?
-----------

This is an example of the User Storage SPI implemented using EJB and JPA. It shows you how you might use these components
to integrate <span>Keycloak</span> with an existing external custom user database. The example integrates with a simple relational
database schema that has one user table that stores a username, email, phone number, and password for one particular user.
Using the User Storage SPI this table is mapped to the <span>Keycloak</span> user metamodel so that it can be consumed by the <span>Keycloak</span>
runtime. Before using this example, you should probably read the User Storage SPI chapter of our server developer guide.


System Requirements
-------------------

You need to have <span>Keycloak</span> running.

All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.3 or later.


Build and Deploy the Quickstart
-------------------------------

You must first deploy the datasource it uses.
Start up the <span>Keycloak</span> server. Then in the directory of this example type the following maven command:

````
mvn -Padd-datasource install
````

**Note**: If the server runs on different port than `10090`, you have to specify it by setting a
maven property `keycloak.management.port`.

````
mvn -Padd-datasource install -Dkeycloak.management.port=9990
````

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 <span>Keycloak</span> 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
````

If you want to play with and modify the example, simply rerun the maven deploy command above and the new version will be hot deployed.

Enable the Provider for a Realm
-------------------------------
Login to the <span>Keycloak</span> Admin Console and got to the User Federation tab. You should now see your deployed provider in the add-provider list box.
Add the provider, save it. This will now enable the provider for the 'master' realm. Because this provider implements the UserRegistrationProvider interface, any new user you create in the
admin console or on the registration pages of <span>Keycloak</span>, will be created in the custom store used by the provider. If you go
to the Users tab in the Admin Console and create a new user, you'll be able to see the provider in action.

Integration test of the Quickstart
----------------------------------

1. Make sure you have an Keycloak server running with an admin user in the `master` realm or use the provided docker image.
2. You need to have Chrome browser installed and updated to the latest version.
3. Run `mvn test -Pkeycloak-remote`. (The datasource will be deployed automatically.)

More Information
----------------
The User Storage SPI and how you can use Java EE to implement it is covered in detail in our server developer guide.

200 changes: 200 additions & 0 deletions user-storage-jpa-legacy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<!--
~ Copyright 2016 Red Hat, Inc. and/or its affiliates
~ and other contributors as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.keycloak.quickstarts</groupId>
<artifactId>keycloak-quickstart-parent</artifactId>
<version>18.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>keycloak-user-storage-jpa-legacy</artifactId>
<packaging>jar</packaging>
<name>Keycloak Quickstart: user-storage-jpa-legacy</name>
<description>User Storage Provider with EJB and JPA</description>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<version.compiler.maven.plugin>3.5.1</version.compiler.maven.plugin>
<version.hibernate.javax.persistence>1.0.0.Final</version.hibernate.javax.persistence>
<version.jboss-ejb-api>1.0.0.Final</version.jboss-ejb-api>
</properties>

<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${version.hibernate.javax.persistence}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
<version>${version.jboss-ejb-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.graphene</groupId>
<artifactId>graphene-webdriver</artifactId>
<version>${arquillian-graphene.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly.extras.creaper</groupId>
<artifactId>creaper-core</artifactId>
<version>${version.creaper}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-cli</artifactId>
<scope>test</scope>
<version>${version.wildfly}</version>
</dependency>

</dependencies>

<build>
<finalName>user-storage-jpa-example-legacy</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.compiler.maven.plugin}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
<configuration>
<skip>false</skip>
<port>${keycloak.management.port}</port>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-quickstart-realm-file-exist</id>
<phase>validate</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<configuration>
<systemPropertyVariables>
<keycloakManagementPort>${keycloak.management.port}</keycloakManagementPort>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</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>
<port>${keycloak.management.port}</port>
<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>
Loading

0 comments on commit 857775b

Please sign in to comment.