如何使用Java查询LDAP的ROOTDSE



在c#中,只有两行需要实现这一目标:

        DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE", dnsDomainName));
        string configurationNamingContext = rootDSE.Properties["configurationNamingContext"][0].ToString();

如何在Java World?

通过使用Spring LDAP来弄清楚它,LDAP URL无法添加rootdse Postfix:ldap://{域名},然后使用通配符搜索:

    LdapTemplate template = new LdapTemplate(ldapContextSource);
    template.setIgnorePartialResultException(true);
    String returnedAtts[] = { "configurationNamingContext" };
    SearchControls controls = new SearchControls(SearchControls.OBJECT_SCOPE,0,0,returnedAtts,false,false);
    LikeFilter  filter = new LikeFilter ("objectClass", "*");
    List<String> result = template.search("", filter.encode(), controls, new AttributesMapper<String>() {
        public String mapFromAttributes(Attributes attrs)
                throws NamingException {
                return attrs.get("configurationNamingContext").get().toString();
             }
          });

使用Spring LDAP构建器和Java 8 lambda表达式时,您可以删除一些额外的样板:

        List<String> result = ldapTemplate.search(query()
                    .searchScope(SearchScope.OBJECT)
                    .where("objectclass").isPresent(),
            (AttributesMapper<String>) attrs ->
                    attrs.get("configurationNamingContext").get().toString());

您还需要此导入:

import static org.springframework.ldap.query.LdapQueryBuilder.*;

最新更新