本教程将向您展示如何编写Java代码以与LDAP交互。 但是在执行此操作之前,我们需要在计算机上设置LDAP服务器和客户端。
如果此时您不确定到底是什么LDAP,建议您参考这篇文章,其中提供了一个很好的定义示例。 (简而言之,有助于将LDAP服务器视为专门的数据库)。
安装LDAP服务器
我在MBP上运行。 环顾了一会后,我发现最容易安装的LDAP服务器是ApacheDirectory,您可以从此处下载。 (安装和启动服务器应少于5分钟)
安装完成后,它将自动启动守护程序。 然后,您可以使用此命令运行服务器。
sudo launchctl start org.apache.directory.server
有关更多安装说明,请参见此处 。
LDAP客户端
您将要查看LDAP服务器的内容。 最容易安装的LDAP客户端是Apache Directory Studio,可以从此处下载。
下载完成后,您需要创建与服务器的连接- 此处包含有关说明。
连接后,Apache Directory Studio应该如下所示:
现在可以从Java程序访问LDAP。 向您展示如何执行此操作的最佳方法是通过示例程序。 该程序将执行以下任务:
- 创建一个新的LDAP对象
- 查看LDAP对象
- 将新属性添加到LDAP对象
- 修改LDAP对象上的属性
- 删除LDAP对象上的属性
- 删除LDAP对象
注意:此类在其自身之后进行清除,即它将LDAP Server保留在其被发现的状态。 如果要查看正在执行的各种任务,只需运行其中一项任务,然后通过LDAP客户端查看LDAP对象。 不要忘记,您可以在LDAP客户端中修改对象并进行测试。
package test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;
public class LDAPTest {
public void run() {
try {
DirContext context = getContext();
String name = "employeeNumber=00001,ou=system";
createLDAPObject(context, name);
createAttribute(context, name, "displayName", "JOBS");
viewAttribute(context, name, "displayName");
updateAttribute(context, name, "displayName", "STEVE");
viewAttribute(context, name, "displayName");
removeAttribute(context, name, "displayName");
removeLDAPObject(context, name);
} catch (NamingException e) {
e.printStackTrace();
}
}
private void removeLDAPObject(DirContext context, String name) throws NamingException {
context.destroySubcontext(name);
}
private void createLDAPObject(DirContext context, String name) throws NamingException {
Attributes attributes = new BasicAttributes();
Attribute attribute = new BasicAttribute("objectClass");
attribute.add("inetOrgPerson");
attributes.put(attribute);
Attribute sn = new BasicAttribute("sn");
sn.add("Steve");
attributes.put(sn);
Attribute cn = new BasicAttribute("cn");
cn.add("Jobs");
attributes.put(cn);
attributes.put("telephoneNumber", "123456");
context.createSubcontext(name, attributes);
}
private void removeAttribute(DirContext context, String name , String attrName) throws NamingException {
Attribute attribute = new BasicAttribute(attrName);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void createAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException {
Attribute attribute = new BasicAttribute(attrName, attrValue);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void updateAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException {
Attribute attribute = new BasicAttribute(attrName, attrValue);
ModificationItem[] item = new ModificationItem[1];
item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
context.modifyAttributes(name, item);
}
private void viewAttribute(DirContext context, String name , String attrName) throws NamingException {
Attributes attrs = context.getAttributes(name);
System.out.println(attrName + ":" + attrs.get(attrName).get());
}
private DirContext getContext() throws NamingException {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
return new InitialDirContext(properties);
}
public static void main(String[] args) {
new LDAPTest().run();
}
}
该代码在下面,应该可以自我解释。