LDAP高手请进!

shining_ljr 2003-12-24 11:20:55
java里面怎么样对OpenLdap进行读写操作啊?
...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicolas2 2003-12-26
  • 打赏
  • 举报
回复
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingException;
import java.util.Hashtable;

class GetAttrib {
public static void main(String[] argv) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
// specify where the ldap server is running
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=javacourses.com,c=Canada");
// use simple authenticate to authenticate the user
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "password");

try {
// Create the initial directory context
DirContext ctx = new InitialDirContext(env);

// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("cn=Qusay Mahmoud");

// Find the surname ("sn") attribute of this object and print it
System.out.println("Last Name: " + attrs.get("sn").get());

// Close the context
ctx.close();
} catch (NamingException e) {
System.err.println("Problem getting attribute: " + e);
}
}
}

再不行就没有办法了。
shining_ljr 2003-12-24
  • 打赏
  • 举报
回复
netscape的Ldap包哪里有下载啊
nicolas2 2003-12-24
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
import javax.naming.*;
//import psi.*;
import netscape.ldap.*;

public class LDAPTestConnect {
public static void main(String args[]){
String ldaphost="10.168.62.187";
int ldapport=389;
String userDN="cn=Directory Manager";
String password="1234";
LDAPConnection ld=new LDAPConnection();;
try{
ld.connect(ldaphost,ldapport);
ld.authenticate(userDN,password);
System.out.println("Connect ok=");
}catch(LDAPException e){
System.out.println("M:initLdapConn() Exception="+e.getMessage());
};
if (ld.isConnected()){
System.out.println("Version:"+ld.LDAP_VERSION);
try{
ld.disconnect();
}catch(LDAPException e){
System.out.println("M:initLdapConn() Exception="+e.getMessage());
}

}
}

}
这个程序用了netscape的jar包免费的,用jdk带的sun的包也是可以的。
nicolas2 2003-12-24
  • 打赏
  • 举报
回复
用jndi啊
shining_ljr 2003-12-24
  • 打赏
  • 举报
回复
用JNDI的话怎样实现啊

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