2

I am using WildFly 8.2 and setup the following external context

<subsystem xmlns="urn:jboss:domain:naming:2.0">
  <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://example.com:389"/>
             <property name="java.naming.security.authentication" value="simple"/>
             <property name="java.naming.security.principal" value="CN=exampleuser,DC=example,DC=com"/>
             <property name="java.naming.security.credentials" value="examplepassword"/>
           </environment>
        </external-context>
     </bindings>
   <remote-naming/>
</subsystem>

I am trying to use this external context to connect to Active Directory with this code:

@Resource(lookup = "java:global/ldap")
private LdapContext ldapCtx;

NamingEnumeration<SearchResult> enumeration = ldapCtx.search();

I get the following errors:

Caused by: java.lang.IllegalArgumentException: JBAS016081: Error injecting resource into CDI managed bean. Can't find a resource named java:global/ldap defined on private javax.naming.ldap.LdapContext com.example.LdapClient.ldapCtx

If I change my code to

@Resource(lookup = "java:global/ldap")
private InitialDirContext iniCtx;
LdapContext ldapCtx = (LdapContext) iniCtx;
NamingEnumeration<SearchResult> enumeration = ldapCtx.search();

I get

Caused by: javax.naming.NamingException: JBAS011878: Failed to lookup ldap [Root exception is java.lang.RuntimeException: java.lang.NoSuchMethodException: javax.naming.ldap.LdapContext.<init>(java.util.Hashtable)]
ci_ms
  • 65
  • 1
  • 8
  • how did you finally get this working? i'm facing the same problem and it would help me a lot your solution – Tommy Nov 17 '16 at 16:00

1 Answers1

2

Looks like you've set the class for your context to the LdapContext interface in the naming subsystem. I believe (and please correct me if I'm wrong as it's been a while since I've done any work with this aspect) this needs to be an actual implementation class, i.e., InitialDirContext, InitialLdapContext, etc. You'll then need to modify your @Resource injection accordingly.

I would have preferred to make this a comment, but not enough points. :)

whitlaaa
  • 892
  • 8
  • 15
  • You are correct and I changed the class in the external-context and that did not change the results. – ci_ms Dec 16 '14 at 20:47
  • Is there a specific reason you need to use the LdapContext interface? Do you need the LDAPv3 control functionality or are you just doing some basic lookups? Did a quick test using @Resource injection to get an InitialDirContext and looking up a user works without any issue. – whitlaaa Dec 16 '14 at 21:32