我在LDAP中添加条目时遇到问题,我想添加一个用户。
class AddUser {
public static void main(String[] args) {
String userName = "manager";
String password = "pass";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.10.45:389/dc=mydc,dc=local");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, new String("mydc" + "\\" + userName));
env.put(Context.SECURITY_CREDENTIALS, password);
// env.put(Context.REFERRAL, "follow");
// entry's DN
String entryDN = "cn=NewUser, dc=mydc, dc=local";
// entry's attributes
Attribute cn = new BasicAttribute("cn", "NewUser");
Attribute sn = new BasicAttribute("sn", "Smith");
Attribute mail = new BasicAttribute("mail", "newuser@foo.com");
Attribute phone = new BasicAttribute("telephoneNumber", "+1 222 3334444");
Attribute uid = new BasicAttribute("uid", "nsmith");
Attribute userPassword = new BasicAttribute("userPassword", "pwd1");
Attribute oc = new BasicAttribute("objectClass");
oc.add("dcObject");
oc.add("person");
oc.add("inetOrgPerson");
DirContext ctx = null;
try {
// get a handle to an Initial DirContext
ctx = new InitialDirContext(env);
// build the entry
Attributes entry = new BasicAttributes();
entry.put(cn);
entry.put(sn);
entry.put(mail);
entry.put(phone);
entry.put(uid);
entry.put(userPassword);
entry.put(oc);
// Add the entry
ctx.createSubcontext(entryDN, entry);
System.out.println("AddUser: added entry " + entryDN + ".");
} catch (NamingException e) {
System.err.println("AddUser: error adding entry." + e);
}
}
}
我收到以下错误:
AddUser:错误添加
entry.javax.naming.directory.NoSuchAttributeException:[LDAP:错误代码16 - 00000057:LdapErr:DSID-0C090C3E,注释:属性转换操作错误,数据0,v1db1];剩余名称cn = NewUser,dc = mydc,dc = local
我不知道错误在哪里。可能只是关于属性的错误输入?
答案 0 :(得分:0)
正确的代码如下:
public static void main(String[] args) {
String userName = "admin";
String password = "s3cret";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.10.10:389/DC=SOFTWAREDEV,DC=LOCAL");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, new String("softwaredev" + "\\" + userName));
env.put(Context.SECURITY_CREDENTIALS, password);
String path = "OU=SoftwareV3,OU=SOFTWARE";
String newUser = "myUser"; // insert user here
String entryDN = "CN=" + newUser + "," + path;
Attribute cn = new BasicAttribute("cn", newUser);
Attribute oc = new BasicAttribute("objectClass");
oc.add("top");
oc.add("person");
oc.add("organizationalPerson");
oc.add("user");
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
Attributes entry = new BasicAttributes();
entry.put(cn);
entry.put(oc);
ctx.createSubcontext(entryDN, entry);
System.out.println("AddUser: added entry " + entryDN + ".");
} catch (NamingException e) {
System.err.println("AddUser: error adding entry." + e);
}
}