Java 如何以编程方式将用户帐户添加到openDS?
我需要以编程的方式向openDS服务器添加一些用户帐户,但即使在浏览了全文之后,我也不知道该如何做。有人能帮我吗?要在OpenDS中以编程方式添加用户帐户,您需要为您的操作系统和首选编程语言使用LDAP客户端库。 OpenDS有一个用于Java的LDAP库,其中包含许多示例代码。Java 如何以编程方式将用户帐户添加到openDS?,java,ldap,opends,Java,Ldap,Opends,我需要以编程的方式向openDS服务器添加一些用户帐户,但即使在浏览了全文之后,我也不知道该如何做。有人能帮我吗?要在OpenDS中以编程方式添加用户帐户,您需要为您的操作系统和首选编程语言使用LDAP客户端库。 OpenDS有一个用于Java的LDAP库,其中包含许多示例代码。 示例在示例目录中。下面的代码正在使用jndi。这将只添加一个具有提供密码的用户对象。这并不多。但这可能会帮助你开始 与OpenDSSDK相比,我更愿意坚持使用jndi import java.util.Hashtabl
示例在示例目录中。下面的代码正在使用jndi。这将只添加一个具有提供密码的用户对象。这并不多。但这可能会帮助你开始 与OpenDSSDK相比,我更愿意坚持使用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();
}
}
}
在这里,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>
谢谢Ludo,但我没有找到添加用户帐户的示例。你能给我一个URL吗?你在找opends sdk的例子吗?是的,我跟踪了Ludo的URL,但没有找到添加用户帐户的例子。您有吗?您好,您可能想看看OpenDJ LDAP SDK(它是OpenDS的积极开发分支)。有关于如何使用客户端库管理帐户的代码示例和文档(添加、修改…):。示例代码如下: