装好了OpenLDAP,但不会加数据,急

fzy1314 2005-02-22 04:26:15
在windows下装好了OpenLDAP软件,也可以启动LDAP服务器,但就是不知道怎么录入数据,我想将姓名和密码录入,应该怎么做啊
...全文
330 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xieskyeagle 2005-03-12
安装好以后,按照readme文件里的说明,运行以下命令(ldap安装在c:\openldap):
C:\> cd openldap
C:\openldap>slapd -d 1
运行以后.在cmd窗口里停在sladp starting这里,光标一直在闪动,请问应该怎么做?
回复
CTSJT210 2005-03-09
fzy1314这个程序里的Env是什么意思
回复
fzy1314 2005-02-24
http://download.bergmans.us/openldap/openldap-2.2.19/openldap-2.2.19-db-4.3.21-openssl-0.9.7e-win32.exe

找到地址了,有谁研究出来告诉我怎么用好吗,万分感谢!
回复
54sunboy 2005-02-24
54sunboy@163.com


我也正解研究这东东,但没有找到for win版,一同研究啦。


3Q楼主
回复
fzy1314 2005-02-24
我也不知道在哪下的了,你告诉我你的邮箱吧,我发给你一个,你会不会用OPENLDAP,如果会给你帮助好吗?
回复
japanpigkiller 2005-02-23
楼主提供一个openLDAP的下载地址好吗?
回复
54sunboy 2005-02-23
楼主,openLdap能在win下安装么?for win在那里有下载?我去http://www.openldap.org/software/download/上没找到呢?你是怎么安装的?能指教么?
回复
fzy1314 2005-02-23
还有,Env到底是什么啊,我编译老是无法通过
回复
fzy1314 2005-02-23
谢谢楼上的大哥,可以解释一下,每个类是做什么的吗,因为我实在有点看不明白,我看你写的类里面好象没有添加信息的方法,是这样的吗?
回复
GJA106 2005-02-23

public static void test_RefSearch() throws Exception {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);

/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

/* Throw exception when referral is encountered */
env.put(Context.REFERRAL, "throw");

try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);

/* specify search controls to search subtree */
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

/* search for all entries with surname of Jensen */
NamingEnumeration results;

while (true) {
try {
results =
ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, controls);
/* for each entry print out name + all attrs and values */
while (results != null && results.hasMore()) {
SearchResult si = (SearchResult) results.next();

/* print its name */
System.out.println("name: " + si.getName());

Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("No attributes");
}
else {
/* print each attribute */
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
Attribute attr = (Attribute) ae.next();
String attrId = attr.getID();

/* print each value */
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " +
vals.nextElement()))
;
}
}
System.out.println();
}

break;

}
catch (ReferralException e) {

if (!followReferral(e.getReferralInfo())) {
if (!e.skipReferral()) {
return;
}
}
/* point to the new context */
ctx = (DirContext) e.getReferralContext();
}
}
}
catch (NamingException e) {
System.err.println("RefSearch example failed.");
e.printStackTrace();
}
}

private static boolean followReferral(Object referralInfo) throws
IOException {
byte[] reply = new byte[4];

System.out.println("Received referral: " + referralInfo);
System.out.print("Follow it? [y/n]");
System.in.read(reply);

return (reply[0] != 'n');
}

public static void test_Search() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MGR_DN); //Specify host and port to use for directory service
try {
DirContext ctx = new InitialDirContext(env); //get a handle to an Initial DirContext
SearchControls constraints = new SearchControls(); //specify search constraints to search subtree
// constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// search for all entries with surname of Jensen
NamingEnumeration results
//= ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, constraints);
= ctx.search("dc=detech,dc=com", "(sn=developer)", constraints);
if (results != null)
System.out.println(results);
//for each entry print out name + all attrs and values
while (results != null && results.hasMoreElements()) {
System.out.println("1");
SearchResult si = (SearchResult) results.next();
System.out.println("2");
// print its name
System.out.println("name: " + si.getName());
System.out.println("name: " + si.toString());

Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("No attributes");
}
else {
// print each attribute
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
Attribute attr = (Attribute) ae.next();
String attrId = attr.getID();

// print each value
for (Enumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " +
vals.nextElement()))
;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}

}

public static void test_Create() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); //Specify host and port to use for directory service
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);

try {
DirContext ctx = new InitialDirContext(env); //get a handle to an Initial DirContext
ctx.createSubcontext("cn=ganjianan,dc=portal,dc=detech,dc=com",Env.getGanjiananAttributes());
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void test_DeleteAttrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); /* Specify host and port to use for directory service */
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);

try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
ctx.destroySubcontext("cn=ganjianan,dc=portal,dc=detech,dc=com");
System.out.println("delete was successful.");
}
catch (NamingException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception{
LdapSun.test_Create();
}
}
回复
GJA106 2005-02-23
public static void test_Modifyrdn() {
/* Values we'll use in creating the entry */
Attribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("inetOrgPerson");

Attribute cn = new BasicAttribute("cn", "Jacques Smith");
Attribute sn = new BasicAttribute("sn", "Smith");
Attribute givenNames = new BasicAttribute("givenname", "Jacques");

/* Specify the DN we're adding */
String dn = "cn=Jacques Smith, " + Env.MY_MODBASE;
/* the destination DN */
String ndn = "cn=Jacques M Smith, " + Env.MY_MODBASE;
/* the new RDN */
String nrdn = "cn=Jacques M Smith";

Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);

/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

/* specify authentication information */
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);

DirContext ctx = null;

