Using External LDAP Naming service in JBoss-5.1.0.GA

Often you’re using ldap for authentication. But what if you’d like to store more information to your ldap and access it from your enterprise application? You can add an external context to your JNDI tree.

Insert the following xml snippet into your ${jboss_home}/server/${server_config}/conf/jboss-service.xml:

<!-- Bind a remote LDAP server -->
<mbean code="org.jboss.naming.ExternalContext" 
       name="jboss.jndi:service=ExternalContext,jndiName=external/ldap/myldap">
    <attribute name="JndiName">external/ldap/myldap</attribute>
    <attribute name="Properties">
        java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
        java.naming.provider.url=ldap://localhost:389/
        java.naming.security.principal=uid=admin,ou=system
        java.naming.security.authentication=simple
        java.naming.security.credentials=secret
    </attribute>
    <attribute name="InitialContext"> javax.naming.ldap.InitialLdapContext </attribute>
    <attribute name="RemoteAccess">true</attribute>
</mbean>

You can access the naming service within your enterprise application like this:

public static LdapContext newMyLdapContext() throws NamingException {
	try {
		InitialContext iniCtx = new InitialContext();
		return (LdapContext) iniCtx.lookup("external/ldap/myldap");
	} catch (NoInitialContextException e) {
		// TODO handle the exception
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.