java中的LDAP搜索:DN包含,

发布于 2024-11-09 18:18:59 字数 475 浏览 0 评论 0原文

我目前在搜索 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 浏览器中可以正常工作。

I'm currently running into issues when searching for entries where the DN contains a comma:

StringTokenizer st = new StringTokenizer(dn, "=");
Attributes searchAttributes = new BasicAttributes(st.nextToken(), st.nextToken());
Enumeration results = ctx.search(baseDn, searchAttributes);

if (results.hasMoreElements()) {
  // ...
}

I tested both dn=first,second as well as dn=first\,second, and although the search runs correctly, I never get any results back. The same baseDn and dn works correctly in Eclipse/Apache Directory Studio LDAP Browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-11-16 18:18:59

依赖于库,例如通过使用 Novell ldap.jar

searchResults = lc.search(searchBase, searchScope, searchFilter, null, false);
//private String searchFilter = "(objectClass=*)"; 

再次依赖于库,因为 Directory Studio LDAP 浏览器可能有自己的驱动程序,并且某些方法可能没有实现,例如使用 ldap.jar 能够在 ActiveDirectory 中搜索

基本上所有库(包括 Windows ActiveDirectory 的 Java 驱动程序)都包含大量与库一起包装的示例,对于在驱动程序中实现的最重要的方法

编辑:

嗯,但是管理员提供了两个相关的

1/ 上下文访问权限(环境之间)
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);

// 然后我才能开始迭代....

depends of libraries, for example by using Novell ldap.jar is constuctor

searchResults = lc.search(searchBase, searchScope, searchFilter, null, false);
//private String searchFilter = "(objectClass=*)"; 

again depends or libraries, because maybe Directory Studio LDAP Browser has own driver, and some methods are implemented another maybe not, for example with ldap.jar is able to seach in ActiveDirectory

basically all libraries (including Java driver for Windows ActiveDirectory) contains tons of examples packed with library, for most importand of methods which are implemented into driver

EDIT:

hmmm, but there are two relevant

1/ access for context given by admin (between enviroments)
2/ with ActiveDirectory (always) and with (old PC) test enviroment for LDAP I must force for thread(s) some small pause

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);

// thenafter I can able to start Iterations....

心如狂蝶 2024-11-16 18:18:59

ldap查询的引用规则可以在http://www.rlmueller.net/CharactersEscaped.htm

我正在使用以下代码片段来查询 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();
        ...
    }

The quoting rules for ldap queries can be found at http://www.rlmueller.net/CharactersEscaped.htm

I'm using the following code snippet to query the cn, should work teh same for the 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();
        ...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文