如何以编程方式将用户帐户添加到openDS?

时间:2011-02-28 13:07:27

标签: java ldap opends

我需要以编程方式将一些用户帐户添加到openDS服务器,但即使查看openDS wiki后我也不知道如何操作。有谁可以帮助我?

3 个答案:

答案 0 :(得分:5)

以下代码使用的是jndi。这只会添加一个提供密码的用户对象。这并不多。但这可能会帮助您开始。

与opends-sdk相比,我更愿意坚持使用jndi。

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import javax.naming.NamingException;

public class App {

    /* Ugly HardCoded stuff */
    public static String ldapUri = "ldap://localhost:2389";
    public static String admindn = "cn=Directory Manager";
    public static String admincred = "password";
    public static String usersContainer = "ou=users,dc=example,dc=com";

    public static void main(String args[]){

    if (args.length != 2) {
        System.out.println("Usage: App userName password");
        return;
    }
    String username = args[0];
    String password = args[1];

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapUri);
            env.put( Context.SECURITY_PRINCIPAL, admindn );
            env.put( Context.SECURITY_CREDENTIALS, admincred );
    try {
            DirContext ctx = new InitialDirContext(env);

        Attributes attrs = new BasicAttributes(true);

        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("inetorgperson");

        Attribute surname = new BasicAttribute("sn");
        surname.add(username);

        Attribute pwd = new BasicAttribute("userpassword");
        pwd.add(password);

        attrs.put(objclass);
        attrs.put(surname);
        attrs.put(pwd);

        ctx.createSubcontext("cn="+username+","+usersContainer, attrs);
        ctx.close();


    } catch (NamingException e) {
        e.printStackTrace();
    }


    }
 }

答案 1 :(得分:2)

要在OpenDS中以编程方式添加用户帐户,您需要为您的操作系统和首选编程语言使用LDAP客户端库。 OpenDS有一个用于Java的LDAP库,包含许多示例代码。 http://www.opends.org/promoted-builds/sdk/20110126210001/ 示例位于Example目录中。

答案 2 :(得分:0)

这里的php中使用的代码对我来说很好

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
<?php
$ldapconfig['host'] = 'PC100';
$ldapconfig['port'] = 1389;
$ldapconfig['basedn'] = 'dc=company,dc=com';

$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);

$password=1;
$username="cn=Directory Manager";


if ($bind=ldap_bind($ds, $username, $password)) {
  echo("Login correct");

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); // IMPORTANT
 $dn = "cn=roshanis,dc=example,dc=com"; 


    $ldaprecord['cn'] = "roshanis";
    $ldaprecord['givenName'] = "mkljl";
    $ldaprecord['sn'] = "roshan";
    $ldaprecord['objectclass'][0] = "inetOrgPerson";    
    $ldaprecord['objectclass'][1] = "test";
    $ldaprecord['mail'] = "lkl@fh.com";






    // add data to directory
    $r = ldap_add($ds, $dn, $ldaprecord);

   // $r= ldap_modify($ds, $dn, $ldaprecord);

} else {

  echo("Unable to bind to server.</br>");


}
?>

</body>
</html>
相关问题