liyuhui
liyuhui

Reputation: 1260

spring-ldap NameNotFoundException no such object

I was using spring-ldap while I got this exception:

服务器未知异常:[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'

1.my configuration

<bean id="ldapTemplate"  class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="contextSource"/>
</bean>

<bean id="contextSource"
      class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="ldap://168.2.8.77:389" />
    <property name="base" value="dc=xinsight,dc=com" />
    <property name="userDn" value="cn=Manager,dc=xinsight,dc=com" />
    <property name="password" value="psw" />
    <!--<property name="referral" value="follow"/>-->
</bean>

2.my 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;
}

3.ldap admin ldap admin

4.I also writed the query method, I can get the userinfo from ldap. So the connection is ok.

Thanks for your help!

Upvotes: 3

Views: 4868

Answers (1)

liyuhui
liyuhui

Reputation: 1260

I have solved this problem.

1.I checked the log file of ldap admin, and I saw the "dn":

ou=Users,dc=xinsight,dc=com,dc=xinsight,dc=com

2.before and after

return LdapNameBuilder.newInstance(Base_DN)
            .add("ou", "Users")
            .add("uid","lyh3")
            .build();

return LdapNameBuilder.newInstance()
            .add("ou", "Users")
            .add("uid","lyh3")
            .build();

And after I removed the param: Base_DN, I created a user successfully. Then I check the log file of ldap admin, it shows that:

dn: ou=Users,dc=xinsight,dc=com

Although I don't know why the value of dn has duplicate 'dc=xinsight,dc=com'. I copied from the official document spring-ldap official doc, and it shows that a 'base_dn' is needed when building 'dn':

protected Name buildDn(Person p) {
  return LdapNameBuilder.newInstance(BASE_DN)
    .add("c", p.getCountry())
    .add("ou", p.getCompany())
    .add("cn", p.getFullname())
    .build();
}

Upvotes: 6

Related Questions