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!