title: spring-ldap NameNotFoundException no such object
body: <p>I was using spring-ldap while I got this exception:</p>
  
  <pre><code>服务器未知异常：[LDAP: error code 32 - No Such Object]; 
  nested exception is javax.naming.NameNotFoundException: 
  [LDAP: error code 32 - No Such Object]; remaining name 'uid=lyh,ou=Users,dc=xinsight,dc=com'
  </code></pre>
  
  <p>1.my configuration</p>
  
  <pre><code>&lt;bean id="ldapTemplate"  class="org.springframework.ldap.core.LdapTemplate"&gt;
      &lt;constructor-arg ref="contextSource"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="contextSource"
        class="org.springframework.ldap.core.support.LdapContextSource"&gt;
      &lt;property name="url" value="ldap://168.2.8.77:389" /&gt;
      &lt;property name="base" value="dc=xinsight,dc=com" /&gt;
      &lt;property name="userDn" value="cn=Manager,dc=xinsight,dc=com" /&gt;
      &lt;property name="password" value="psw" /&gt;
      &lt;!--&lt;property name="referral" value="follow"/&gt;--&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>2.my code</p>
  
  <pre><code>public void create() {
      User user = new User();
      user.setFdUsername("lyhtest");
      user.setFdTenantName("root");
      user.setFdLdapPassword("ldappsw");
      user.setFdUserid(Long.valueOf(10));
  
      Name dn = buildDn(user);
      ldapTemplate.bind(dn, null, buildAttributes(user));
  }
  
  /**
   * 动态创建DN
   * spring-ldap提供了：LdapNameBuilder，LdapUtils
   */
  private Name buildDn(User user) {
      return LdapNameBuilder.newInstance(BASE_DN)
              .add("ou", "Users")
              .build();
  }
  
  /**
   * 配置属性
   * @param user
   * @return
   */
  private Attributes buildAttributes(User user) {
      Attributes attrs = new BasicAttributes();
  
      BasicAttribute objectclass = new BasicAttribute("objectclass");
      objectclass.add("top");
      objectclass.add("posixAccount");
      objectclass.add("inetOrgPerson");
      attrs.put(objectclass);
  
      attrs.put("userPassword", user.getFdLdapPassword());
      attrs.put("cn",user.getFdUsername()+"@"+user.getFdTenantName());
      attrs.put("sn",user.getFdUsername()+"@"+user.getFdTenantName());
      attrs.put("displayName",user.getFdUsername()+"@"+user.getFdTenantName());
      attrs.put("homeDirectory","/root");
      attrs.put("uidNumber",user.getFdUserid().toString());
      attrs.put("uid",user.getFdUsername());
      attrs.put("gidNumber","0");
  
      return attrs;
  }
  </code></pre>
  
  <p>3.ldap admin
  <a href="https://i.stack.imgur.com/fPxmh.png" rel="nofollow noreferrer">ldap admin</a></p>
  
  <p>4.I also writed the query method, I can get the userinfo from ldap. So the connection is ok.</p>
  
  <p>Thanks for your help!</p>