1.连接
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class ActiveDirectoryConntct1
{
public static void main(String[] args)
{
String account="user1";
String password="password1";
String root="dc=test,dc=com"; //root
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.1:389/" + root);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid=" + account + "," + root);
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx = null;
try
{
ctx = new InitialDirContext(env);
System.out.println("认证成功");
}
catch(javax.naming.AuthenticationException e)
{
System.out.println("认证失败");
}
catch(Exception e)
{
System.out.println("认证出错:");
e.printStackTrace();
}
if(ctx != null)
{
try
{
ctx.close();
}
catch (NamingException e)
{
//ignore
}
}
System.exit(0);
}
}
2.查询
DirContext ctx = ...
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
NamingEnumeration en = ctx.search("", "uid=*", constraints); //查询所有用户
while (en != null && en.hasMoreElements())
{
Object obj = en.nextElement();
if(obj instanceof SearchResult)
{
SearchResult si = (SearchResult) obj;
System.out.println("name: " + si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null)
{
System.out.println("No attributes");
}
else
{
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();)
{
Attribute attr = (Attribute) ae.next();
String attrId = attr.getID();
for (Enumeration vals = attr.getAll();vals.hasMoreElements();)
{
System.out.print(attrId + ": ");
Object o = vals.nextElement();
if(o instanceof byte[])
System.out.println(new String((byte[])o));
else
System.out.println(o);
}
}
}
}
else
{
System.out.println(obj);
}
System.out.println();
}
...
ctx.close();
3.添加
DirContext ctx = ...;
String newUserName = "user2";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("inetOrgPerson");
attrs.put(objclassSet);
attrs.put("sn", newUserName);
attrs.put("uid", newUserName);
ctx.createSubcontext("uid=" + newUserName, attributes);
...
4.删除
String account = "user2";
DirContext ctx = ...;
ctx.destroySubcontext("uid=" + account);
5.修改
String account = "user2";
String newDisplayName = "newDisplayName";
ModificationItem modificationItem[] = new ModificationItem[1];
modificationItem[0] =
new ModificationItem(
DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("displayName", newDisplayName);
DirContext ctx = ...;
ctx.modifyAttributes("uid=" + account, modificationItem);
posted on 2005-10-27 16:40
my java 阅读(1375)
评论(1) 编辑 收藏 所属分类:
JNDI