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.

You will follow these basic steps to test this quickstart:

  1. Add the application user.

  2. Back up the server configuration before starting the server.

  3. Configure the application security domain.

  4. Build and deploy the application.

  5. Test the secured servlet deployment.

  6. Build the custom HTTP mechanism module and add it to the server.

  7. Configure the server to override the deployment and use the custom module.

  8. Test the secured servlet using the custom mechanism

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.

Add the Authorized Application User

This quickstart uses secured management 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.

If you prefer, you can use the add-user utility interactively. For an example of how to use the add-user utility, see the instructions located here: Add an Application User.

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.

Configure the Application Security Domain

By default, the deployed application uses the other security domain. You need to map this security domain to an Elytron HTTP authentication factory. For your convenience, this quickstart includes the command to configure the security domain in the configure-security-domain.cli script located 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=configure-security-domain.cli

You should see the following result.

{"outcome" => "success"}

Build and Deploy the Quickstart

  1. Make sure you start the WildFly server as described above.

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

  3. Type the following command to build the artifacts.

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

WFLYUT0021: Registered web context: '/http-custom-mechanism-webapp' for server 'default-server'
WFLYSRV0010: Deployed "http-custom-mechanism-webapp.war" (runtime-name : "http-custom-mechanism-webapp.war")

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>

Build the Custom HTTP Mechanism JAR

Once the secured servlet is deployed and you have tested it to make sure it is working, you need to build the module for the custom HTTP mechanism.

  1. Open a terminal and navigate to the custom-module/ folder located in the root directory of this quickstart.

  2. Type the following command to build the custom HTTP mechanism.

    $ mvn clean install

This creates the target/http-custom-mechanism.jar file in the quickstart custom-module/ directory.

You should see [INFO] BUILD SUCCESS in the console output.

Add the Custom Module to the Server

Now you must add the http-custom-mechanism.jar 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 located in the custom-module/ directory of this quickstart.

In a terminal, navigate to the custom-module/ folder located in 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 http-custom-mechanism.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
                                ├── http-custom-mechanism.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 fto 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.

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 you start the WildFly server as described above.

  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.

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