The helloworld-mutual-ssl-secured quickstart demonstrates securing a Web application using client certificate authentication with authorization

What is it?

This example demonstrates the configuration of mutual TLS authentication in WildFly Application Server 33 to secure a war application.

Mutual TLS provides the same security as TLS, with the addition of authentication and non-repudiation of the client authentication, using digital signatures. When mutual authentication is used, the server would request the client to provide a certificate in addition to the server certificate issued to the client. Mutual authentication requires an extra round trip time for client certificate exchange. In addition, the client must obtain and maintain a digital certificate. We can secure our war application deployed over WildFly with mutual(two-way) client certificate authentication and provide access permissions or privileges to legitimate users.

The out of the box configuration for WildFly has one-way TLS enabled by default. This quickstart shows how to configure WildFly with mutual (two-way) TLS authentication in order to secure a WAR application with restricted access.

System Requirements

The application this project produces is designed to be run on WildFly Application Server 33 or later.

All you need to build this project is Java 11.0 (Java SDK 11) 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 WILDFLY_HOME and QUICKSTART_HOME Variables

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.

When you see the replaceable variable QUICKSTART_HOME, replace it with the path to the root directory of all of the quickstarts.

Add the Authorized Application User

This quickstart uses secured application interfaces and requires that you create the following application user to access the running application.

UserName Realm Password Roles

quickstartUser

ApplicationRealm

quickstartPwd1!

JBossAdmin

To add the application user, open a terminal and type the following command:

$ WILDFLY_HOME/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'JBossAdmin'
Note
For Windows, use the WILDFLY_HOME\bin\add-user.bat script.
Important
For the purpose of this quickstart the password can contain any valid value because the ApplicationRealm will be used for authorization only, for example, to obtain the security roles.

Back Up the WildFly Standalone Server Configuration

Before you begin, back up your server configuration file.

  1. If it is running, stop the WildFly server.

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

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

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

    $ WILDFLY_HOME/bin/standalone.sh 
    Note
    For Windows, use the WILDFLY_HOME\bin\standalone.bat script.

Set Up the Client Keystore

  1. Create the client certificate, which is used to authenticate against the server when accessing a resource through TLS.

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

The certificate and keystore are now properly configured.

Configure the Server

You configure the SSL context and required security domain by running JBoss CLI commands. For your convenience, this quickstart batches the commands into a configure-ssl.cli script provided in the root directory of this quickstart.

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

  2. Review the configure-ssl.cli file in the root of this quickstart directory. Comments in the script describe the purpose of each block of commands.

  3. 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-ssl.cli
    Note
    For 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
    process-state: reload-required
  4. 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.

  1. The following key-store was added to the elytron subsystem:

    <key-store name="qsTrustStore">
        <credential-reference clear-text="secret"/>
        <implementation type="JKS"/>
        <file path="server.truststore" relative-to="jboss.server.config.dir"/>
    </key-store>
  2. The following trust-manager was added to the elytron subsystem:

    <trust-managers>
        <trust-manager name="qsTrustManager" key-store="qsTrustStore"/>
    </trust-managers>
  3. The default ssl-context was updated to reference the trust-manager to enable two-way TLS:

    <server-ssl-contexts>
        <server-ssl-context name="applicationSSC" need-client-auth="true" key-manager="applicationKM" trust-manager="qsTrustManager"/>
    </server-ssl-contexts>

    Note that the https-listener in the undertow subsystem references the applicationSSC server-ssl-context by default.

  4. The following realms were added to the elytron subsystem:

    <key-store-realm name="KeyStoreRealm" key-store="qsTrustStore"/>
    
    <aggregate-realm name="QuickstartRealm" authentication-realm="KeyStoreRealm" authorization-realm="ApplicationRealm"/>

    The aggregate-realm defines different security realms for authentication and authorization. In this case, the KeyStoreRealm is responsible for authenticating the principal extracted from the client’s certificate and the ApplicationRealm is responsible for obtaining the roles required to access the application.

  5. The following principal-decoder and security-domain were added to the elytron subsystem:

    <x500-attribute-principal-decoder name="QuickstartDecoder" attribute-name="cn"/>
    
    <security-domain name="QuickstartDomain" default-realm="QuickstartRealm" permission-mapper="default-permission-mapper" principal-decoder="QuickstartDecoder">
        <realm name="QuickstartRealm" role-decoder="groups-to-roles"/>
    </security-domain>

    The x500-attribute-principal-decoder creates a new Principal from the CN attribute of the X500Principal obtained from the client’s certificate. This new principal is supplied to the security realms and is also the principal returned in methods like getUserPrincipal and getCallerPrincipal.

  6. The following http-authentication-factory was added to the elytron subsystem:

    <http-authentication-factory name="quickstart-http-authentication" http-server-mechanism-factory="global" security-domain="QuickstartDomain">
        <mechanism-configuration>
            <mechanism mechanism-name="CLIENT_CERT"/>
        </mechanism-configuration>
    </http-authentication-factory>

    It defines the security domain that will handle requests using the CLIENT_CERT HTTP mechanism.

  7. The following application-security-domain was added to the undertow subsystem:

    <application-security-domains>
        <application-security-domain name="client_cert_domain" http-authentication-factory="quickstart-http-authentication"/>
    </application-security-domains>

    It maps the client_cert_domain from the quickstart application to the http-authentication-factory shown above, so requests made to the application go through the configured HTTP authentication factory.

