Example #1
0
  public static void main(String[] args) {
    String ldapHost = "192.168.121.130";
    String loginDN = "cn=admin,dc=ucweb,dc=com";
    String password = "******";
    String containerName = "dc=ucweb,dc=com";

    int ldapPort = LDAPConnection.DEFAULT_SSL_PORT;
    int ldapVersion = LDAPConnection.LDAP_V3;

    LDAPJSSESecureSocketFactory ssf =
        new LDAPJSSESecureSocketFactory(TrustManager.createSSLSocketFactory());
    LDAPConnection lc = new LDAPConnection(ssf);

    LDAPAttributeSet attributeSet = new LDAPAttributeSet();

    attributeSet.add(
        new LDAPAttribute("objectclass", new String[] {new String("top"), new String("person")}));
    attributeSet.add(new LDAPAttribute("cn", "17"));
    attributeSet.add(new LDAPAttribute("sn", "17"));
    attributeSet.add(new LDAPAttribute("description", " "));
    //        attributeSet.add(new LDAPAttribute("userPassword", "111111"));
    String dn = "cn=17," + containerName;
    LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);

    try {
      lc.connect(ldapHost, ldapPort);
      lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
      System.out.println("login ldap server successfully.");
      lc.add(newEntry);
      System.out.println("Added object: " + dn + " successfully.");

    } catch (LDAPException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } finally {
      try {
        if (lc.isConnected()) {
          lc.disconnect();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }