日期:2014-05-20  浏览次数:20764 次

用java访问windows的AD的问题。急!!!帮顶也有分!!
现在的项目中需要用java连接DA中去查找用户和部门信息。
我在网上找的代码,但是总是出错,请各位大侠帮我看看,指点一下。
public class Ldap {
private DirContext ctx = null;

//然后是初始化:
public void init() {
String account = "admin";//操作LDAP的帐户。默认就是Admin。
String password = "hg123@szdbt";//帐户Admin的密码。
String root = "dc=dbt.com.cn,dc=com"; //所操作的WLS域。也就是LDAP的根节点的DC
Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");//必须这样写,无论用什么LDAP服务器。
env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + ",cn=users,"+ root); //载入登陆帐户和登录密码
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.PROVIDER_URL, "ldap://192.168.1.100:389/" + root);

try {  
ctx = new InitialDirContext(env);//初始化上下文
System.out.println("认证成功");//这里可以改成异常抛出
} catch (javax.naming.AuthenticationException e) {
e.printStackTrace();
System.out.println("认证失败");
} catch (Exception e) {
System.out.println("认证出错:" + e);
}
}

//查询操作:
public void search() {//我只能按照某些属性查找节点,偶还不会怎么查找一个目录或按照更复杂的正则式查找特定节点/目录
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String s="DBT\\zhoushiyong";
NamingEnumeration en = ctx.search("", "uid=" + s, constraints); (就在这一句报错!!!!!!)
if (en == null) {
System.out.println("Have no NamingEnumeration.");
}
if (!en.hasMoreElements()) {
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()) {//可以查出多个元素
Object obj = en.nextElement();
if (obj instanceof SearchResult) {
SearchResult si = (SearchResult) obj;
System.out.println("\tname: " + si.getName());
Attributes attrs = si.getAttributes();
if (attrs == null) {
System.out.println("\tNo 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("\t\t" + 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();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in search():" + e);
}
}

//关闭连接:
public void close() {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
}

public static void main(String args[]){
Ldap ladp=new Ldap();
ladp.init();
ladp.search();
ladp.close();
}
}

运行的结果报错:

认证成功
javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece]; remaining name ''
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3025)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2931)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2737)
at com.sun.jndi.ldap.LdapCtx.searchAux(LdapCtx.java:1808)