There are plenty articles available describing how to set up parts of LDAP authentication for EJB3, but I battled to find an article which pulls everthing together (If I have missed one, please provide a link). I will break the process we went through into four easy steps.
Step 1 : Login and browse your LDAP server
This is an important step since you need to verify that your LDAP connection and logon details are correct. You also need to make sure that your Distinguished Name (DN) is correct. We used an excellent LDAP Eclipse plugin by Apache to do this. After logging on (remember that your username needs a @ domain attached to it) we could browse our LDAP server and confirm that all our LDAP configuration was correct.
Make sure that your LDAP group (example : YourGroupName) has been configured properly and that all the members have been set up. This group is the security role you will configure on your ejb.
Step 2 : Setup you JBoss Security Domain (login-config.xml)
Configuring a JBoss LDAP Security Domain is really simple. The is a LDAP module available :
Please refer to this comprehensive article for all the ways to configure the module. Our configuration looks as follows:
<login-module code="org.jboss.security.auth.spi.LdapLoginModule" flag="required" >
<module-option name="rolesCtxDN">OU=Current,OU=User Accounts,OU=bar,DC=foo,DC=co,DC=za</module-option>
Step 3 : EJB3 setup
Now that we have our Security Domain configured, all we need to do now is to configure our EJB. Using the following standard Class Level annotation :
you tell your EJB which security domain to use. The next annotations tells your EJB which roles to allow (This is your LDAP group which your user should be a member of):
This annotation can be put on a Class or method level depending on the granularity required. There are plenty of other annotations to configured the granularity (example : @PermitAll) but our requirement was to lock down all our EJB's remote interface calls with a single role.
Step 4 : EJB Client
When you get your initial context as a client, make sure that you use Login Initial Context Factory :
Properties prop = new Properties();
And you are done.