ejb-security-programmatic-auth: Using the programmatic API to invoke a remote EJB using different identities
Author: Stefan Guilhen
Level: Intermediate
Technologies: EJB, Security
Summary: The ejb-security-programmatic-auth
quickstart demonstrates how to programmatically setup different identities when invoking a remote secured EJB.
Target Product: JBoss EAP
Source: https://github.com/jboss-developer/jboss-eap-quickstarts/
The ejb-security-programmatic-auth
quickstart demonstrates how to invoke a remote secured EJB using the Elytron
client API to establish different identities. The quickstart client application accomplishes that by looking up and invoking the secured EJB under different AuthenticationContext
s. Each context is setup to use a different identities and credentials.
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.
Using the add-user utility script, you must add the following users to the ApplicationRealm
:
UserName | Realm | Password | Roles |
---|---|---|---|
quickstartUser | ApplicationRealm | quickstartPwd1! | guest |
superUser | ApplicationRealm | superPwd1! | guest,admin |
The first application user has guest
access rights to the application but no admin
rights. The second user has both rights.
To add the application users, open a command prompt and type the following commands:
For Linux:
EAP7_HOME/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'
EAP7_HOME/bin/add-user.sh -a -u 'superUser' -p 'superPwd1!' -g 'guest,admin'
For Windows:
EAP7_HOME\bin\add-user.bat -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'
EAP7_HOME\bin\add-user.bat -a -u 'superUser' -p 'superPwd1!' -g 'guest,admin'
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.
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.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.cli
file 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.cli For Windows: EAP7_HOME\bin\jboss-cli.bat --connect --file=configure-elytron.cli
You 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
application-security-domain
mapping was added to theejb3
subsystem:<application-security-domains> <application-security-domain name="quickstart-domain" security-domain="ApplicationDomain"/> </application-security-domains>
The
application-security-domain
essentially enables Elytron security for the quickstart EJBs. It maps thequickstart-domain
that was set in the EJBs via annotation to the ElytronApplicationDomain
that will be responsible for authenticating and authorizing access to the EJBs. -
The
http-connector
in theremoting
subsystem was updated to use theapplication-sasl-authentication
authentication factory:<http-connector name="http-remoting-connector" connector-ref="default" security-realm="ApplicationRealm" sasl-authentication-factory="application-sasl-authentication"/>
This allows for the identity that was established in the connection authentication to be propagated to the components.
-
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 package wildfly:deploy
-
This will deploy
target/ejb-security-programmatic-auth.jar
to the running instance of the server.
Before you run the client, make sure you have already successfully deployed the EJBs to the server in the previous step and that your command prompt is still in the same folder.
Type this command to execute the client:
mvn exec:exec
When you run the mvn exec:exec
command, you see the following output. Note there may be other log messages interspersed between these.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Called secured bean, caller principal quickstartUser
Principal has admin permission: false
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Called secured bean, caller principal superUser
Principal has admin permission: true
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
As expected, the quickstart
user is able to call the methods available for guest
s but does not have the admin
permission to call administrative methods on the remote EJB. The superUser
on the other hand has permissions to call both methods.
-
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 back-up 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.cli
This script reverts the changes made to the
ejb3
andremoting
subsystems. 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.xml
file with the back-up copy of the file.
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 Add the Application Users as described above.
-
Be sure to configure the server by running the JBoss CLI script as described above under Configure the Server.
-
Right-click on the
ejb-security-programmatic-auth
project and chooseRun As
-->Maven build
. Enterclean package wildfly:deploy
for theGoals:
and clickRun
. This deploys theejb-security-programmatic-auth
JAR to the JBoss EAP server. -
Right-click on the
ejb-security-programmatic-auth
project and chooseRun As
-->Run Configurations...
. -
Enter
exec:exec
for theGoals
and clickRun
. -
Review the output in the console window. You should see the following output.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Called secured bean, caller principal quickstartUser Principal has admin permission: false * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Called secured bean, caller principal superUser Principal has admin permission: true * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
To undeploy the project, right-click on the
ejb-security-programmatic-auth
project and chooseRun As
-->Maven build
. Enterwildfly:undeploy
for theGoals
and 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