[英]how to specify search scope and returning attributes together for ldap DirContext.search(…)
[英]java- how to tell LDAP DirContext.search(…).hasMore() to return false instead of throwing a PartialResultException
我设置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 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();
}
调用e.hasMoreElements()而不是e.hasMore()解决了这个问题。 也就是说,当有引用时,它返回false(而不是抛出异常)。
但是,它不是一个最佳解决方案,因为我们可能会遗漏其他真正的NamingException异常(例如CommunicationException)。
我仍然希望找到告诉DirContext.search忽略引用的正确方法,以便NamingEnumeration.hasMore()返回false而不是抛出异常。 任何的想法?
我在http://download.java.net/openjdk/jdk6/ - > openjdk-6-src-b24-14_nov_2011.tar.gz下载了JDK源代码但是这个源代码与JDK二进制文件不完全对应,请参阅如何找到JDK1.6二进制文件的确切来源(包括com.sun.jndi。*)
从下面的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;
}
但我仍然不确定。
我认为我们可以告诉DirContext.search忽略引用是有道理的,因此NamingEnumeration.hasMore()返回false而不是抛出异常。
任何的想法?
从一点点我的理解, 有忽略引荐,与Active Directory的LDAP操作打交道时,它可以是特别有用的方式。 您必须创建并注入一个新的LdapTemplate
(使用已配置的ContextSource
构造),而不是从头开始创建自己的InitialDirContext
。 我不知道这是否与您的设置兼容。 我希望这有帮助。
从Spring LDAP Core LdapTemplate
文档 :
AD服务器显然无法自动处理引用,这会导致在搜索中遇到引用时抛出
PartialResultException
。 要避免这种情况,请将ignorePartialResultException
属性设置为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.