【问题标题】:not able to create and fetch user in java from OpenDS active directory无法从 OpenDS 活动目录在 java 中创建和获取用户
【发布时间】:2012-07-30 14:02:03
【问题描述】:

DirContext ldapContext;

String baseName = "dc=MyCompany,dc=com";

String serverIP = "xx.xxx.xxx.xxx";

公共 ADConnection() {

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);

 }

}

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,"); }

在创建用户和获取用户后,我在控制台中遇到以下错误。

javax.naming.directory.InitialDirContext@18eb9e6

名称:user1,属性:{displayname=displayName:user1 User, givenname=givenName:user1,

objectclass=objectclass: top, person, organizationsperson, user,

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

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

创建错误:javax.naming.NameNotFoundException:[LDAP:错误代码 32 - 提供的条目

cn=user1,dc=MyCompany,dc=com 无法添加,因为其后缀未定义为

目录服务器中的后缀];剩余名称 'cn=user1,dc=MyCompany,dc=com'

javax.naming.NameNotFoundException:[LDAP:错误代码 32 - 提供的条目

cn=user1,dc=MyCompany,dc=com 无法添加,因为其后缀未定义为

目录服务器中的后缀];剩余名称 'cn=user1,dc=MyCompany,dc=com'

【问题讨论】:

    标签: java ldap


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2011-07-06
      相关资源
      最近更新 更多