sos,遇到了很头疼的问题
我的windows 2003 server系统中安装了Active Directory,域名为myhome.brh.local
打开"Active Directory用户与计算机"的界面,在myhome.brh.local下建了一个组织机构bmp,又在该组织机构下面建了一个用户text,我要写java程序来访问该用户的每个属性.
package com.brh.myADText.text;
import java.util.Properties;
import javax.naming.*;
import javax.naming.directory.*;
//include the JNDI in the classpath. You should use the same JDK used by WebSphere Application server.
public class TextLdap {
public static void main(String[] args) {
// ***************** user information to be authenticated
// ********************************
// *****************Please modify the following three properties
// accordingly ************
String ldapHost = "ldap://192.168.0.101:389"; // ldap host +
// port number
String DN = "ExAdmin@myhome.brh.local";
// String DN = new String();
// DN to be
// authenticated
String password = "brh_123456"; // DN's password
// ***************** End of user information
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.SECURITY_AUTHENTICATION, "simple"); // use simple
//
// mechanism
props.put(Context.SECURITY_CREDENTIALS, password);
props.put(Context.SECURITY_PRINCIPAL, DN);
props.put(Context.PROVIDER_URL, ldapHost);
// authentication
long start = System.currentTimeMillis();
long end = 0;
long time = 0;
try {
DirContext ctx = new InitialDirContext(props);
Attributes attrsl = ctx.getAttributes ("cn=text,cn=bmp,DN=myhome,DN=hrm,DN=local");
System.out.println("ok");
// Create the search controls
SearchControls searchCtls = new SearchControls();
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// specify the LDAP search filter
String searchFilter = "(&(OU=bmp)(CN=text))";
// String searchFilter = null;
// Specify the Base for the search
String searchBase = "";
// initialize counter to total the group members
int totalResults = 0;
// Specify the attributes to return
String returnedAtts[] = {"displayname","password"};
searchCtls.setReturningAttributes(returnedAtts);
// Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,
searchCtls);
// Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
System.out.println(">>>" + sr.getName());
//Print out the groups
Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) {
Attribute attr = (Attribute) ae.next();
System.out.println("AttributeID: " + attr.getID());
for (NamingEnumeration e = attr.getAll(); e.hasMore();
totalResults++) {
System.out.println("Attributes:"+e.next());
}
}
} catch (NamingException e) {
e.printStackTrace();
System.err.println("Problem listing membership: " + e);
}
}
}
//System.out.println("Total groups: " + totalResults);
ctx.close();
} catch (Exception ex) {
System.out.println("Exception is " + ex.toString());
}
}
}
程序中的searchBase,searchFilter这两个参数的值应该怎么写,我怎么该也不对,老是报错.
javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-031006C5, problem 5012 (DIR_ERROR), data 0],remaining name 'ou=bmp,DN=myhome,DN=brh,DN=local'
各位大哥大姐们,快帮帮小弟吧,我已经头疼了好几天了.