java连接Ldap 仅做个人学习之用
package com.nxw.test;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.UUID;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class Ldap {
public static void main(String[] args) {
String url = "ldap://127.0.0.1:389/";
String domain = "dc=nxw,dc=org";
String user = "cn=manager";
String password = "secret";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工厂
env.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, user+","+domain); // 填DN
env.put(Context.SECURITY_CREDENTIALS, password); // AD Password
env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");
LdapContext ldapCtx = null;
try {
ldapCtx = new InitialLdapContext(env , null);
queryGroup(ldapCtx);
} catch (NamingException e) {
e.printStackTrace();
} finally {
if(ldapCtx != null) {
try {
ldapCtx.close();
} catch (NamingException e) {
}
}
}
}
private static void queryGroup(LdapContext ldapCtx) throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "objectClass=organizationalUnit";
String searchBase = "dc=nxw,dc=org";
String returnedAtts[] = {"distinguishedName", "objectGUID", "name"};
searchCtls.setReturningAttributes(returnedAtts);
NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes Attrs = sr.getAttributes();
if (Attrs != null) {
NamingEnumeration<?> ne = Attrs.getAll();
while(ne.hasMore()) {
Attribute Attr = (Attribute)ne.next();
String name = Attr.getID();
Enumeration<?> values = Attr.getAll();
if (values != null) { // 迭代
while (values.hasMoreElements()) {
String value = "";
if("objectGUID".equals(name)) {
value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();
} else {
value = (String)values.nextElement();
}
System.out.println(name + " " + value);
}
}
}
System.out.println("=====================success!");
}
}
}
}
通过java实现对Ldap的增删改操作 http://blog.csdn.net/nxw_tsp/article/details/52690376
所需jar包 http://download.csdn.net/detail/nxw_tsp/9642131
本博文参考 http://blog.csdn.net/zfpigpig/article/details/21176697