Authenticate JBoss application using JAAS and LDAP

Development
Authenticate JBoss application using JAAS and LDAP

It is easy to connect a JBoss to an LDAP server and creating Java EE applications that use the LDAP information for authorization and authentication.

Following these simple steps, you will be able to configure your JBoss and a web application (configuring an ejb deployment is similar; Just read the Java EE API to map the attributes from web.xml to ejb annotations).

For this example, we already have an Apache Directory Server running locally, with the sample LDIF with the sevenSeas company imported. You can find the file and tutorial in the Apache DS documentation.


There are two main steps to be able to use LDAP as an authentication mechanism:

  1. Configure JBoss to connect to LDAP server
  2. Configure the application to use the application policy

Configure JBoss to connect to LDAP server

The JBoss connects to the LDAP server using an application-policy, which is configured in %server_path%/conf/login-config.xml

Just add the following entry in the login-config.xml. You can see the description of the important attributes:

com.sun.jndi.ldap.LdapCtxFactory ldap://localhost:10389 simple uid=admin,ou=system secret ou=people,o=sevenSeas (uid={0}) ou=groups,o=sevenSeas (uniquemember={1}) cn SUBTREE_SCOPE 0 true

As you can see, the bindCredential is not encrypted. In order to do so, you can check out our post about securing the LdapExtLoginModule.

Configure the web application to use the application policy

First, we need to connect the java web application to the application policy defined in JBoss. In order to do this, you need to create the file jboss-web.xml in the WEB-INF folder, the same folder where the web.xml resides.

Here is the content of the file (this works for JBoss 5 in a windows machine, you may need to change the header of the file):

java:/jaas/ApacheDS

This will tell the container to use the ApacheDS application-policy we defined previously in JBoss.

After this, we only need to restrict the specific urls or servlets to certain roles. In this example, we will only allow access for users in the group “HMS Bounty”, otherwise a 403 (forbidden) will be issued.

In order to do this, we need to edit the web.xml file, adding the following configuration:

AServlet com.app.AServlet AServlet /AServlet All resources All resources /* HMS Bounty BASIC

Following this example, you can restrict access to different resources to other roles.

Configure an EJB based WS to use the application policy

If, in turn, you want to secure an EJB based WS, just adding these annotations at the start of the implementing class will do:

@org.jboss.wsf.spi.annotation.WebContext(contextRoot=”MyCtxRoot” , authMethod = “BASIC”, secureWSDLAccess = false) @org.jboss.ejb3.annotation.SecurityDomain( “java:/jaas/ApacheDS” ) @RolesAllowed(“HMS Bounty”) public class MyWSImplementation implements MyWSInterface{

We are a a Clutch Champion for 2023!
Development
We are a a Clutch Champion for 2023!
Kreitech has been recognized as a 2023 Clutch Champion by Clutch, the leading global marketplace of B2B service provider
Read More
Navigating a Post-2020 World: A Time of Business Challenges and Opportunities
Development
Navigating a Post-2020 World: A Time of Business Challenges and Opportunities
2020 will be remembered as an unprecedented, challenging, and unique year, a significant opportunity for the global
Read More
How Hiring an External Team can Accelerate Development and save costs?
Development
How Hiring an External Team can Accelerate Development and save costs?
In today's fast-paced software industry, companies often face the challenge of meeting deadlines and achieving goals
Read More
Have a project or an idea?
Let's make it real!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.