Securing EJBs with Elytron

This tutorial will demonstrate how you can configure Authentication/Authorization for your EJBs on WildFly using Elytron Security Framework.

Two core authentication factories are provided as part of Elytron security framework:

  • HTTP Authentication Factory which is obviously used for Web applications performing HTTP Authentication
  • SASL Authentication Factory which is used for other network protocols, including standard protocols such as LDAP, IMAP, etc., but also JBoss Remoting which is the EJB primary transport.

elytron tutorial wildfly

We will learn now how to deploy a FileSystem Realm to be used as repository for identities that will authenticate in an EJB application.

First of all, connect to the Command Line Interface of the application server.

$ ./ -c

Now let’s define the new File System Realm through the CLI as follows:


Once created the File System Realm called “demoFSRealm” we will add one Identity to it:

/subsystem=elytron/filesystem-realm=demoFsRealm:add-identity-attribute(identity=ejbuser,name=Roles, value=["guest","manager"])

The roles needed by the application will be collected from the “Roles” attribute:


Done with the Realm, we will create a Security Domain which is bound to this Realm:


As we will be using SASL as authentication mechanism, we will need a SASL Authentication Factory for it:


Done with Elytron, the last steps needed are adding a reference to Elytron Security Domain in the “other” Security Domain contained in the “ejb3” subsystem:


Finally, as the EJB call initially lands on the HTTP Connector, we will add a reference to the SASL authentication factory we have created:


Configuring the EJB Server Side

Done with the server side, we will configure our EJBs to use the “other ” Security Domain (which in turn references Elytron’s “fsSD” Security Domain:


import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.SecurityDomain;

@RolesAllowed({ "guest" })
public class SecuredEJB implements SecuredEJBRemote {

    // Inject the Session Context
    private SessionContext ctx;

    public String getSecurityInfo() {
        Principal principal = ctx.getCallerPrincipal();
        return principal.toString();

    public boolean secured() {
        return true;

As you can see, two “guest” Role is required to access this class but for the “secured” method which requires a manager role.

Configuring the EJB Client side

From the EJB Client side, the only requirement is to provide a reference to the Authentication Configuration, by specifying the Users that are configured in the Realm:

    <authentication-client xmlns="urn:elytron:1.0">
            <rule use-configuration="default-config"/>
            <configuration name="default-config">
                <set-user-name name="ejbuser"/>
                    <clear-password password="password123"/>
                <sasl-mechanism-selector selector="DIGEST-MD5"/>
                    <use-service-loader />

The Client code is straightforward: the basic getSecurityInfo() will be available to anyone with the “guest” role, while the secured() will be available just for the “manager” role.

import java.util.Hashtable;

import javax.ejb.EJBAccessException;
import javax.naming.Context;
import javax.naming.InitialContext;

public class RemoteEJBClient {

    public static void main(String[] args) throws Exception {

        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
        jndiProperties.put(Context.PROVIDER_URL, "remote+http://localhost:8080");
        final Context context = new InitialContext(jndiProperties);

        SecuredEJBRemote reference = (SecuredEJBRemote) context.lookup("ejb:/ejb-remote-server/SecuredEJB!"
                + SecuredEJBRemote.class.getName());

        System.out.println("Successfully called secured bean, caller principal " + reference.getSecurityInfo());
        boolean hasFullPermission = false;
        try {
            hasFullPermission = reference.secured();
        } catch (EJBAccessException e) {
        System.out.println("\nPrincipal has admin permission: " + hasFullPermission);


If you configured (or if you update) the Realm to have both Roles, then you would be able to call both methods successfully:

/subsystem=elytron/filesystem-realm=demoFsRealm:add-identity-attribute(identity=ejbuser,name=Roles, value=["guest","manager"])