我正在使用 Ldap 从 AD LDS 检索帐户:
Hashtable props = new Hashtable();
props.put(Context.SECURITY_PRINCIPAL, "cn=adminuser,o=myorg,c=uk");
props.put(Context.SECURITY_CREDENTIALS, "password");
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldaps://myldapserver:636");
InitialLdapContext context = new InitialLdapContext(props, null);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(null);
// according to javadoc, null means "return all attributes"
NamingEnumeration<SearchResult> results =
context.search(userBase, "cn=SOMEUSER", controls);
帐户恢复正常。但并非 SOMEUSER 的所有属性都会返回。
具体来说,该msDS-UserPasswordExpired
属性永远不会回来。
但是...如果我在中列出该属性SearchControls
:
controls.setReturningAttributes(new String[] {
"msDS-UserPasswordExpired", "cn", "mail"
});
然后神奇地回来了。
为什么?SearchControl
javadoc在撒谎吗?
我如何告诉它我真的 很想恢复所有属性?
解决方法是列出我想要返回的每个属性。但这很可怕,并且会使添加未来字段非常容易出错。