try {
/* get a handle to an Initial DirContext */
ctx = new InitialDirContext(env);
Attributes orig = new BasicAttributes();
orig.put(objClasses);
orig.put(cn);
orig.put(sn);
orig.put(givenNames);

/* Add the entry */
ctx.createSubcontext(dn, orig);
System.out.println("Added entry " + dn + ".");

}
catch (NameAlreadyBoundException e) {
/* If entry exists already, fine. Ignore this error. */
System.out.println("Entry " + dn +
" already exists, no need to add");
}
catch (NamingException e) {
System.err.println("Modrdn: problem adding entry." + e);
System.exit(1);
}

try {
/* Delete the destination entry, for this example */
ctx.destroySubcontext(ndn);
System.out.println("Deleted entry " + ndn + ".");

}
catch (NameNotFoundException e) {
/* If entry does not exist, fine. Ignore this error. */
System.out.println("Entry " + ndn + " is not in the directory. " +
"No need to delete.");
}
catch (NamingException e) {
System.err.println("Modrdn: problem deleting entry." + e);
System.exit(1);
}

/* Do the modrdn operation */
try {
ctx.rename(dn, ndn);
System.out.println("The modrdn operation was successful. Entry " +
dn + " has been changed to " + ndn + ".");
}
catch (NamingException e) {
System.err.println("Modify operation failed." + e);
}

}
public static void test_Rdentry() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);

/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);

/* Read Babs' entry */
Attributes attrs = ctx.getAttributes(Env.ENTRYDN);

if (attrs == null) {
System.out.println(Env.ENTRYDN + "has no attributes");
}
else {
/* print each attribute */
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
Attribute attr = (Attribute) ae.next();
String attrId = attr.getID();

/* print each value */
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " + vals.nextElement()))
;
}
}
}
catch (NamingException e) {
System.err.println("Rdentry example failed.");
e.printStackTrace();
}
}
回复
GJA106 2005-02-23
以下代码运行正常,不知道楼的环境如何了?

import java.util.Hashtable;
import java.util.Enumeration;

import java.util.*;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import java.io.IOException;
import com.sun.jndi.ldap.LdapCtx;

public class LdapSun {
public LdapSun() {
}

public static void test_Getattrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);

String[] attrs = new String[4];
attrs[0] = "cn"; /* Get canonical name(s) (full name) */
attrs[1] = "sn"; /* Get surname(s) (last name) */
attrs[2] = "mail"; /* Get email address(es) */
attrs[3] = "telephonenumber"; /* Get telephone number(s) */

Attributes result = ctx.getAttributes(Env.ENTRYDN, attrs);

if (result == null) {
System.out.println(Env.ENTRYDN +
"has none of the specified attributes.");
}
else {
/* print it out */
Attribute attr = result.get("cn");
if (attr != null) {
System.out.println("Full name:");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}

attr = result.get("sn");
if (attr != null) {
System.out.println("Last name (surname):");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}

attr = result.get("mail");
if (attr != null) {
System.out.println("Email address:");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}
attr = result.get("telephonenumber");
if (attr != null) {
System.out.println("Telephone number:");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}
}
}
catch (NamingException e) {
System.err.println("Getattrs example failed.");
e.printStackTrace();
}

}

public static void test_Modifyattrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); /* Specify host and port to use for directory service */
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);

try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
/* construct the list of modifications to make */
ModificationItem[] mods = new ModificationItem[2];
Attribute mod0 = new BasicAttribute("mail", "babs@eng");
// Update mail attribute
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
// Add another value to description attribute
Attribute mod1 = new BasicAttribute("description",
"This entry was modified with the Modattrs program on " +
(new Date()).toString());
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);
/* Delete the description attribute altogether */
/*
Attribute mod1 = new BasicAttribute("description");
mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod1);
*/

/* make the change */
ctx.modifyAttributes("cn=ganjianan,dc=portal,dc=detech,dc=com", mods);
System.out.println("modification was successful.");

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

}

回复
fzy1314 2005-02-23
没人能解决这个问题吗???55555555
回复
fzy1314 2005-02-22
那么在xx.ldif中我是怎么存储数据呢?比如,我想存储一个用户名:fzy 和密码:fzy 那么xx.ldif文件我应该怎么写,我又要怎么读取数据呢?

象我下面这样写是这对的吗???
public class LDAPPermission
{
private static Log log = LogFactory.getLog(LDAPPermission.class);

private static String ou = "ou=people";
private static String ldapHost = "127.0.0.1";
private static int ldapPort = 1234;
private static String baseDN = "o=fzy.com,o=fzy.com"; //这里是这样写吗
private DirContext ctx = null;
private String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
private int SCOPE = SearchControls.SUBTREE_SCOPE;

public LDAPPermission()
{
}

public boolean checkPermission(String username, String password)
{
String userName = "uid="+username+
",ou=people,o=fzy.com,o=fzy.com";
Object o = (Object)userName;
StringBuffer jndiHost = new StringBuffer();
jndiHost.append("ldap://");
jndiHost.append(ldapHost);
jndiHost.append(":");
jndiHost.append(ldapPort);

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
env.put(Context.PROVIDER_URL, jndiHost.toString());
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL,
"uid="+username+",ou=people,o=fzy.com,o=fzy.com");
env.put(Context.SECURITY_CREDENTIALS, password);
boolean isPass = false;
try
{
ctx = new InitialDirContext(env);

Attributes attrs = ctx.getAttributes(userName);
if(attrs!=null)
{
isPass = true;
}
} catch(NamingException e) //modified by xingchenyang
{
log.debug(e);
//ExcAct.doHandle(e);
return false;
}
return isPass;
}
}
回复
GJA106 2005-02-22
OpenLDAP只提供查询,没有提供录入/保存功能。想实现手工录入只能换其它的软件或自己写代码录入(通过xx.ldif文件来实现一性录入)
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-02-22 04:26
社区公告
暂无公告