The http-custom-mechanism quickstart demonstrates how to implement a custom HTTP authentication mechanism that can be registered with Elytron.

What is it?

The http-custom-mechanism quickstart demonstrates how to implement a custom HTTP mechanism and then register it with Elytron. This makes it possible to override the configuration within the deployment to make use of this mechanism without requiring modifications to the deployment. This example makes use of custom HTTP headers to receive a clear text username and password and use them for authentication.

Warning
Generally you should avoid passing clear text passwords. It is only done here to simplify the example.

This example consists of the following Maven projects.

Project Description

webapp

The project in the webapp/ folder contains a deployable web application consisting of a secured servlet that is used to test the configuration. It generates the http-custom-mechanism-webapp.war archive that is used to test the secured application.

custom-module

This project contains the source for a custom HTTP authentication module that overrides the deployment configuration. It contains of the following Java classes and resources.

  • CustomMechanismFactory: This class implements HttpServerAuthenticationMechanismFactory and creates the new custom HttpServerAuthenticationMechanism.

  • CustomHeaderHttpAuthenticationMechanism: This class implements HttpServerAuthenticationMechanism and contains the source code for the custom mechanism processing. An HttpServerRequest object is passed to the evaluateRequest method. The mechanism processes the requests and uses one of the following callback methods on the request to indicate the outcome:

    • authenticationComplete - The mechanism successfully authenticated the request.

    • authenticationFailed - Authentication was attempted but failed.

    • authenticationInProgress - Authentication started but an additional round trip is needed.

    • badRequest - The authentication for this mechanism failed validation of the request.

    • noAuthenticationInProgress - The mechanism did not attempt any stage of authentication.

  • org.wildfly.security.examples.CustomMechanismFactory: This resource file contains a single line of text that names the custom mechanism factory class, which in this case is org.jboss.as.quickstart.http_custom_mechanism.CustomMechanismFactory.

System Requirements

The application this project produces is designed to be run on WildFly Application Server 32 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.

Building and running the quickstart application with a WildFly server distribution

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.

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!

Users

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 'Users'
Note
For Windows, use the WILDFLY_HOME\bin\add-user.bat script.

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.

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 http-custom-mechanism/target/http-custom-mechanism-webapp.war to the running instance of the server.

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

Test the Secured Servlet

Before you continue, you must test the secured servlet deployment to make sure it is working. Since this application uses a standard mechanism, it could be tested using a browser. However, after you implement the custom HTTP mechanism, the browser will not understand the request, so it is better to test the call using a client that will allow you to manipulate the headers yourself.

Issue the following command to test the deployment.

curl -v http://localhost:8080/http-custom-mechanism-webapp/secured -u quickstartUser:quickstartPwd1!

You should see the HTTP result HTTP/1.1 200 OK, along with some header information, then followed by this output.

<html>
 <head><title>Secured Servlet</title></head>
 <body>
   <h1>Secured Servlet</h1>
   <p>Current Principal 'quickstartUser'</p>
 </body>
</html>

Add the Custom Module to the Server

Now you must add the jar built by custom-module, as a custom module to the WildFly server. For your convenience, this quickstart includes the command to add the module in the add-custom-module.cli script, provided in the root directory of this quickstart.

In a 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=add-custom-module.cli
Note
For Windows, use the WILDFLY_HOME\bin\jboss-cli.bat script.

This creates the custom WILDFLY_HOME/modules/modules/org/jboss/as/quickstart/http_custom_mechanism/main folder, then copies in the custom-module/target/custom-module.jar file and creates the required module.xml file.

You can verify the module structure in your file manager.

.
└── WILDFLY_HOME
    └── modules
        └── org
            └── jboss
                └── as
                    └── quickstart
                        └── http_custom_mechanism
                            └── main
                                ├── custom-module.jar
                                └── module.xml

Configure the Server to Use the Custom Module

You configure the server to use the custom module by running 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 have done 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 to use the custom HTTP module created by this quickstart . 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-elytron.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 service-loader-http-server-mechanism-factory was added to the http element of the elytron subsystem:

    <http>
        <http-authentication-factory name="custom-mechanism" http-server-mechanism-factory="custom-factory" security-domain="ApplicationDomain">
            <mechanism-configuration>
                <mechanism mechanism-name="CUSTOM_MECHANISM"/>
              </mechanism-configuration>
        </http-authentication-factory>
        ...
        <service-loader-http-server-mechanism-factory name="custom-factory" module="org.jboss.as.quickstart.http_custom_mechanism.custom-http-mechanism"/>
    </http>
  2. The application-security-domain in the undertow subsystem was updated to use the custom-mechanism authentication factory with override-deployment-config set to true.

    <application-security-domains>
        <application-security-domain name="other" http-authentication-factory="custom-mechanism" override-deployment-config="true"/>
    </application-security-domains>

Test the Secured Servlet Using the Custom Mechanism

Now you need to test the override of the deployment with the custom HTTP mechanism, please start the server as previously described.

If you use the same curl command as when you tested the servlet before implementing the custom HTTP mechanism, it will fail with the following error.

< HTTP/1.1 401 Unauthorized
....
< X-MESSAGE: Please resubmit the request with a username specified using the X-USERNAME and a password specified using the X-PASSWORD header.

This is because the authentication mechanism rejected the call and subsequently added a header describing how to do authentication. You must modify the curl command to the following.

curl -v http://localhost:8080/http-custom-mechanism-webapp/secured -H "X-USERNAME:quickstartUser" -H "X-PASSWORD:password"

You should see the HTTP result HTTP/1.1 200 OK, along with some header information, then followed by this output.

<html>
 <head><title>Secured Servlet</title></head>
 <body>
   <h1>Secured Servlet</h1>
   <p>Current Principal 'quickstartUser'</p>
 </body>
</html>

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

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

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 webapp/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:

$ webapp/target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'Users'
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>
                            <feature-packs>
                                <feature-pack>
                                    <location>org.wildfly:wildfly-galleon-pack:${version.server}</location>
                                </feature-pack>
                            </feature-packs>
                            <layers>...</layers>
                            <!-- deploys the quickstart on root web context -->
                            <name>ROOT.war</name>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>package</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    ...
                </plugins>
            </build>
        </profile>
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 /http-custom-mechanism 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:

    $ webapp/target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'Users'
  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 -f webapp/pom.xml wildfly:start -DjbossHome=webapp/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 http://localhost:8080.

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

    $ mvn -f webapp/pom.xml wildfly:shutdown

WildFly for OpenShift Incompatibility

This quickstart is not compatible with WildFly for OpenShift.