简体   繁体   English

java-如何告诉LDAP DirContext.search(...)。hasMore()返回false而不是抛出PartialResultException

[英]java- how to tell LDAP DirContext.search(…).hasMore() to return false instead of throwing a PartialResultException

I set-up the LDAP DirContext.search(...) to ignore referrals, but I still get a referral exception when I call NamingEnumeration.hasMore(). 我设置LDAP DirContext.search(...)来忽略引用,但是当我调用NamingEnumeration.hasMore()时,我仍然得到引用异常。

Exception in thread "main" javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name 'DC=company,DC=com'
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2846)
    at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2820)
    at com.sun.jndi.ldap.LdapNamingEnumeration.getNextBatch(LdapNamingEnumeration.java:129)
    at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:198)
    at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(LdapNamingEnumeration.java:171)

Can I tell DirContext.search to ignore referrals, so that NamingEnumeration.hasMore() returns false instead of throwing an exception? 我可以告诉DirContext.search忽略引用,以便NamingEnumeration.hasMore()返回false而不是抛出异常吗?

this is the snipped: 这是剪辑:

import javax.naming.*;
import javax.naming.directory.*;

Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, ldapInitContextFactory);
p.setProperty(Context.PROVIDER_URL, ldapURL);
p.setProperty(Context.SECURITY_CREDENTIALS, ldapPassword);
p.setProperty(Context.SECURITY_PRINCIPAL, ldapUser);
p.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
p.setProperty(Context.REFERRAL, "ignore");
DirContext ctx = new InitialDirContext(p);

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setDerefLinkFlag(false);

NamingEnumeration e = ctx.search(ldapBaseDN, ldapQuery, null, searchControls);

for (; e.hasMore();) {
    SearchResult sr = (SearchResult) e.next();
    System.out.println("\nSearch Result: " + sr.getName());
}

Note: if I enable referrals, I get an LdapReferralException expcetion when I call NamingEnumeration.hasMore(). 注意:如果我启用了引用,当我调用NamingEnumeration.hasMore()时,我会得到一个LdapReferralException expcetion。

javax/naming/NamingEnumeration.java
public interface NamingEnumeration<T> extends Enumeration<T> {
    public boolean hasMore() throws NamingException;
    public T next() throws NamingException;
    ...
}

java/util/Enumeration.java
public interface Enumeration<E> {
    boolean hasMoreElements();
    E nextElement();
}

calling e.hasMoreElements() instead of e.hasMore() solves the issue. 调用e.hasMoreElements()而不是e.hasMore()解决了这个问题。 that is, it returns false (instead of throwing an exception) when there are referrals. 也就是说,当有引用时,它返回false(而不是抛出异常)。

however, it is not an optimal solution, in the sense that we might be missing other real NamingException exceptions (such as CommunicationException). 但是,它不是一个最佳解决方案,因为我们可能会遗漏其他真正的NamingException异常(例如CommunicationException)。

I still would like to find the proper way to tell DirContext.search to ignore referrals, so that NamingEnumeration.hasMore() returns false instead of throwing an exception. 我仍然希望找到告诉DirContext.search忽略引用的正确方法,以便NamingEnumeration.hasMore()返回false而不是抛出异常。 any idea? 任何的想法?

I downloaded the JDK source code at http://download.java.net/openjdk/jdk6/ -> openjdk-6-src-b24-14_nov_2011.tar.gz however this source code does not correspond exactly with the JDK binaries, see how to find the exact sources of a JDK1.6 binary (including com.sun.jndi.*) 我在http://download.java.net/openjdk/jdk6/ - > openjdk-6-src-b24-14_nov_2011.tar.gz下载了JDK源代码但是这个源代码与JDK二进制文件不完全对应,请参阅如何找到JDK1.6二进制文件的确切来源(包括com.sun.jndi。*)

from this JDK source code below it doesn't seem possible to get a "false" instead of an exception when there are referrals. 从下面的JDK源代码中,当有引用时,它似乎不可能得到“假”而不是异常。

./jdk/src/share/classes/com/sun/jndi/ldap/LdapCtx.java
protected void processReturnCode(LdapResult res, Name resolvedName, Object resolvedObj, Name remainName, Hashtable envprops, String fullDN) throws NamingException {
    NamingException e;

    switch (res.status) {
    case LdapClient.LDAP_SUCCESS:

        // handle Search continuation references
        if (res.referrals != null) {
            msg = "Unprocessed Continuation Reference(s)";

            if (handleReferrals == LdapClient.LDAP_REF_IGNORE) {
                e = new PartialResultException(msg);
                break;
            }
        [...]
    }
    [...]
    throw e;
}

but still I am not sure. 但我仍然不确定。

I think it makes sense that we could tell DirContext.search to ignore referrals, so that NamingEnumeration.hasMore() returns false instead of throwing an exception. 我认为我们可以告诉DirContext.search忽略引用是有道理的,因此NamingEnumeration.hasMore()返回false而不是抛出异常。

any idea? 任何的想法?

From what little I understand, there is a way to ignore referrals, which can be especially helpful when dealing with Active Directory LDAP operations. 从一点点我的理解, 忽略引荐,与Active Directory的LDAP操作打交道时,它可以是特别有用的方式。 You'd have to create and inject a new LdapTemplate (constructed with a configured ContextSource ) instead of creating your own InitialDirContext from scratch. 您必须创建并注入一个新的LdapTemplate (使用已配置的ContextSource构造),而不是从头开始创建自己的InitialDirContext I can't tell if that's going to be compatible with your setup. 我不知道这是否与您的设置兼容。 I hope this helps. 我希望这有帮助。

From the Spring LDAP Core LdapTemplate documentation : 从Spring LDAP Core LdapTemplate 文档

AD servers are apparently unable to handle referrals automatically, which causes a PartialResultException to be thrown whenever a referral is encountered in a search. AD服务器显然无法自动处理引用,这会导致在搜索中遇到引用时抛出PartialResultException To avoid this, set the ignorePartialResultException property to true . 要避免这种情况,请将ignorePartialResultException属性设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM