title: Spring-security-ldap: Failed instantiate InitialContextFactory com.sun.jndi.ldap.LdapCtxFactory
body: <p>I am migrating from Jboss 7.1 to Wildfly and getting exception on fail auth.</p>
  
  <p>To login in the system I user spring-security-ldap and cas-client, and when I type wrong password on the login form I get exception:</p>
  
  <pre><code>    Caused by: javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory com.sun.jndi.ldap.LdapCtxFactory from classloa
  der ModuleClassLoader for Module "deployment.gas.war:main" from Service Module Loader [Root exception is javax.naming.AuthenticationExceptio
  n: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1 ]]
          at org.jboss.as.naming.InitialContext.getDefaultInitCtx(InitialContext.java:116)
          at org.jboss.as.naming.InitialContext.init(InitialContext.java:99)
          at javax.naming.ldap.InitialLdapContext.&lt;init&gt;(InitialLdapContext.java:153) [rt.jar:1.7.0_60]
          at org.jboss.as.naming.InitialContext.&lt;init&gt;(InitialContext.java:90)
          at org.jboss.as.naming.InitialContextFactory.getInitialContext(InitialContextFactory.java:44)
          at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684) [rt.jar:1.7.0_60]
          at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) [rt.jar:1.7.0_60]
          at javax.naming.InitialContext.init(InitialContext.java:242) [rt.jar:1.7.0_60]
          at javax.naming.ldap.InitialLdapContext.&lt;init&gt;(InitialLdapContext.java:153) [rt.jar:1.7.0_60]
          at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(LdapContextSource.java:43) [spring-ldap-core-1.3.1.
  RELEASE.jar:1.3.1.RELEASE]
          at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:254) [spring-ldap-core-1.3.1
  .RELEASE.jar:1.3.1.RELEASE]
          ... 48 more
  </code></pre>
  
  <p>My module for spring-security:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;module xmlns="urn:jboss:module:1.1" name="org.spring.security" slot="3.1.3"&gt;
      &lt;resources&gt;
          &lt;resource-root path="spring-security-ldap-3.1.3.RELEASE.jar"/&gt;
          &lt;resource-root path="spring-security-config-3.1.3.RELEASE.jar"/&gt;
          &lt;resource-root path="spring-security-core-3.1.3.RELEASE.jar"/&gt;
          &lt;resource-root path="spring-security-web-3.1.3.RELEASE.jar"/&gt;
          &lt;resource-root path="spring-security-cas-3.1.3.RELEASE.jar"/&gt;
          &lt;resource-root path="spring-ldap-core-1.3.1.RELEASE.jar"/&gt;
      &lt;/resources&gt;
  
      &lt;dependencies&gt;
          &lt;module name="org.spring.core" slot="3.2.1" export="true" /&gt;
          &lt;module name="org.jasig.cas.client.cas-client-core" slot="3.1.12" export="true"/&gt;
          &lt;module name="org.apache.commons-lang" slot="2.5"/&gt;
          &lt;module name="sun.jdk" export="true"/&gt;
      &lt;/dependencies&gt;
  &lt;/module&gt;
  </code></pre>
  
  <p>Everything was fine at Jboss AS 7.1, may be I forgot to change something in properties?</p>
  
  <p>--- EDIT</p>
  
  <p>All because of org.springframework.security.ldap.authentication.BindAuthentication class:</p>
  
  <p>There is a function bindWithDn(...), that tries to create context with userDnStr and throws exception if can't. I have a list of userDnStr and the first userDnStr (if user doesn't exists at this ldap folder) function throws exception. But on Wildfly it throws org.springframework.ldap.UncategorizedLdapException instead of AuthenticationException or OperationNotSupportedException as on Jboss 7 / Tomcat</p>
  
  <p>Spring sources:</p>
  
  <pre><code> // This will be thrown if an invalid user name is used and the method may
              // be called multiple times to try different names, so we trap the exception
              // unless a subclass wishes to implement more specialized behaviour.
              if ((e instanceof org.springframework.ldap.AuthenticationException)
                      || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
                  handleBindException(userDnStr, username, e);
              } else {
                  throw e;
              }
  </code></pre>
  
  <p>In case of AuthenticationException or OperationNotSupportedException spring can handle it and moves on and tries the next userDnStr.  Still question is: Why getContext() throws UncategorizedLdapException on Wildfly?</p>