项目要用到域登陆,还不知道是AD还是domino,逼得我要整个地去研究ldap。
看了现有两个LDAP开发的框架,JLDAP是像JDBC一样的用法,一般是这样用:
需要下载一个叫做ldap.jar的包,在csdn就有
LDAPConnection conn = new LDAPConnection();
try
{
conn.connect(paramsBean.getHostname(), (int) new Integer(paramsBean
.getHostport()));
String distinguishedName = paramsBean.getRoot() + ","
+ paramsBean.getDomain();
System.out.println(distinguishedName);
// conn.bind("version","DN","password");
conn.bind(LDAPConnection.SCOPE_SUB, "cn=kehaoinfo//dengjianbin", password);
return conn;
}
catch (LDAPException le)
{
try
{
if (conn != null)
conn.disconnect();
System.out.println("conn.disconnect()");
}
catch (Exception e)
{
e.printStackTrace();
}
le.printStackTrace();
}
finally
{
if (conn != null)
try
{
conn.disconnect();
}
catch (LDAPException e)
{
e.printStackTrace();
}
}
而关于openLDAP,可以去看看这个
http://www.ringkee.com/note/opensource/openldap.htm
我主要要说的,还是ldap的结构,他和现在的数据库结构很不一样,可以参考这个:
LDAP管理员指南 仅前七章
去google一下上面这个内容,可以看到。
目录访问主要是通过DN来访问的,只要知道了DN的构成,差不多就能应用ldap来访问ldap服务器了,下面就说一说DN的构成:
DN="[root,][domain]"
一般网址都是形如:[www|XX].[orgName].[orgType].[country] 的样式,如www.hzau.edu.cn
这里,DN值的[domain]部分就可以是
DC=hzau,DC=edu,DC=cn
这个DN就指向了这个域,可以直接使用这个DN值来遍历所有能访问的entry记录。
一般在一个机构内,都会有把自己按职能分成很多个部门,如后勤部的网址为www.hzau.edu.cn/hqb
那么,一般这个部门就会有相应的root,root 值一般可由自定义的schema和约定的字段和值组成,如后勤部的成员users属性attribute的root可能是如下值之一:
OU=users,CN=hqb
CN=users,CN=hqb
如果有一个后勤部成员的schema指定了他的name=member,email=member@XXX.xx....等,
我们就可以使用一个这样的DN指向他
name=member,CN=users,CN=hqb,DC=hzau,DC=edu,DC=cn
如果使用JNDI访问这个DN,就可以使用过滤直接访问到这个entry并取出他的参数,只要你知道他的schema
这里把我写的代码放出,需要用到dom4j-1.6.1.jar, jaxen-1.1-beta-9.jar, 或更高版本的包。
/**
* 主程序,这个是针对AD的,他的用户名有
*/
public class LDAPBase
{
public void getADInfo()
{
Hashtable HashEnv = new Hashtable();
String LDAP_URL = "ldap://192.168.0.3:389"; // LDAP访问地址
// String adminName = "CN=OAWebUser,CN=Users,DC=Hebmc,DC=com";//AD的用户名
String adminName = "administrator@khinfo.com"; // 注意用户名的写法:domain/User 或
// User@domain.com
String adminPassword = "abcdef"; // 密码
HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD
// Password
HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
HashEnv.put(Context.PROVIDER_URL, LDAP_URL);
LdapContext ctx = null;
try
{
ctx = new InitialLdapContext(HashEnv, null);
SearchControls searchCtls = new SearchControls(); // Create the
// search
// controls
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
// the
// search
// scope
String searchFilter = "objectClass=*"; // specify the LDAP
// search filter
// String searchFilter = "objectClass=organizationalUnit";//specify
// the LDAP search filter
String searchBase = "CN=ldap,CN=Users,DC=khinfo,DC=com"; // Specify
// the
// Base
// for
// the
// search//搜索域节点
int totalResults = 0;
// Specify the attributes to return
// String returnedAtts[] = {"memberOf"};//定制返回属性,要知道他的schema,或使用*
String returnedAtts[] = {
// "url", "employeeID",
"name" // "telephoneNumber",
// "mobile"//,
// ,"department"
,"mail" }; // 定制返回属性
searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集
// Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,
searchCtls);
while (answer.hasMoreElements())
{
SearchResult sr = (SearchResult) answer.next();
System.out
.println("************************************************");
System.out.println(sr.getName());
Attributes Attrs = sr.getAttributes();
if (Attrs != null)
{
try
{
for (NamingEnumeration ne = Attrs.getAll(); ne
.hasMore();)
{
Attribute Attr = (Attribute) ne.next();
System.out.println(" AttributeID="
+ Attr.getID().toString());
// 读取属性值
for (NamingEnumeration e = Attr.getAll(); e
.hasMore(); totalResults++)
{
System.out.println(" AttributeValues="
+ e.next().toString()+" ");
}
System.out.println(" ---------------");
// 读取属性值
Enumeration values = Attr.getAll();
if (values != null)
{ // 迭代
while (values.hasMoreElements())
{
System.out.println(" AttributeValues="
+ values.nextElement());
}
}
System.out.println(" ---------------");
}
}
catch (NamingException e)
{
try
{
if (ctx != null)
ctx.close();
}
catch (NamingException e1)
{
e1.printStackTrace();
}
System.err.println("Throw Exception : " + e);
}
}
}
System.out.println("Number: " + totalResults);
ctx.close();
}
catch (NamingException e)
{
try
{
if (ctx != null)
ctx.close();
}
catch (NamingException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
System.err.println("Throw Exception : " + e);
}
finally
{
try
{
if (ctx != null)
ctx.close();
}
catch (NamingException e1)
{
e1.printStackTrace();
}
}
}
public static void main(String args[])
{
LDAPBase ad = new LDAPBase();
ad.getADInfo();
}
}
一个BEAN
package ldapbean;
public class LdapBean
{
private String domain;
private String dn;
private String hostname;
private String hostport;
public String getDomain()
{
return domain;
}
public void setDomain(String domain)
{
this.domain = domain;
}
public String getDn()
{
return dn;
}
public void setDn(String dn)
{
this.dn = dn;
}
public String getHostname()
{
return hostname;
}
public void setHostname(String hostname)
{
this.hostname = hostname;
}
public String getHostport()
{
return hostport;
}
public void setHostport(String hostport)
{
this.hostport = hostport;
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<domain name="domain" decription="域值">khinfo.com</domain>
<dn name="dn" decription="根节点">CN=ldap,cn=Users,dc=khinfo,dc=com</dn>
<hostname name="hostname" decription="AD主机名称">192.168.0.3</hostname>
<hostport name="hostport" decription="AD主机端口">389</hostport>
</list>
这里参考了别人的代码,记不得那里copy来的了,这里说声谢谢