Author: Stefan Guilhen
Level: Intermediate
Technologies: EJB, Security
Summary: The ejb-security-jaas quickstart demonstrates how legacy JAAS security domains can be used in conjunction with Elytron
to secure JEE applications.
Target Product: JBoss EAP
Source: https://github.com/jboss-developer/jboss-eap-quickstarts/
The ejb-security-jaas quickstart demonstrates how legacy JAAS-based security domains can be used in conjunction with WildFly Elytron to secure JEE applications. The secured EJB component can be accessed indirectly using a web application and it can also be directly invoked by a remote client. This quickstart shows how Red Hat JBoss Enterprise Application Platform must be configured to support both scenarios using the legacy JAAS integration.
The overall steps required to use the JAAS integration are as follows:
- Specify a
JAASsecurity domain in the legacysecuritysubsystem. - Export an
Elytron-compatible security realm that delegates to the legacy JAAS security domain. - Create a
security-domainin theelytronsubsystem that uses the exported realm. - Setup an
http-authentication-factoryin theelytronsubsystem to handle the web requests. - Setup a
sasl-authentication-factoryin theelytronsubsystem to handle the requests made by remote clients. - Add the
application-security-domainmappings to bothejb3andundertowsubsystems to enableElytronsecurity for the EJB3 and web components.
The applications these projects produce are designed to be run on Red Hat JBoss Enterprise Application Platform 7.1 or later.
All you need to build these projects is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later. See Configure Maven for JBoss EAP 7.1 to make sure you are configured correctly for testing the quickstarts.
To run these quickstarts with the provided build scripts, you need the JBoss EAP distribution ZIP. For information on how to install and run JBoss, see the Red Hat JBoss Enterprise Application Platform Documentation Getting Started Guide located on the Customer Portal.
You can also use JBoss Developer Studio or Eclipse to run the quickstarts.
In the following instructions, replace EAP7_HOME with the actual path to your JBoss EAP installation. The installation path is described in detail here: Use of EAP7_HOME and JBOSS_HOME Variables.
-
Open a command line and navigate to the JBoss EAP server
configurationdirectory:For Linux: EAP7_HOME/standalone/configuration For Windows: EAP7_HOME\standalone\configuration -
Create a file named
users.propertiesand add the following username/password pair:quickstartUser=quickstartPwd1! -
Create a file named
roles.propertiesand add the following username/roles pair:quickstartUser=guestThis concludes the configuration required by the legacy
JAASlogin module used in this quickstart.
These steps assume you are running the server in standalone mode and using the default standalone.xml supplied with the distribution.
You configure the security domain by running JBoss CLI commands. For your convenience, this quickstart batches the commands into a configure-elytron-jaas.cli script provided in the root directory of this quickstart.
-
Before you begin, back up your server configuration file
- If it is running, stop the JBoss EAP server.
- Back up the file:
EAP7_HOME/standalone/configuration/standalone.xml - After you have completed testing this quickstart, you can replace this file to restore the server to its original configuration.
-
Start the JBoss EAP server by typing the following:
For Linux: EAP7_HOME/bin/standalone.sh For Windows: EAP7_HOME\bin\standalone.bat -
Review the
configure-elytron-jaas.clifile in the root of this quickstart directory. This script adds the configuration that enables Elytron security for the quickstart components. Comments in the script describe the purpose of each block of commands. -
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing EAP7_HOME with the path to your server:
For Linux: EAP7_HOME/bin/jboss-cli.sh --connect --file=configure-elytron-jaas.cli For Windows: EAP7_HOME\bin\jboss-cli.bat --connect --file=configure-elytron-jaas.cliYou should see the following result when you run the script:
The batch executed successfully process-state: reload-required -
Stop the JBoss EAP server.
After stopping the server, open the EAP7_HOME/standalone/configuration/standalone.xml file and review the changes.
-
The following
security-domainwas added to the legacysecuritysubsystem:<security-domain name="quickstart-domain" cache-type="default"> <authentication> <login-module code="Remoting" flag="optional"> <module-option name="password-stacking" value="useFirstPass"/> </login-module> <login-module code="UsersRoles" flag="required"> <module-option name="usersProperties" value="${jboss.server.config.dir}/users.properties"/> <module-option name="rolesProperties" value="${jboss.server.config.dir}/roles.properties"/> <module-option name="password-stacking" value="useFirstPass"/> </login-module> </authentication> <mapping> <mapping-module code="SimpleRoles" type="role"> <module-option name="quickstartUser" value="admin"/> </mapping-module> </mapping> </security-domain>The
quickstart-domainis used to authenticate and authorize users. TheRemotinglogin module is added to properly authenticate requests made from remote clients. Amapping-moduleis added that can be used to provide an extra role (admin). It is used later on to show how the legacy role mappers can be enabled and disabled. -
The following
elytron-realmwas added to the legacysecuritysubsystem:<elytron-integration> <security-realms> <elytron-realm name="LegacyRealm" legacy-jaas-config="quickstart-domain" apply-role-mappers="false"/> </security-realms> </elytron-integration>This block tells the
securitysubsystem to export anElytron-compatible realm calledLegacyRealmthat will delegate authentication and authorization decisions to the legacyquickstart-domain. Setting theapply-role-mappersattribute tofalseindicates to the exported realm that it should not use any role mappers defined in the legacy security domain. -
The following
security-domainwas added to theelytronsubsystem:<security-domain name="LegacyDomain" default-realm="LegacyRealm" permission-mapper="default-permission-mapper" security-event-listener="local-audit"> <realm name="LegacyRealm"/> </security-domain> -
The following
http-authentication-factorywas added to theelytronsubsystem:<http-authentication-factory name="quickstart-http-authentication" http-server-mechanism-factory="global" security-domain="LegacyDomain"> <mechanism-configuration> <mechanism mechanism-name="BASIC"> <mechanism-realm realm-name="Legacy Realm"/> </mechanism> </mechanism-configuration> </http-authentication-factory>It creates the HTTP authentication factory that will handle BASIC requests by delegating the security domain created in step 3.
-
The following
application-security-domainmapping was added to theundertowsubsystem:<application-security-domains> <application-security-domain name="legacy-domain" http-authentication-factory="quickstart-http-authentication"/> </application-security-domains>It tells
Undertowto use the HTTP authentication factory created in step 4 for web applications that specify the security domainlegacy-domainin their metadata. The quickstart application specifies this domain both for the web layer, in thejboss-web.xmlfile, and the EJB component, using annotation in the code. -
The following
sasl-authentication-factorywas added to theelytronsubsystem:<sasl-authentication-factory name="quickstart-sasl-authentication" sasl-server-factory="configured" security-domain="LegacyDomain"> <mechanism-configuration> <mechanism mechanism-name="PLAIN"/> </mechanism-configuration> </sasl-authentication-factory> -
The
http-remoting-connectorin theremotingsubsystem was updated to use thesasl-authentication-factorycreated in step 6:<http-connector name="http-remoting-connector" connector-ref="default" security-realm="ApplicationRealm" sasl-authentication-factory="quickstart-sasl-authentication"/>Authentication performed by the quickstart remote client is handled by this SASL authentication factory.
-
Finally, the following
application-security-domainmapping was added to theejb3subsystem:<application-security-domains> <application-security-domain name="legacy-domain" security-domain="LegacyDomain"/> </application-security-domains>This mapping basically enables
Elytronsecurity for EJB3 applications that specify the security domainlegacy-domainin their metadata (either via jboss-ejb3.xml or annotations). The quickstart application uses the@SecurityDomainannotation in the bean class to specify this security domain.
-
Open a command prompt and navigate to the root of the JBoss EAP directory.
-
The following shows the command line to start the server:
For Linux: EAP7_HOME/bin/standalone.sh For Windows: EAP7_HOME\bin\standalone.bat
-
Make sure you have started the JBoss EAP server as described above.
-
Open a command prompt and navigate to the root directory of this quickstart.
-
Type this command to build and deploy the archive:
mvn clean install wildfly:deploy -
This will deploy
target/ejb-security-jaas.warto the running instance of the server.
The application will be running at the following URL http://localhost:8080/ejb-security-jaas/.
When you access the application, you are presented with a browser login challenge.
-
If you attempt to login with a user name and password combination that has not been added to the server, the login challenge will be redisplayed.
-
When you login successfully using
quickstartUser/quickstartPwd1!, the browser displays the following security info:Successfully called Secured EJB Principal : quickstartUser Remote User : quickstartUser Has admin permission : false Authentication Type : BASIC -
The application can also be accessed directly by a remote client. Type the following command in the root directory of the quickstart:
mvn exec:execThe remote client application runs and displays the results of calling the secured bean:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Called secured bean, caller principal quickstartUser Principal has admin permission: false * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -
Next, change the exported realm so that it now uses the legacy role mappers as defined in the legacy
JAASsecurity domain.Make sure you are still in the root directory of this quickstart, and run the following command, replacing EAP7_HOME with the path to your server:
For Linux: EAP7_HOME/bin/jboss-cli.sh --connect --file=enable-role-mappers.cli For Windows: EAP7_HOME\bin\jboss-cli.bat --connect --file=enable-role-mappers.cliYou should see the following result when you run the script:
{ "outcome" => "success", "response-headers" => { "operation-requires-reload" => true, "process-state" => "reload-required" } } -
If you didn't close your web browser, re-load the quickstart application page. Otherwise open a new browser, point it to the URL http://localhost:8080/ejb-security-jaas/ and login with
quickstartUser/quickstartPwd1!. It should now display a page confirming the user now has theadminrole that was provided by the legacy role mapper:Successfully called Secured EJB Principal : quickstartUser Remote User : quickstartUser Has admin permission : true Authentication Type : BASIC -
The same result can be observed when re-running the remote client application:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Called secured bean, caller principal quickstartUser Principal has admin permission: true * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
Make sure you have started the JBoss EAP server as described above.
-
Open a command prompt and navigate to the root directory of this quickstart.
-
When you are finished testing, type this command to undeploy the archive:
mvn wildfly:undeploy
You can restore the original server configuration by running the restore-configuration.cli script provided in the root directory of this quickstart or by manually restoring the backup copy the configuration file.
-
Start the JBoss EAP server by typing the following:
For Linux: EAP7_HOME/bin/standalone.sh For Windows: EAP7_HOME\bin\standalone.bat -
Open a new command prompt, navigate to the root directory of this quickstart, and run the following command, replacing EAP7_HOME with the path to your server:
For Linux: EAP7_HOME/bin/jboss-cli.sh --connect --file=restore-configuration.cli For Windows: EAP7_HOME\bin\jboss-cli.bat --connect --file=restore-configuration.cliThis script reverts the changes made to the
ejb3,elytron,securityandundertowsubsystems. You should see the following result when you run the script:The batch executed successfully process-state: reload-required
- If it is running, stop the JBoss EAP server.
- Replace the
EAP7_HOME/standalone/configuration/standalone.xmlfile with the backup copy of the file.
After you are done with this quickstart, remember to remove the users.properties and roles.properties files from the
server configuration directory (EAP7_HOME/standalone/configuration/).
You can also start the server and deploy the quickstarts or run the Arquillian tests from Eclipse using JBoss tools. For general information about how to import a quickstart, add a JBoss EAP server, and build and deploy a quickstart, see Use JBoss Developer Studio or Eclipse to Run the Quickstarts.
- Be sure to Create the Properties Files for the JAAS Security Domain as described above.
- Be sure to configure the server by running the JBoss CLI script as described above under Configure the Server. Stop the server at the end of that step.
- To deploy the application to the JBoss EAP server, right-click on the
ejb-security-jaasproject and chooseRun As-->Run on Server. - You are presented with a browser login challenge. Enter the credentials as described above under Access the Application to see the running application. Note that
Has admin permissionisfalse. - Leave the application running in JBoss Developer Studio. To configure the server to use the legacy role mappers, open a terminal, and run the
enable-role-mappers.cliscript as described above under Access the Application. - Go back to JBoss Developer Studio and click
Refresh the current page. Note thatHas admin permissionis nowtrue. - To undeploy the project, right-click on the
ejb-security-jaasproject and chooseRun As-->Maven build. Enterwildfly:undeployfor theGoalsand clickRun. - Be sure to Restore the Server Configuration when you have completed testing this quickstart.
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