最初の質問 (最初の部分は Active Directory へのアクセスでした) を手伝ってくれた Sotirios Delimanolis にまず感謝します。
だから今私のコードは次のとおりです。
DirContext ctx = null;
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://"+serverAddress+":389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, DOMAIN+username);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
// Create the initial context
ctx = new InitialDirContext(env);
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("mail", "XXXXXX@XXXX.com"));
matchAttrs.put(new BasicAttribute("cn"));
// Search for objects that have those matching attributes
NamingEnumeration<SearchResult> answer = ctx.search("ou=People", matchAttrs);
while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
}
エラーがあります:
Failed to bind to LDAP / get account information: javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr: DSID-03100754, problem 5012 (DIR_ERROR), data 0 ; remaining name 'ou=People'
http://docs.oracle.com/javase/jndi/tutorial/basics/directory/basicsearch.htmlでこのコード(次のコード)を見つけました:
// Specify the attributes to match
// Ask for objects that has a surname ("sn") attribute with
// the value "Geisel" and the "mail" attribute
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("sn", "Geisel"));
matchAttrs.put(new BasicAttribute("mail"));
// Search for objects that have those matching attributes
NamingEnumeration answer = ctx.search("ou=People", matchAttrs);
You can then print the results as follows.
while (answer.hasMore()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println(">>>" + sr.getName());
printAttrs(sr.getAttributes());
}
したがって、ターゲットコンテキスト「ou = People」が各アクティブディレクトリに固有であるか、「ou」と「People」で常に同じであるかを知りたい: http://www.kouti.com/tables/userattributes. htm
どうもありがとう !