Skip to content

Latest commit

 

History

History
1378 lines (1170 loc) · 52.4 KB

File metadata and controls

1378 lines (1170 loc) · 52.4 KB

jaxrs-jwt: Jakarta REST secured using JSON Web Tokens (JWTs)

The jaxrs-jwt quickstart demonstrates a Jakarta REST secured application using JSON Web Tokens (JWT) with Elytron.

What is it?

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 with customer role authority.

  • /rest/admin - Can be accessed by users with admin 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 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.

Use of the EAP_HOME and QUICKSTART_HOME Variables

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.

Back Up the JBoss EAP Standalone Server Configuration

Before you begin, back up your server configuration file.

  1. If it is running, stop the JBoss EAP server.

  2. Back up the EAP_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 JBoss EAP Standalone Server

  1. Open a terminal and navigate to the root of the JBoss EAP directory.

  2. Start the JBoss EAP server with the default profile by typing the following command.

    $ EAP_HOME/bin/standalone.sh 
    Note
    For Windows, use the EAP_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.

  1. Before you begin, make sure you do the following:

  2. 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.

    Important
    This 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.
  3. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing EAP_HOME with the path to your server:

    $ EAP_HOME/bin/jboss-cli.sh --connect --file=configure-elytron.cli
    Note
    For Windows, use the EAP_HOME\bin\jboss-cli.bat script.

    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 jku header parameter so the warnings can be ignored. When making use of a real token provider with a jwt-realm, token validation will fail if the token contains a jku header parameter and the allowed jku values have not been specified.

  4. Stop the JBoss EAP server.

Review the Modified Server Configuration

After stopping the server, open the EAP_HOME/standalone/configuration/standalone.xml file and review the changes.

  1. The following token-realm was added to the security-realms element in the elytron subsystem.

    <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>
  2. The following security-domain was added, which uses the jwt-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>
  3. The following HTTP authentication factory was added, which uses BEARER_TOKEN and the jwt-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>
  4. 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>
  5. 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>

Build and Deploy the Quickstart

  1. Make sure JBoss EAP server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type the following command to build the quickstart.

    $ mvn clean package
  4. 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.

Access the Application

The JwtAuthIT test shows how a client can authenticate with the server.

Run the Integration Tests

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.

  1. Make sure JBoss EAP server is started.

  2. Make sure the quickstart is deployed.

  3. Type the following command to run the verify goal with the integration-testing profile activated.

    $ mvn verify -Pintegration-testing 

Undeploy the Quickstart

When you are finished testing the quickstart, follow these steps to undeploy the archive.

  1. Make sure JBoss EAP server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type this command to undeploy the archive:

    $ mvn wildfly:undeploy

Restore the JBoss EAP Standalone Server Configuration

You can restore the original server configuration using either of the following methods.

Restore the JBoss EAP Standalone Server Configuration by Running the JBoss CLI Script

  1. Start the JBoss EAP server as described above.

  2. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing EAP_HOME with the path to your server:

    $ EAP_HOME/bin/jboss-cli.sh --connect --file=restore-configuration.cli
    Note
    For Windows, use the EAP_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 JBoss EAP 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.

  1. If it is running, stop the JBoss EAP server.

  2. Replace the EAP_HOME/standalone/configuration/standalone.xml file with the backup copy of the file.

Building and running the quickstart application with provisioned JBoss EAP server

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 target/server directory, and its usage is similar to a standard server distribution, with the simplification that there is never the need to specify the server configuration to be started.

Follow these steps to run the quickstart using the provisioned server.

Procedure
  1. Make sure the server is provisioned.

    $ mvn clean package
  2. Start the JBoss EAP provisioned server, using the WildFly Maven Plugin start goal.

    $ mvn wildfly:start 
  3. Type the following command to run the integration tests.

    $ mvn verify -Pintegration-testing 
  4. Shut down the JBoss EAP provisioned server.

    $ mvn wildfly:shutdown

Building and running the quickstart application with OpenShift

Build the JBoss EAP Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts

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.

Getting Started with JBoss EAP for OpenShift and Helm Charts

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.

Prerequisites

  • You must be logged in OpenShift and have an oc client 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 applications

Deploy the JBoss EAP Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts

Log 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: 1

This 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-jwt

The 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: 1

This 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/eap81

Get 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.

Run the Integration Tests with OpenShift

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.

Undeploy the JBoss EAP Source-to-Image (S2I) Quickstart from OpenShift with Helm Charts

$ helm uninstall jaxrs-jwt