improvements: remove ear, use keycloak official image, and update readme

This commit is contained in:
zak905 2021-12-13 21:16:31 +01:00 committed by Zakaria Amine
parent 9d816c5f60
commit 2ec76fe137
10 changed files with 70 additions and 170 deletions

View file

@ -1,3 +1,13 @@
Running: `mvn package && docker-compose up`
## How to run
you can run the project by running the following from a terminal: `mvn -f api-key-module package && mvn -f dashboard-service package && docker-compose up`
Note: You need to add `auth-server` to your hosts file (`/etc/hosts` for linux) and map it to localhost.
## Testing
1. Navigate to localhost:8180 in a browser, you will redirected to keycloak for authentication
2. you need register a new user, after which you will be redirected to the main dashboard page which will show your API key
3. copy the API key and use it to call the API: `curl -v -H "x-api-key: $THE_API_KEY" localhost:8200`, if you omit the API key, you will get 401 status
More explanations can be found in this blog [post](http://www.zakariaamine.com/2019-06-14/extending-keycloak)

View file

@ -1,19 +0,0 @@
FROM openjdk:11-jre-slim
WORKDIR /keycloak
ENV KEYCLOAK_VERSION 12.0.4
RUN apt-get update && apt-get install -y wget && wget https://github.com/keycloak/keycloak/releases/download/${KEYCLOAK_VERSION}/keycloak-${KEYCLOAK_VERSION}.tar.gz
RUN tar xvf keycloak-${KEYCLOAK_VERSION}.tar.gz --strip-components=1 && rm keycloak-${KEYCLOAK_VERSION}.tar.gz
#add admin user
RUN ./bin/add-user-keycloak.sh -u admin -p admin --realm master
COPY target/api-key-ear-0.1.ear standalone/deployments
EXPOSE 8080
ENTRYPOINT ["./bin/standalone.sh", "-b", "0.0.0.0"]

View file

@ -1,80 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-keycloak-example</artifactId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-key-ear</artifactId>
<packaging>ear</packaging>
<properties>
<dockerfile-maven-version>1.4.3</dockerfile-maven-version>
</properties>
<dependencies>
<dependency>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-module</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<jarModule>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-module</artifactId>
<includeInApplicationXml>true</includeInApplicationXml>
<bundleFileName>api-key-module.jar</bundleFileName>
<bundleDir>/</bundleDir>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<jarModule>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-module</artifactId>
<includeInApplicationXml>true</includeInApplicationXml>
<bundleFileName>api-key-module.jar</bundleFileName>
<bundleDir>/</bundleDir>
</jarModule>
</modules>
<outputDirectory>
${project.build.directory}/deploy
</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View file

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<module-alias name="deployment.api-key"/>
</deployment>
<sub-deployment name="api-key-module.jar">
<dependencies>
<module name="javax.persistence.api"/>
<module name="javax.servlet.api"/>
<module name="javax.transaction.api"/>
<module name="javax.ws.rs.api"/>
<module name="org.jboss.resteasy.resteasy-jaxb-provider"/>
<module name="org.jboss.resteasy.resteasy-jaxrs"/>
<module name="org.jboss.resteasy.resteasy-multipart-provider"/>
<module name="org.javassist"/>
<module name="org.hibernate"/>
<module name="org.keycloak.keycloak-core"/>
<module name="org.keycloak.keycloak-server-spi"/>
<module name="org.keycloak.keycloak-server-spi-private"/>
<module name="org.keycloak.keycloak-model-jpa"/>
<module name="org.keycloak.keycloak-model-infinispan"/>
<module name="org.keycloak.keycloak-services"/>
</dependencies>
</sub-deployment>
</jboss-deployment-structure>

View file

@ -2,18 +2,15 @@
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-keycloak-example</artifactId>
<version>0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-key-module</artifactId>
<groupId>com.gwidgets</groupId>
<version>0.1</version>
<properties>
<java.version>11</java.version>
<keycloak.version>12.0.4</keycloak.version>
<keycloak.version>15.0.2</keycloak.version>
</properties>
<dependencies>
@ -47,4 +44,44 @@
<version>1.11.538</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Dependencies>org.keycloak.keycloak-server-spi-private,org.keycloak.keycloak-server-spi,org.keycloak.keycloak-services</Dependencies>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}/deploy</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,20 +1,20 @@
FROM maven:3-openjdk-11-slim as builder
#FROM maven:3-openjdk-11-slim as builder
RUN mkdir app
#RUN mkdir app
WORKDIR app
#WORKDIR app
COPY src src
COPY pom.xml .
#COPY src src
#COPY pom.xml .
RUN mvn clean package
#RUN mvn clean package
FROM openjdk:11-jre-slim
ARG VERSION=0.1
COPY --from=builder /app/target/dashboard-service-${VERSION}.jar app.jar
COPY ./target/dashboard-service-${VERSION}.jar app.jar
EXPOSE 8080
EXPOSE 8180
CMD ["java", "-jar", "app.jar"]

View file

@ -16,7 +16,7 @@
<properties>
<java.version>11</java.version>
<keycloak.version>12.0.3</keycloak.version>
<keycloak.version>15.0.2</keycloak.version>
</properties>
<dependencies>

View file

@ -1,13 +1,14 @@
version: '3.3'
version: '3.7'
services:
auth-server:
build: api-key-ear
image: jboss/keycloak:15.0.2
environment:
REALM_NAME: example
command: ["-Dkeycloak.migration.action=import", "-Dkeycloak.migration.provider=dir", "-Dkeycloak.migration.dir=/import", "-Dkeycloak.profile.feature.upload_scripts=enabled", "-Dkeycloak.migration.strategy=OVERWRITE_EXISTING"]
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
JAVA_OPTS_APPEND: "-Dkeycloak.migration.action=import -Dkeycloak.migration.provider=dir -Dkeycloak.migration.dir=/import -Dkeycloak.migration.strategy=IGNORE_EXISTING -Dkeycloak.profile.feature.upload_scripts=enabled"
volumes:
- ./api-key-ear/import:/import
- ./api-key-ear/target/deploy:/keycloak/standalone/deployments
- ./import:/import
- ./api-key-module/target/deploy:/opt/jboss/keycloak/standalone/deployments/
ports:
- "8080:8080"
dashboard-service:

24
pom.xml
View file

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<groupId>com.gwidgets</groupId>
<artifactId>api-key-keycloak-example</artifactId>
<version>0.1</version>
<modules>
<module>api-key-module</module>
<module>api-key-ear</module>
</modules>
<packaging>pom</packaging>
</project>