The jaxrs-jwt
quickstart demonstrates a JAX-RS secured application using JSON Web Tokens (JWT) with Elytron.
What is it?
This quickstart demonstrates how to secure a JAX-RS 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 withcustomer
role authority. -
/rest/admin
- Can be accessed by users withadmin
role authority. -
/rest/claims
- Can be accessed by any authenticated user and demonstrates how to extract token claims. -
/rest/token
-POST
endpoint 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. |
System Requirements
The application this project produces is designed to be run on WildFly Application Server 12 or later.
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.
To run these quickstarts with the provided build scripts, you need the WildFly distribution ZIP. For information on how to install and run the WildFly server, see the Getting Started Guide for JBoss Enterprise Application Platform Continuous Delivery located on the Red Hat Customer Portal.
Use of WILDFLY_HOME
In the following instructions, replace WILDFLY_HOME
with the actual path to your WildFly installation. The installation path is described in detail here: Use of WILDFLY_HOME and JBOSS_HOME Variables.
Generate an RS256 Key Pair
Elytron uses RS256 (SHA256withRSA), RS384 (SHA384withRSA), and RS512 (SHA512withRSA) asymmetric keys for signing JWTs. The keys must be in PKCS#8 format.
You can generate your own RS256 key pair using java keytool.
-
Open a terminal and navigate to the WildFly server
configuration
directory:For Linux: standalone/configuration For Windows: standalone\configuration
-
Create a keystore for your server using the following command:
$>keytool -genkey -alias alias -keyalg RSA -keysize 2048 -keystore jwt.keystore -storepass secret -keypass secret What is your first and last name? [Unknown]: localhost What is the name of your organizational unit? [Unknown]: wildfly What is the name of your organization? [Unknown]: jboss What is the name of your City or Locality? [Unknown]: Raleigh What is the name of your State or Province? [Unknown]: Carolina What is the two-letter country code for this unit? [Unknown]: US Is CN=localhost, OU=wildfly, O=jboss, L=Raleigh, ST=Carolina, C=US correct? [no]: yes
Back Up the WildFly Standalone Server Configuration
Before you begin, back up your server configuration file.
-
If it is running, stop the WildFly server.
-
Back up the
WILDFLY_HOME/standalone/configuration/standalone.xml
file.
After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.
Start the WildFly Standalone Server
-
Open a terminal and navigate to the root of the WildFly directory.
-
Start the WildFly server with the default profile by typing the following command.
$ WILDFLY_HOME/bin/standalone.sh
NoteFor Windows, use the WILDFLY_HOME\bin\standalone.bat
script.
Configure the Server
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 WildFly standalone server configuration as described above.
-
Start the WildFly server with the standalone default profile as described above.
-
-
Review the
configure-elytron.cli
file 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
WILDFLY_HOME
with the path to your server:$ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=configure-elytron.cli
NoteFor Windows, use the WILDFLY_HOME\bin\jboss-cli.bat
script. -
Because this example quickstart demonstrates security, system exceptions are thrown when secured EJB access is attempted by an invalid user. If you want to review the security exceptions in the server log, you can skip this step. If you want to suppress these exceptions in the server log, run the following command, replacing
WILDFLY_HOME
with the path to your server:$ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=configure-system-exception.cli
NoteFor Windows,use the WILDFLY_HOME\bin\jboss-cli.bat
script.You should see the following result when you run the script:
The batch executed successfully
-
Stop the WildFly server.
Review the Modified Server Configuration
After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml
file and review the changes.
-
The following
token-realm
was added to thesecurity-realms
element in theelytron
subsystem.<token-realm name="jwt-realm" principal-claim="sub"> <jwt issuer="quickstart-jwt-issuer" audience="jwt-audience" key-store="jwt-key-store" certificate="alias"/> </token-realm>
-
The following
security-domain
was 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_TOKEN
and 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>
-
Finally, the application security domain is configured in Undertow to use the new HTTP authentication factory.
<application-security-domains> <application-security-domain name="other" http-authentication-factory="jwt-http-authentication"/> </application-security-domains>
Build and Deploy the Quickstart
-
Make sure you start the WildFly server as described above.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type the following command to build the artifacts.
$ mvn clean package 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.
Access the Application
Before you run the client, make sure you have already successfully deployed the REST to the server in the previous step and that your terminal is still in the same folder.
Type the following command to execute the client.
$ mvn exec:java
Investigate the Console Output
When you run the mvn exec:java
command, you see the following output.
------------------------------
Testing admin
------------------------------
Obtaining JWT...
Accessing /protected...
Status: 200
{"path":"protected","result":"Hello admin!"}
Accessing /public...
Status: 200
{"path":"public","result":"Hello admin!"}
Accessing /customer...
Status: 403
Accessing /claims...
Status: 200
{"sub":"admin","aud":["jwt-audience"],"iss":"quickstart-jwt-issuer","groups":["admin"],"exp":1519336360000}
------------------------------
Testing customer
------------------------------
Obtaining JWT...
Accessing /protected...
Status: 403
Accessing /public...
Status: 200
{"path":"public","result":"Hello customer!"}
Accessing /customer...
Status: 200
{"path":"customer","result":"Hello customer!"}
Accessing /claims...
Status: 200
{"sub":"customer","aud":["jwt-audience"],"iss":"quickstart-jwt-issuer","groups":["customer"],"exp":1519336360000}
------------------------------
Testing without token
------------------------------
Accessing /protected...
Status: 401
Accessing /public...
Status: 200
{"path":"public","result":"Hello anonymous!"}
Accessing /customer...
Status: 401
Accessing /claims...
Status: 204
The client tries to test service functionality using 3 identities.
-
admin
- this user belongs to groupadmin
, which gives him rights to access/rest/protected
-
customer
- this user belongs to groupcustomer
, which gives him rights to access/rest/customer
-
no credentials provided - the client tries to access all endpoints, but can only access unprotected
/rest/public
The endpoint /rest/claims
demonstrates a way, how you could extract token claims for further manipulation.
Undeploy the Quickstart
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure you start the WildFly server as described above.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type this command to undeploy the archive:
$ mvn wildfly:undeploy
Restore the WildFly Standalone Server Configuration
You can restore the original server configuration using either of the following methods.
-
You can run the
restore-configuration.cli
script provided in the root directory of this quickstart. -
You can manually restore the configuration using the backup copy of the configuration file.
Restore the WildFly Standalone Server Configuration by Running the JBoss CLI Script
-
Start the WildFly server as described above.
-
Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing
WILDFLY_HOME
with the path to your server:$ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=restore-configuration.cli
NoteFor Windows, use the WILDFLY_HOME\bin\jboss-cli.bat
script.
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-required
Restore the WildFly Standalone Server Configuration Manually
When 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 WildFly server.
-
Replace the
WILDFLY_HOME/standalone/configuration/standalone.xml
file with the backup copy of the file.
Debug the Application
If you want to debug the source code of any library in the project, run the following command to pull the source into your local repository. The IDE should then detect it.
$ mvn dependency:sources