The http-custom-mechanism
quickstart demonstrates how to implement a custom HTTP authentication mechanism that can be registered with Elytron.
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 |
---|---|
|
The project in the |
|
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.
|
The application this project produces is designed to be run on WildFly Application Server 35 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.
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.
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.
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.
|
-
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.
-
Make sure WildFly server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type the following command to build the quickstart.
$ mvn clean package
-
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.
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>
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
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.
-
Before you begin, make sure you have done 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 to use the custom HTTP module created by this quickstart . Comments in the script describe the purpose of each block of commands. -
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.You should see the following result when you run the script.
The batch executed successfully process-state: reload-required
-
Stop the WildFly server.
After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml
file and review the changes.
-
The following
service-loader-http-server-mechanism-factory
was added to thehttp
element of theelytron
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>
-
The
application-security-domain
in theundertow
subsystem was updated to use thecustom-mechanism
authentication factory withoverride-deployment-config
set totrue
.<application-security-domains> <application-security-domain name="other" http-authentication-factory="custom-mechanism" override-deployment-config="true"/> </application-security-domains>
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>
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.
-
Make sure WildFly server is started.
-
Make sure the quickstart is deployed.
-
Type the following command to run the
verify
goal with theintegration-testing
profile activated.$ mvn verify -Pintegration-testing
When you are finished testing the quickstart, follow these steps to undeploy the archive.
-
Make sure WildFly server is started.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Type this command to undeploy the archive:
$ mvn wildfly:undeploy
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.
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
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.
Instead of using a standard WildFly server distribution, you can alternatively provision a WildFly 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.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<discover-provisioning-info>
<version>${version.server}</version>
</discover-provisioning-info>
<add-ons>...</add-ons>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>
When built, the provisioned WildFly server can 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.
Follow these steps to run the quickstart using the provisioned server.
-
Make sure the server is provisioned.
$ mvn clean package
-
Add the quickstart user:
$ webapp/target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'Users'
-
Start the WildFly provisioned server, using the WildFly Maven Plugin
start
goal.$ mvn -f webapp/pom.xml wildfly:start
-
Type the following command to run the integration tests.
$ mvn verify -Pintegration-testing
-
Shut down the WildFly provisioned server.
$ mvn -f webapp/pom.xml wildfly:shutdown