title: LdapCtx cannot be cast to DirContextAdapter
body: <p>I am running with Spring Security 3.2.9 RELEASE, trying to access AD with SpringSecurityLdapTemplate, while encountered this problem.</p>
  
  <p>Dependencies are 
  spring-security-ldap-3.2.9.RELEASE.jar
  spring-ldap-core-1.3.2.RELEASE.jar</p>
  
  <p>Problem details as below:
  java.lang.ClassCastException: com.sun.jndi.ldap.LdapCtx cannot be cast to org.springframework.ldap.core.DirContextAdapter
      at org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntryInternal(SpringSecurityLdapTemplate.java:219)</p>
  
  <p>from the code</p>
  
  <pre><code>try {
          while (resultsEnum.hasMore()) {
              SearchResult searchResult = resultsEnum.next();
              DirContextAdapter dca = (DirContextAdapter) searchResult.getObject();
              Assert.notNull(dca, "No object returned by search, DirContext is not correctly configured");
  
              if (logger.isDebugEnabled()) {
                  logger.debug("Found DN: " + dca.getDn());
              }
              results.add(dca);
          }
      } catch (PartialResultException e) {
          LdapUtils.closeEnumeration(resultsEnum);
          logger.info("Ignoring PartialResultException");
      }
  </code></pre>
  
  <p>Seems it is trying to cast the search result to DirContextAdapter, don't know why it cannot cast.</p>
  
  <p>All my code is running fine with spring-security-3.1.0 Release, because 3.1.0 using different logic</p>
  
  <pre><code> try {
          while (resultsEnum.hasMore()) {
              SearchResult searchResult = resultsEnum.next();
              // Work out the DN of the matched entry
              DistinguishedName dn = new DistinguishedName(new CompositeName(searchResult.getName()));
  
              if (base.length() &gt; 0) {
                  dn.prepend(searchBaseDn);
              }
  
              if (logger.isDebugEnabled()) {
                  logger.debug("Found DN: " + dn);
              }
              results.add(new DirContextAdapter(searchResult.getAttributes(), dn, ctxBaseDn));
          }
      } catch (PartialResultException e) {
          LdapUtils.closeEnumeration(resultsEnum);
          logger.info("Ignoring PartialResultException");
      }
  </code></pre>
  
  <p>How to resolve this problem?</p>