我在搜索DN包含逗号的条目时遇到了问题:
StringTokenizer st = new StringTokenizer(dn, "=");
Attributes searchAttributes = new BasicAttributes(st.nextToken(), st.nextToken());
Enumeration results = ctx.search(baseDn, searchAttributes);
if (results.hasMoreElements()) {
// ...
}
我测试了dn=first,second
以及dn=first\,second
和虽然搜索运行正常,但我从未得到任何结果。相同的baseDn和dn在Eclipse / Apache Directory Studio LDAP浏览器中正常工作。
答案 0 :(得分:1)
取决于库,例如使用Novell ldap.jar是constuctor
searchResults = lc.search(searchBase, searchScope, searchFilter, null, false);
//private String searchFilter = "(objectClass=*)";
再取决于或库,因为可能Directory Studio LDAP浏览器有自己的驱动程序,而某些方法可能没有实现,例如ldap.jar能够在ActiveDirectory中搜索
基本上所有库(包括Windows ActiveDirectory的Java驱动程序)都包含大量包含库的示例,对于大多数导入以及在驱动程序中实现的方法
编辑:
嗯,但有两个相关的1 /访问admin给出的上下文(环境之间) 2 /使用ActiveDirectory(总是)和(旧PC)测试环境用于LDAP我必须强制线程暂停一些小暂停
private void readData() {
searchResults = new LDAPSearchResults();
try {
Thread.sleep(450);
} catch (InterruptedException ex) {
Logger.getLogger(Profylaxia.class.getName()).log(Level.SEVERE, null, ex);
}
try {
searchResults = lc.search(searchBase, searchScope, searchFilter, null, false);
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(Profylaxia.class.getName()).log(Level.SEVERE, null, ex);
}
int noResult = searchResults.getCount();
System.out.println(" noResult : " + noResult);
//之后我可以开始迭代......
答案 1 :(得分:1)
可以在http://www.rlmueller.net/CharactersEscaped.htm
找到ldap查询的引用规则我正在使用以下代码片段来查询cn,对于dn应该是同样的工作:
String searchFilter = "(&(objectClass=user)(cn=" + query + "))";
SearchControls searchControls = new SearchControls();
String[] resultAttributes = {"cn", "distinguishedName", "displayName", "lastLogon", "description"};
searchControls.setReturningAttributes(resultAttributes);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration ne = getContext().search(root, searchFilter, searchControls);
List<DirectoryUser> result = new ArrayList<DirectoryUser>();
while (ne.hasMoreElements()) {
SearchResult searchResult = (SearchResult)ne.nextElement();
Attributes attrs = searchResult.getAttributes();
...
}