Test the Server TLS Configuration

To test the TLS configuration, start WildFly and access: https://localhost:8443

If it is configured correctly, you should be asked to trust the server certificate.

Import the Certificate into Your Browser

Before you access the application, you must import the client.keystore.P12, which holds the client certificate, into your browser.

Import the Certificate into Google Chrome

  1. Click the Chrome menu icon (3 dots) in the upper right on the browser toolbar and choose Settings. This takes you to link:`chrome://settings/.

  2. Click on Privacy and security and then on Security.

  3. Scroll down to the Advanced section and on the Manage certificates screen, select the Your Certificates tab and click on the Import button.

  4. Select the client.keystore.p12 file. You will be prompted to enter the password: secret.

  5. The client certificate is now installed in the Google Chrome browser.

Import the Certificate into Mozilla Firefox

  1. Click the Edit menu item on the browser menu and choose Settings.

  2. A new window will open. Click on Privacy & Security and scroll down to the Certificates section.

  3. Click the View Certificates button.

  4. A new window will open. Select the Your Certificates tab and click the Import button.

  5. Select the client.keystore.p12 file. You will be prompted to enter the password: secret.

  6. The certificate is now installed in the Mozilla Firefox browser.

Build and Deploy the Quickstart

  1. Make sure WildFly 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 helloworld-mutual-ssl-secured/target/helloworld-mutual-ssl-secured.war to the running instance of the server.

You should see a message in the server log indicating that the archive deployed successfully.

If mutual TLS is configured properly and the WAR application is secured, you will be able to access the application only if the DN of client certificate, for example client.keystore.p12, is same as the one defined in app-roles.properties file. Otherwise, it will result in an HTTP error status code of 403 Access Denied/Forbidden.

Access the Application

The application will be running at the following URL: https://localhost:8443/helloworld-mutual-ssl-secured

A page displaying the caller principal and the client certificate used for mutual TLS should be visible:

Hello World ! Mutual TLS client authentication is successful and your war app is secured.!!

Caller Principal: quickstartUser

Client Certificate Pem: MIIDhTCCAm2gAwIBAgIEf9lc5DANBgkqhkiG9w0BAQsFADBzMQswCQYDVQQGEwJCUjESMBAGA1UECBMJU2FvIFBhdW
xvMRIwEAYDVQQHEwlTYW8gUGF1bG8xEzARBgNVBAoTCk15IENvbXBhbnkxDjAMBgNVBAsTBVNhbGVzMRcwFQYDVQQDEw5xdWlja3N0YXJ0VXNlcjAe
Fw0xNzA3MjQxOTE0MDNaFw0xODA3MjQxOTE0MDNaMHMxCzAJBgNVBAYTAkJSMRIwEAYDVQQIEwlTYW8gUGF1bG8xEjAQBgNVBAcTCVNhbyBQYXVsbz
ETMBEGA1UEChMKTXkgQ29tcGFueTEOMAwGA1UECxMFU2FsZXMxFzAVBgNVBAMTDnF1aWNrc3RhcnRVc2VyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAnHwflE8K/ArTPbTeZZEFK+1jtpg9grPSD62GIz/awoIDr6Rf9vCBTpAg4lom62A0BNZDEJKdab/ExNOOBRY+/pOnYlXZTYlDpdQQap
0E7UP5EfHNZsafgpfILCop2LdTuUbcV7tLKBsthJLJ0ZCoG5QJFble+OPxEbissOvIqHfvUJZi34k9ULteLJc330g0uTuDrLgtoFQ0cbHa4FCQ86o8
5EuRPpFeW6EBA3iYE/tKHSYsK7QSajefX6jZjXoZiUflw97SAGL43ZtvNbrKRywEfsVqDpDurjBg2HI+YahuDz5R1QWTSyTHWMZzcyJYqxjXiSf0oK
1cUahn6m5t1wIDAQABoyEwHzAdBgNVHQ4EFgQUlYS+xjK7KxNMf13UxMgiEssJOQkwDQYJKoZIhvcNAQELBQADggEBADkp+R6kSNXJNfihqbDRp3uF
tNMG6OgaYsfC7RtNLMdrhvoLlU7uWzxVCFuifvNlWVRiADBHDCRQU2uNRFW35GQSfHQyok4KoBuKlfBtQ+Xu7c8R0JzxN/rPJPXoCbShzDHo1uoz5/
dzXZz0EjjWCPJk+LVEhEvH0GcWAp3x3irpNU4hRZLd0XomY0Z4NnUt7VMBNYDOxVxgT9qcLnEaEpIfYULubLLCFHwAga2YgsKzZYLuwMaEWK4zhPVF
ynfnMaOxI67FC2QzhfzERyKqHj47WuwN0xWbS/1gBypS2nUwvItyxaEQG2X5uQY8j8QoY9wcMzIIkP2Mk14gJGHUnA8=

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 WildFly 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 -Dserver.dir=__WILDFLY_HOME__

Undeploy the Quickstart

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

  1. Make sure WildFly 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 WildFly Standalone Server Configuration

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

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

  1. Start the WildFly server as described above.

  2. 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
    Note
    For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

This script reverts the changes made to the 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.

  1. If it is running, stop the WildFly server.

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

Remove the keystores and certificates created for this quickstart

  1. Run the CLI script to restore client cert configuration:

    $ WILDFLY_HOME/bin/jboss-cli.sh --connect --file=restore-client-cert.cli
  2. Open a terminal and navigate to the WildFly server configuration directory:

    $ cd WILDFLY_HOME/standalone/configuration/
    Note
    For Windows, use the WILDFLY_HOME\bin\standalone.bat script.
  3. Remove the client.keystore.P12, clientCert.crt, and server.truststore files that were generated for this quickstart.

Remove the Client Certificate from Your Browser

After you are done with this quickstart, remember to remove the certificate that was imported into your browser.

Remove the Client Certificate from Google Chrome

  1. Click the Chrome menu icon (3 dots) in the upper right on the browser toolbar and choose Settings. This takes you to chrome://settings/.

  2. Click on Privacy and security and then on Security.

  3. Scroll down to the Advanced section and on the Manage certificates screen, select the Your Certificates tab and then click on the arrow to the right of the certificate to be removed.

  4. The certificate is expanded, displaying the quickstartUser entry. Click on the icon (3 dots) to the right of it and then select Delete.

  5. Confirm the deletion in the dialog box. The certificate has now been removed from the Google Chrome browser.

Remove the Client Certificate from Mozilla Firefox

  1. Click the Edit menu item on the browser menu and choose Preferences.

  2. A new window will open. Click on Privacy & Security and scroll down to the Certificates section.

  3. Click the View Certificates button.

  4. A new window will open. Select the Your Certificates tab.

  5. Select the quickstartUser certificate and click the Delete button.

  6. The certificate has now been removed from the Mozilla Firefox browser.

Building and running the quickstart application with provisioned WildFly server

Instead of using a standard WildFly server distribution, you can alternatively provision a WildFly server to deploy and run the quickstart, by activating the Maven profile named provisioned-server when building the quickstart:

$ mvn clean package -Pprovisioned-server

The provisioned WildFly server, with the quickstart deployed, can then 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.

The quickstart user should be added before running the provisioned server:

$ target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'JBossAdmin'
Note

For Windows, use the WILDFLY_HOME\bin\add-user.bat script.

The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml:

        <profile>
            <id>provisioned-server</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.wildfly.plugins</groupId>
                        <artifactId>wildfly-maven-plugin</artifactId>
                        <configuration>
                            <discover-provisioning-info>
                                <version>${version.server}</version>
                            </discover-provisioning-info>
                            <!--
                                Rename the output war to ROOT.war before adding it to the server, so that the
                                application is deployed in the root web context.
                            -->
                            <name>ROOT.war</name>
                            <add-ons>...</add-ons>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>package</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    ...
                </plugins>
            </build>
        </profile>

The plugin uses WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.

If you get an error or the server is missing some functionality which cannot be auto-discovered, you can download the WildFly Glow CLI and run the following command to see more information about what add-ons are available:

wildfly-glow show-add-ons
Note

Since the plugin configuration above deploys quickstart on root web context of the provisioned server, the URL to access the application should not have the /helloworld-mutual-ssl-secured path segment after HOST:PORT.

Run the Integration Tests with a provisioned server

The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with a provisioned server.

Follow these steps to run the integration tests.

  1. Make sure the server is provisioned.

    $ mvn clean package -Pprovisioned-server
  2. Add the quickstart user:

    $ target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'JBossAdmin'
  3. Start the WildFly provisioned server, this time using the WildFly Maven Plugin, which is recommended for testing due to simpler automation. The path to the provisioned server should be specified using the jbossHome system property.

    $ mvn wildfly:start -DjbossHome=target/server 
  4. Type the following command to run the verify goal with the integration-testing profile activated, and specifying the quickstart’s URL using the server.host system property, which for a provisioned server by default is https://localhost:8443.

    $ mvn verify -Pintegration-testing -Dserver.host=https://localhost:8443 
  5. Shutdown the WildFly provisioned server, this time using the WildFly Maven Plugin too.

    $ mvn wildfly:shutdown

WildFly for OpenShift Incompatibility

This quickstart is not compatible with WildFly for OpenShift.