gpt4 book ai didi

java - 无法在 Java 中从 OpenDS Activity 目录创建和获取用户

转载 作者:行者123 更新时间:2023-12-01 05:13:15 26 4
gpt4 key购买 nike



<p>DirContext ldapContext;</p>

<p>String baseName = "dc=MyCompany,dc=com";</p>

<p>String serverIP = "xx.xxx.xxx.xxx";</p>

<p>public ADConnection() {</p>

<pre><code>try {

Hashtable ldapEnv = new Hashtable(11);

ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

ldapEnv.put(Context.PROVIDER_URL, "ldap://xx.xx.xx.xx:389");

ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");

ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");

ldapEnv.put(Context.SECURITY_CREDENTIALS, "xxxx");

ldapContext = new InitialDirContext(ldapEnv);

System.out.println(ldapContext);



}
catch (Exception e) {

System.out.println(" bind error: " + e);

e.printStackTrace();

System.exit(-1);

}
</code></pre>

<p>}</p>



public void createNew(String username, String surname, String givenName) {

try {

String distinguishedName = "cn=" + username + baseName;

Attributes newAttributes = new BasicAttributes(true);

Attribute oc = new BasicAttribute("objectclass");

oc.add("top");

oc.add("person");

oc.add("organizationalperson");

oc.add("user");

newAttributes.put(oc);

newAttributes.put(new BasicAttribute("sAMAccountName", username));

newAttributes.put(new BasicAttribute("userPrincipalName", username + "@" + serverIP));

newAttributes.put(new BasicAttribute("cn", username));

newAttributes.put(new BasicAttribute("sn", surname));

newAttributes.put(new BasicAttribute("givenName", givenName));

newAttributes.put(new BasicAttribute("displayName", givenName + " " + surname));

System.out.println("Name: " + username + " Attributes: " + newAttributes);

ldapContext.createSubcontext(distinguishedName, newAttributes);

}
catch (Exception e) {

System.out.println("create error: " + e);

e.printStackTrace();

System.exit(-1);

}

}



public Attributes fetch(String username) {

Attributes attributes = null;

try {
System.out.println("fetching: " + username);

DirContext o = (DirContext)ldapContext.lookup("cn=" + username+ baseName);

System.out.println("search done\n");

attributes = o.getAttributes("");

for (NamingEnumeration ae = attributes.getAll(); ae.hasMoreElements();) {


Attribute attr = (Attribute)ae.next();

String attrId = attr.getID();

for (NamingEnumeration vals = attr.getAll(); vals.hasMore();) {

String thing = vals.next().toString();

System.out.println(attrId + ": " + thing);

}
}
}
catch (Exception e) {

System.out.println(" fetch error: " + e);

System.exit(-1);
}
return attributes;
}

public static void main(String[] args) {


adc.createNew("user1,", "User", "user1");

Attributes a = adc.fetch("user1,");
}

while creating user and after fetching user I am getting below error in the console.

javax.naming.directory.InitialDirContext@18eb9e6

Name: user1, Attributes: {displayname=displayName: user1 User, givenname=givenName: user1,

objectclass=objectclass: top, person, organizationalperson, user,

samaccountname=sAMAccountName: user1,, sn=sn: User, userprincipalname=userPrincipalName:

user1,@xx.xxx.xxx.xxx, cn=cn: user1,}

create error: javax.naming.NameNotFoundException: [LDAP: error code 32 - The provided entry

cn=user1,dc=MyCompany,dc=com cannot be added because its suffix is not defined as one of the

suffixes within the Directory Server]; remaining name 'cn=user1,dc=MyCompany,dc=com'

javax.naming.NameNotFoundException: [LDAP: error code 32 - The provided entry

cn=user1,dc=MyCompany,dc=com cannot be added because its suffix is not defined as one of the

suffixes within the Directory Server]; remaining name 'cn=user1,dc=MyCompany,dc=com'

最佳答案

dc=mycompany,dc=com 的后端是否存在?如果没有,您必须创建它并至少填充基础对象 dc=mycompany,dc=com

关于java - 无法在 Java 中从 OpenDS Activity 目录创建和获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11723120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com