ejb-security-programmatic-auth: Using the programmatic API to invoke a remote EJB using different identities
The ejb-security-programmatic-auth
quickstart demonstrates how to programmatically setup different identities when invoking a remote secured EJB.
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 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.
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! |
guest |
quickstartAdmin |
ManagementRealm |
adminPwd1! |
guest,admin |
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 'guest'
$ WILDFLY_HOME/bin/add-user.sh -a -u 'quickstartAdmin' -p 'adminPwd1!' -g 'guest,admin'
Note
|
For Windows, use the WILDFLY_HOME\bin\add-user.bat script.
|
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.
-
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.
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, make sure you do 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 security for the quickstart components. 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
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 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. -
Take a look at the existing
http-connector
configuration in theremoting
subsystem. Notice that it uses theapplication-sasl-authentication
authentication factory, which references theApplicationDomain
security domain mentioned above:<http-connector name="http-remoting-connector" connector-ref="default" sasl-authentication-factory="application-sasl-authentication"/>
This allows for the identity that was established in the connection authentication to be propagated to the components.
-
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 install
-
Type the following command to deploy the quickstart.
$ mvn wildfly:deploy
This deploys the ejb-security-programmatic-auth/target/ejb-security-programmatic-auth.jar
to the running instance of the server.
You should see a message in the server log indicating that the archive deployed successfully.
Before you run the client, make sure you have already successfully deployed the EJBs to the server in the previous step and that your terminal is still in the same folder.
Type the following 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 quickstartAdmin
Principal has admin permission: true
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
As expected, the quickstart
user is able to call the methods available for guest
, but does not have the admin
permission to call administrative methods on the remote EJB. The quickstartAdmin
on the other hand has permissions to call both methods.
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
You can restore the original server configuration using either of the following methods.
-
You can run the
restore-configuration.cli
script provided in the root directory of this quickstart. -
You can manually restore the configuration using the backup copy of the configuration file.
-
Start the WildFly server as described above.
-
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=restore-configuration.cli
NoteFor Windows, use the WILDFLY_HOME\bin\jboss-cli.bat
script.
This script reverts the changes made to the ejb3
and remoting
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.
-
Make sure you add the authorized application and management users as described above.
-
Make sure you 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 choose Run As → Maven build. Enter
clean package wildfly:deploy
for the Goals and click Run. This deploys theejb-security-programmatic-auth
JAR to the WildFly server. -
Right-click on the ejb-security-programmatic-auth project and choose Run As → Run Configurations….
-
Enter
exec:exec
for the Goals and click Run. -
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 quickstartAdmin Principal has admin permission: true * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
To undeploy the project, right-click on the ejb-security-programmatic-auth project and choose Run As → Maven build. Enter
wildfly:undeploy
for the Goals and click Run. -
Make sure you restore the WildFly standalone server configuration when you have completed testing this quickstart.
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 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 install
-
Add the quickstart user:
$ target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'
-
Add the quickstart admin:
$ target/server/bin/add-user.sh -a -u 'quickstartAdmin' -p 'adminPwd1!' -g 'guest,admin'
For Windows, use the WILDFLY_HOME\bin\add-user.bat
script.
-
Start the WildFly provisioned server, using the WildFly Maven Plugin
start
goal.$ mvn wildfly:start
-
Type the following command to run the integration tests.
$ mvn verify -Pintegration-testing
-
Shut down the WildFly provisioned server.
$ mvn wildfly:shutdown