2012-07-30 54 views
1

不能創建和OpenDS的活動目錄

DirContext ldapContext;

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

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

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

 

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

[email protected]

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'

回答

0

在Java中獲取用戶是否爲dc=mycompany,dc=com EXI後端ST?如果不是,則必須創建它並至少填充基礎對象dc=mycompany,dc=com

+0

問題solve.i給了錯誤的ldap端口 – user739115 2012-07-31 07:12:23