Tommy Tommy - 13 days ago 7
Java Question

LDAP jndi connection (java + jboss eap 6)

i'm new to ldap and also to jndi resources and even to jboss (lol) and i'm trying to figure out how to make this working. I need to connect with an LDAP server which config is defined in Jboss standalone.xml file, and need to access it through jndi within java.

I've defined in standalone.xml and external-context to connect with LDAP:

<subsystem xmlns="urn:jboss:domain:naming:1.4">
<bindings>
<external-context name="java:global/ldap" module="org.jboss.as.naming" class="javax.naming.ldap.InitialLdapContext" cache="true">
<environment>
<property name="java.naming.factory.initial" value="com.sun.jndi.ldap.LdapCtxFactory"/>
<property name="java.naming.provider.url" value="ldap://host:port"/>
<property name="java.naming.security.authentication" value="simple"/>
<property name="java.naming.security.principal" value="user"/>
<property name="java.naming.security.credentials" value="password"/>
</environment>
</external-context>
</bindings>
<remote-naming/>
</subsystem>


Then in java i've this code:

InitialLdapContext ctx = new InitialLdapContext();
LdapContext ldapContext = null;
ldapContext = (LdapContext) ctx.lookup("java:global/ldap"));


But when the lookup executes i get the following error:

java.lang.NoSuchMethodException: javax.naming.ldap.InitialLdapContext.<init>(java.util.Hashtable)


I've tried many different examples but i can't get anything working...

Thanks in advance.

Answer

Finally I've solved it this way, using InitialDirContext as class of external context.

standalone.xml:

<subsystem xmlns="urn:jboss:domain:naming:1.4">
        <bindings>
          <external-context name="java:global/ldap" module="org.jboss.as.naming" class="javax.naming.directory.InitialDirContext" cache="true">
              <environment>
                 <property name="java.naming.factory.initial" value="com.sun.jndi.ldap.LdapCtxFactory"/>
                 <property name="java.naming.provider.url" value="ldap://host:port"/>
                 <property name="java.naming.security.authentication" value="simple"/>
                 <property name="java.naming.security.principal" value="user"/>
                 <property name="java.naming.security.credentials" value="password"/>
               </environment>
            </external-context>
         </bindings>
        <remote-naming/>
</subsystem>

In java:

InitialDirContext ldapContext = new InitialDirContext();
ldapContext = (InitialDirContext) new InitialContext().lookup("java:global/ldap");

And I get the context correctly, I hope this can help somebody else.

Comments