我将 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)
我可以告诉 DirContext.search 忽略引用,以便 NamingEnumeration.hasMore() 返回 false 而不是抛出异常吗?
这是剪断:
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());
}
注意:如果我启用推荐,我会在调用 NamingEnumeration.hasMore() 时收到 LdapReferralException 异常。