The jaxrs-jwt quickstart demonstrates a Jakarta REST secured application using JSON Web Tokens (JWT) with Elytron.
This quickstart demonstrates how to secure a Jakarta REST service with JWTs using the Elytron subsystem.
There are 4 resource endpoints, plus another one for generating JWTs.
-
/rest/public- Requires no authentication. -
/rest/customer- Can be accessed by users withcustomerrole authority. -
/rest/admin- Can be accessed by users withadminrole authority. -
/rest/claims- Can be accessed by any authenticated user and demonstrates how to extract token claims. -
/rest/token-POSTendpoint for generating tokens from provided credentials.
|
Note
|
This quickstart asserts only few JWT claims for demonstration purposes. In your application, you should use all claims required by the specification you are using. |
The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 8.1 or later.
All you need to build this project is Java SE 17.0 or later, and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.
In the following instructions, replace EAP_HOME with the actual path to your JBoss EAP installation. The installation path is described in detail here: Use of EAP_HOME and JBOSS_HOME Variables.
When you see the replaceable variable QUICKSTART_HOME, replace it with the path to the root directory of all of the quickstarts.
Before you begin, back up your server configuration file.
-
If it is running, stop the JBoss EAP server.
-
Back up the
EAP_HOME/standalone/configuration/standalone.xmlfile.
After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.
-
Open a terminal and navigate to the root of the JBoss EAP directory.
-
Start the JBoss EAP server with the default profile by typing the following command.
$ EAP_HOME/bin/standalone.shNoteFor Windows, use the EAP_HOME\bin\standalone.batscript.
You configure the security domain by running JBoss CLI commands. For your convenience, this quickstart batches the commands into a configure-elytron.cli script provided in the root directory of this quickstart.
-
Before you begin, make sure you do the following:
-
Back up the JBoss EAP standalone server configuration as described above.
-
Start the JBoss EAP server with the standalone default profile as described above.
-
-
Review the
configure-elytron.clifile in the root of this quickstart directory. This script adds the configuration that enables Elytron security for the quickstart deployment. Comments in the script describe the purpose of each block of commands.ImportantThis script contains placeholder PEM public key to make the deployment of this quickstart easy. DO not use this key for anything but testing purposes! You must generate your own key pair for your own application. -
Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing
EAP_HOMEwith the path to your server:$ EAP_HOME/bin/jboss-cli.sh --connect --file=configure-elytron.cliNoteFor Windows, use the EAP_HOME\bin\jboss-cli.batscript.You will see the following warnings when executing the script:
WFLYELY01090: Allowed jku values haven't been specified for token realm 'jwt-realm'. Token validation will fail if the token contains a 'jku' header parameter. The allowed jku values can be specified as a space separated string using the 'wildfly.elytron.jwt.allowed.jku.values.jwt-realm' system property. ELY01179: SSL not configured. jku claim will not be supported. ELY01182: Allowed jku values haven't been configured for the JWT validator. Token validation will fail if the token contains a 'jku' header parameter.The quickstart creates and makes use of a JWT that doesn’t include a
jkuheader parameter so the warnings can be ignored. When making use of a real token provider with ajwt-realm, token validation will fail if the token contains ajkuheader parameter and the allowed jku values have not been specified. -
Stop the JBoss EAP server.
After stopping the server, open the EAP_HOME/standalone/configuration/standalone.xml file and review the changes.
-
The following
token-realmwas added to thesecurity-realmselement in theelytronsubsystem.<token-realm name="jwt-realm" principal-claim="sub"> <jwt issuer="quickstart-jwt-issuer" audience="jwt-audience" key-store="jwt-key-store" certificate="jwt-auth"/> </token-realm>
-
The following
security-domainwas added, which uses thejwt-realm.<security-domain name="jwt-domain" default-realm="jwt-realm" permission-mapper="default-permission-mapper"> <realm name="jwt-realm" role-decoder="groups-to-roles"/> </security-domain>
-
The following HTTP authentication factory was added, which uses
BEARER_TOKENand thejwt-realm.<http-authentication-factory name="jwt-http-authentication" http-server-mechanism-factory="global" security-domain="jwt-domain"> <mechanism-configuration> <mechanism mechanism-name="BEARER_TOKEN"> <mechanism-realm realm-name="jwt-realm"/> </mechanism> </mechanism-configuration> </http-authentication-factory>
-
The application security domain in the Undertow subsystem is configured to use the new HTTP authentication factory.
<application-security-domains> <application-security-domain name="other" http-authentication-factory="jwt-http-authentication"/> </application-security-domains>
-
Finally, the application security domain in the EJB subsystem is configured to use the
jwt-domain.<application-security-domains> <application-security-domain name="other" security-domain="jwt-domain"/> </application-security-domains>
-
Make sure JBoss EAP server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type the following command to build the quickstart.
$ mvn clean package -
Type the following command to deploy the quickstart.
$ mvn wildfly:deploy
This deploys the jaxrs-jwt/target/jaxrs-jwt.war to the running instance of the server.
You should see a message in the server log indicating that the archive deployed successfully.
This quickstart includes integration tests, which are located under the src/test/ directory. The integration tests verify that the quickstart runs correctly when deployed on the server.
Follow these steps to run the integration tests.
-
Make sure JBoss EAP server is started.
-
Make sure the quickstart is deployed.
-
Type the following command to run the
verifygoal with theintegration-testingprofile activated.$ mvn verify -Pintegration-testing
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure JBoss EAP server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type this command to undeploy the archive:
$ mvn wildfly:undeploy
You can restore the original server configuration using either of the following methods.
-
You can run the
restore-configuration.cliscript provided in the root directory of this quickstart. -
You can manually restore the configuration using the backup copy of the configuration file.
-
Start the JBoss EAP server as described above.
-
Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing
EAP_HOMEwith the path to your server:$ EAP_HOME/bin/jboss-cli.sh --connect --file=restore-configuration.cliNoteFor Windows, use the EAP_HOME\bin\jboss-cli.batscript.
This script reverts the changes made to the undertow and elytron subsystem.You should see the following result when you run the script.
The batch executed successfully
process-state: reload-requiredWhen you have completed testing the quickstart, you can restore the original server configuration by manually restoring the backup copy the configuration file.
-
If it is running, stop the JBoss EAP server.
-
Replace the
EAP_HOME/standalone/configuration/standalone.xmlfile with the backup copy of the file.
Instead of using a standard JBoss EAP server distribution, you can alternatively provision a JBoss EAP server to deploy and run the quickstart. The functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml:
<profile>
<id>provisioned-server</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jboss.eap.plugins</groupId>
<artifactId>eap-maven-plugin</artifactId>
<configuration>
...
<feature-packs>
<feature-pack>
<location>org.jboss.eap:wildfly-ee-galleon-pack</location>
</feature-pack>
...
</feature-packs>
<layers>...</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>|
Note
|
When built, the provisioned JBoss EAP server can be found in the |
Follow these steps to run the quickstart using the provisioned server.
-
Make sure the server is provisioned.
$ mvn clean package -
Start the JBoss EAP provisioned server, using the WildFly Maven Plugin
startgoal.$ mvn wildfly:start -
Type the following command to run the integration tests.
$ mvn verify -Pintegration-testing -
Shut down the JBoss EAP provisioned server.
$ mvn wildfly:shutdown
On OpenShift, the S2I build with Apache Maven uses an openshift Maven profile to provision a JBoss EAP server, deploy and run the quickstart in OpenShift environment.
The server provisioning functionality is provided by the EAP Maven Plugin, and you may find its configuration in the quickstart pom.xml:
<profile>
<id>openshift</id>
<build>
<plugins>
<plugin>
<groupId>org.jboss.eap.plugins</groupId>
<artifactId>eap-maven-plugin</artifactId>
<configuration>
...
<feature-packs>
<feature-pack>
<location>org.jboss.eap:wildfly-ee-galleon-pack</location>
</feature-pack>
<feature-pack>
<location>org.jboss.eap.cloud:eap-cloud-galleon-pack</location>
</feature-pack>
</feature-packs>
<layers>...</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>You may note that it uses the cloud feature pack which enables a configuration tuned for the OpenShift environment.
This section contains the basic instructions to build and deploy this quickstart to JBoss EAP for OpenShift or JBoss EAP for OpenShift Online using Helm Charts.
-
You must be logged in OpenShift and have an
occlient to connect to OpenShift -
Helm must be installed to deploy the backend on OpenShift.
Once you have installed Helm, you need to add the repository that provides Helm Charts for JBoss EAP.
$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
"jboss-eap" has been added to your repositories
$ helm search repo jboss-eap
NAME CHART VERSION APP VERSION DESCRIPTION
jboss-eap/eap81 ... ... A Helm chart to build and deploy EAP 8.1 applicationsLog in to your OpenShift instance using the oc login command.
The backend will be built and deployed on OpenShift with a Helm Chart for JBoss EAP.
Navigate to the root directory of this quickstart and run the following command:
$ helm install jaxrs-jwt -f charts/helm.yaml jboss-eap/eap81 --wait --timeout=10m0s
NAME: jaxrs-jwt
...
STATUS: deployed
REVISION: 1This command will return once the application has successfully deployed. In case of a timeout, you can check the status of the application with the following command in another terminal:
oc get deployment jaxrs-jwtThe Helm Chart for this quickstart contains all the information to build an image from the source code using S2I on Java 17:
build:
uri: https://github.com/jboss-developer/jboss-eap-quickstarts.git
ref: 8.1.x
contextDir: jaxrs-jwt
deploy:
replicas: 1This will create a new deployment on OpenShift and deploy the application.
If you want to see all the configuration elements to customize your deployment you can use the following command:
$ helm show readme jboss-eap/eap81Get the URL of the route to the deployment.
$ oc get route jaxrs-jwt -o jsonpath="{.spec.host}"Access the application in your web browser using the displayed URL.
The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with the quickstart running on OpenShift.
|
Note
|
The integration tests expect a deployed application, so make sure you have deployed the quickstart on OpenShift before you begin. |
Run the integration tests using the following command to run the verify goal with the integration-testing profile activated and the proper URL:
$ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route jaxrs-jwt --template='{{ .spec.host }}') |
Note
|
The tests are using SSL to connect to the quickstart running on OpenShift. So you need the certificates to be trusted by the machine the tests are run from. |