【发布时间】:2012-04-03 13:15:26
【问题描述】:
我试图通过搜索employeeID 属性(每个员工都是唯一的)来查找用户名的CN。我已经让它返回一个包含所有属性的字符串,但我希望它只返回用户的 CN(例如:'John Doe' 或 'cn=John Doe';两者都可以)
public void getEmployeeId(String id) {
// TODO stuff
String groupName = "ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE";
try {
System.out.println("Creating initial directory context...");
LdapContext ctx = new InitialLdapContext(env, null);
// Create default search controls
SearchControls ctls = new SearchControls();
// Search for user with 'id' as value for employeeID attribute
String filter = "(&(employeeID=" +id + "))";
// Search for objects using filter
NamingEnumeration answer = ctx.search(groupName, filter, ctls);
// Print the answer
// Search.printSearchEnumeration(answer);
System.out.println("-----------------");
System.out.println(answer.next());
System.out.println("-----------------");
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
注意:我知道你可以只从字符串中剪切部分,但我希望它只返回我需要的值。
【问题讨论】:
标签: java active-directory ldap