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.
|
You will follow these basic steps to test this quickstart:
-
Add the application user.
-
Back up the server configuration before starting the server.
-
Configure the application security domain.
-
Build and deploy the application.
-
Test the secured servlet deployment.
-
Build the custom HTTP mechanism module and add it to the server.
-
Configure the server to override the deployment and use the custom module.
-
Test the secured servlet using the custom mechanism
By default, the deployed application uses the other
security domain. You need to map this security domain to an Elytron Security Domain. 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 {jbossHomeName}
with the path to your server.
$ {jbossHomeName}/bin/jboss-cli.sh --connect --file=configure-security-domain.cli
You should see the following result.
{"outcome" => "success"}
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")
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>
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.
-
Open a terminal and navigate to the
custom-module/
folder located in the root directory of this quickstart. -
Type the following command to build the custom HTTP mechanism.
$ mvn clean install
This creates the target/{artifactId}.jar
file in the quickstart custom-module/
directory.
You should see [INFO] BUILD SUCCESS
in the console output.
Now you must add the {artifactId}.jar
as a custom module to the {productName} 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 {jbossHomeName}
with the path to your server.
$ {jbossHomeName}/bin/jboss-cli.sh --connect --file=add-custom-module.cli
Note
|
For Windows, use the {jbossHomeName}\bin\jboss-cli.bat script.
|
This creates the custom {jbossHomeName}/modules/modules/org/jboss/as/quickstart/http_custom_mechanism/main
folder, then copies in the {artifactId}.jar
file and creates the required module.xml
file.
You can verify the module structure in your file manager.
.
└── {jbossHomeName}
└── modules
└── org
└── jboss
└── as
└── quickstart
└── http_custom_mechanism
└── main
├── {artifactId}.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 {productName} standalone server configuration as described above.
-
Start the {productName} 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 fto 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
{jbossHomeName}
with the path to your server.$ {jbossHomeName}/bin/jboss-cli.sh --connect --file=configure-elytron.cli
NoteFor Windows, use the {jbossHomeName}\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 {productName} server.
After stopping the server, open the {jbossHomeName}/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.
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>