The ejb-security-programmatic-auth quickstart demonstrates how to programmatically setup different identities when invoking a remote secured EJB.

What is it?

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.

System Requirements

The application this project produces is designed to be run on WildFly Application Server 33 or later.

All you need to build this project is Java 11.0 (Java SDK 11) 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.

Use of the WILDFLY_HOME and QUICKSTART_HOME Variables

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.

Add the Authorized Application and Management Users

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.

Back Up the WildFly Standalone Server Configuration

Before you begin, back up your server configuration file.

  1. If it is running, stop the WildFly server.

  2. 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.

Start the WildFly Standalone Server

  1. Open a terminal and navigate to the root of the WildFly directory.

  2. Start the WildFly server with the default profile by typing the following command.

    $ WILDFLY_HOME/bin/standalone.sh 
    Note
    For Windows, use the WILDFLY_HOME\bin\standalone.bat script.

Configure the Server

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.

  1. Before you begin, make sure you do the following:

  2. 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.

  3. 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
    Note
    For 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
  4. Stop the WildFly server.

Review the Modified Server Configuration

After stopping the server, open the WILDFLY_HOME/standalone/configuration/standalone.xml file and review the changes.

  1. The following application-security-domain mapping was added to the ejb3 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 the quickstart-domain that was set in the EJBs via annotation to the Elytron ApplicationDomain that will be responsible for authenticating and authorizing access to the EJBs.

  2. Take a look at the existing http-connector configuration in the remoting subsystem. Notice that it uses the application-sasl-authentication authentication factory, which references the ApplicationDomain 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.

Build and Deploy the Quickstart

  1. Make sure WildFly server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type the following command to build the quickstart.

    $ mvn clean install
  4. 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.

Access the Application

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

Investigate the Console Output

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.

Run the Integration Tests

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.

  1. Make sure WildFly server is started.

  2. Make sure the quickstart is deployed.

  3. Type the following command to run the verify goal with the integration-testing profile activated.

    $ mvn verify -Pintegration-testing 

Undeploy the Quickstart

When you are finished testing the quickstart, follow these steps to undeploy the archive.

  1. Make sure WildFly server is started.

  2. Open a terminal and navigate to the root directory of this quickstart.

  3. Type this command to undeploy the archive:

    $ mvn wildfly:undeploy

Restore the WildFly Standalone Server Configuration

You can restore the original server configuration using either of the following methods.

Restore the WildFly Standalone Server Configuration by Running the JBoss CLI Script

  1. Start the WildFly server as described above.

  2. 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
    Note
    For 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

Restore the WildFly Standalone Server Configuration Manually

When you have completed testing the quickstart, you can restore the original server configuration by manually restoring the backup copy the configuration file.

  1. If it is running, stop the WildFly server.

  2. Replace the WILDFLY_HOME/standalone/configuration/standalone.xml file with the backup copy of the file.

Run the Quickstart in Red Hat CodeReady Studio or Eclipse

You can also start the server and deploy the quickstarts or run the Arquillian tests in Red Hat CodeReady Studio or from Eclipse using JBoss tools. For general information about how to import a quickstart, add a WildFly server, and build and deploy a quickstart, see Use Red Hat CodeReady Studio or Eclipse to Run the Quickstarts.

  • 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 AsMaven build. Enter clean package wildfly:deploy for the Goals and click Run. This deploys the ejb-security-programmatic-auth JAR to the WildFly server.

  • Right-click on the ejb-security-programmatic-auth project and choose Run AsRun 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 AsMaven 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.

Debug the Application

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

Building and running the quickstart application with provisioned WildFly server

Instead of using a standard WildFly server distribution, you can alternatively provision a WildFly server to deploy and run the quickstart, by activating the Maven profile named provisioned-server when building the quickstart:

$ mvn clean install -Pprovisioned-server

The provisioned WildFly server, with the quickstart deployed, can then 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.

The quickstart user should be added before running the provisioned server:

$ target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'
Note

For Windows, use the WILDFLY_HOME\bin\add-user.bat script.

The quickstart admin should be added before running the provisioned server:

$ target/server/bin/add-user.sh -a -u 'quickstartAdmin' -p 'adminPwd1!' -g 'guest,admin'
Note

For Windows, use the WILDFLY_HOME\bin\add-user.bat script.

The server provisioning functionality is provided by the WildFly Maven Plugin, and you may find its configuration in the quickstart pom.xml:

        <profile>
            <id>provisioned-server</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.wildfly.plugins</groupId>
                        <artifactId>wildfly-maven-plugin</artifactId>
                        <configuration>
                            <discover-provisioning-info>
                                <version>${version.server}</version>
                            </discover-provisioning-info>
                            <!--
                                Rename the output war to ROOT.war before adding it to the server, so that the
                                application is deployed in the root web context.
                            -->
                            <name>ROOT.war</name>
                            <add-ons>...</add-ons>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>package</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    ...
                </plugins>
            </build>
        </profile>

The plugin uses WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.

If you get an error or the server is missing some functionality which cannot be auto-discovered, you can download the WildFly Glow CLI and run the following command to see more information about what add-ons are available:

wildfly-glow show-add-ons
Note

Since the plugin configuration above deploys quickstart on root web context of the provisioned server, the URL to access the application should not have the /ejb-security-programmatic-auth path segment after HOST:PORT.

Run the Integration Tests with a provisioned server

The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with a provisioned server.

Follow these steps to run the integration tests.

  1. Make sure the server is provisioned.

    $ mvn clean install -Pprovisioned-server
  2. Add the quickstart user:

    $ target/server/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'
  3. 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.

  1. Start the WildFly provisioned server, this time using the WildFly Maven Plugin, which is recommended for testing due to simpler automation. The path to the provisioned server should be specified using the jbossHome system property.

    $ mvn wildfly:start -DjbossHome=target/server 
  2. Type the following command to run the verify goal with the integration-testing profile activated, and specifying the quickstart’s URL using the server.host system property, which for a provisioned server by default is http://localhost:8080.

    $ mvn verify -Pintegration-testing -Dserver.host=http://localhost:8080 
  3. Shutdown the WildFly provisioned server, this time using the WildFly Maven Plugin too.

    $ mvn wildfly:shutdown