此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.8spring-doc.cadn.net.cn

介绍

概述

Spring LDAP 旨在简化 Java 中的 LDAP 编程。该库提供的一些功能包括:spring-doc.cadn.net.cn

传统 Java LDAP 与LdapClient

考虑一种方法,该方法应该搜索所有人员的某个存储,并在列表中返回他们的姓名。 通过使用 JDBC,我们将创建一个连接,并使用一个语句运行一个查询。然后,我们将遍历结果集并检索我们想要的,将其添加到列表中。spring-doc.cadn.net.cn

使用 JNDI 处理 LDAP 数据库,我们将创建一个上下文并使用搜索过滤器执行搜索。然后,我们将遍历生成的命名枚举,检索我们想要的属性,并将其添加到列表中。spring-doc.cadn.net.cn

在 Java LDAP 中实现这种人名搜索方法的传统方法类似于下一个示例。请注意标记为粗体的代码 - 这是 实际执行与方法的业务目的相关的任务。剩下的就是管道了。spring-doc.cadn.net.cn

public class TraditionalPersonRepoImpl implements PersonRepo {
   public List<String> getAllPersonNames() {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=example,dc=com");

      DirContext ctx;
      try {
         ctx = new InitialDirContext(env);
      } catch (NamingException e) {
         throw new RuntimeException(e);
      }

      List<String> list = new LinkedList<String>();
      NamingEnumeration results = null;
      try {
         SearchControls controls = new SearchControls();
         controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
         results = ctx.search("", "(objectclass=person)", controls);

         while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("cn");
            String cn = attr.get().toString();
            list.add(cn);
         }
      } catch (NameNotFoundException e) {
         // The base context was not found.
         // Just clean up and exit.
      } catch (NamingException e) {
         throw new RuntimeException(e);
      } finally {
         if (results != null) {
            try {
               results.close();
            } catch (Exception e) {
               // Never mind this.
            }
         }
         if (ctx != null) {
            try {
               ctx.close();
            } catch (Exception e) {
               // Never mind this.
            }
         }
      }
      return list;
   }
}

通过使用 Spring LDAPAttributesMapperLdapClient类,我们可以通过以下代码获得完全相同的功能:spring-doc.cadn.net.cn

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

public class PersonRepoImpl implements PersonRepo {
   private LdapClient ldapClient;

   public void setLdapClient(LdapClient ldapClient) {
      this.ldapClient = ldapClient;
   }

   public List<String> getAllPersonNames() {
      return ldapClient.search().query(
            query().where("objectclass").is("person")
         ).toObject((Attributes attrs) ->
            attrs.get("cn").get().toString();
         );
   }
}

样板代码的数量明显少于传统示例中的样板代码量。 这LdapClientsearch 方法确保DirContext实例,执行搜索,使用给定的AttributesMapper, 收集内部列表中的字符串,最后返回该列表。它还确保NamingEnumerationDirContext已正确关闭,并且 处理可能发生的任何异常。spring-doc.cadn.net.cn

当然,这是一个 Spring Framework 子项目,我们使用 Spring 来配置我们的应用程序,如下所示:spring-doc.cadn.net.cn

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ldap="http://www.springframework.org/schema/ldap"
       xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/ldap https://www.springframework.org/schema/ldap/spring-ldap.xsd">

   <ldap:context-source
          url="ldap://localhost:389"
          base="dc=example,dc=com"
          username="cn=Manager"
          password="secret" />

   <bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
        <constructor-arg ref="contextSource" />
    </bean>

   <bean id="personRepo" class="com.example.repo.PersonRepoImpl">
      <property name="ldapClient" ref="ldapClient" />
   </bean>
</beans>
要使用自定义 XML 名称空间来配置 Spring LDAP 组件,你需要在 XML 声明中包含对此名称空间的引用,如前面的示例所示。

2.2 中的新增功能

有关 2.2 的完整详细信息,请参阅 2.2.0.RC1 的更改日志。 Spring LDAP 2.2 的亮点如下:spring-doc.cadn.net.cn

2.1 中的新增功能

有关 2.1 的完整详细信息,请参阅 2.1.0.RC12.1.0 的更改日志Spring LDAP 2.1 的亮点如下。spring-doc.cadn.net.cn

2.0 中的新增功能

虽然在 2.0 版中对 Spring LDAP API 进行了相当重大的现代化改造,但已经非常小心地确保尽可能的向后兼容性。 使用 Spring LDAP 1.3.x 的代码应该在使用 2.0 库时编译和运行,而无需进行任何修改,只有少数例外。spring-doc.cadn.net.cn

例外情况是,为了进行一些重要的重构,有少量的类被移动到新的包中。 移动的类通常不是预期的公共 API 的一部分,迁移过程应该很顺利。每当升级后找不到 Spring LDAP 类时,您应该在 IDE 中组织导入。spring-doc.cadn.net.cn

不过,您应该会遇到一些弃用警告,并且还有许多其他 API 改进。 为了尽可能多地利用 2.0 版本,建议放弃已弃用的类和方法,并迁移到新的、改进的 API 实用程序。spring-doc.cadn.net.cn

以下列表简要描述了 Spring LDAP 2.0 中最重要的更改:spring-doc.cadn.net.cn

打包概述

要使用 Spring LDAP,您至少需要以下内容:spring-doc.cadn.net.cn

除了必需的依赖项之外,某些功能还需要以下可选依赖项:spring-doc.cadn.net.cn

  • spring-data-ldap:用于存储库支持等的基本基础设施spring-doc.cadn.net.cn

  • spring-context:如果您的应用程序是使用 Spring Application Context 连接的,则需要。spring-context增加了应用程序对象使用一致的 API 获取资源的能力。如果您打算使用BaseLdapPathBeanPostProcessor.spring-doc.cadn.net.cn

  • spring-tx:如果您计划使用客户端补偿事务支持,则需要。spring-doc.cadn.net.cn

  • spring-jdbc:如果您计划使用客户端补偿事务支持,则需要。spring-doc.cadn.net.cn

  • commons-pool:如果您计划使用池化功能,则需要此属性。spring-doc.cadn.net.cn

  • spring-batch:如果您计划将 LDIF 解析功能与 Spring Batch 一起使用,则需要。spring-doc.cadn.net.cn

spring-data-ldap传递添加spring-repository.xsdspring-ldap.xsd使用。 因此, Spring LDAP 的 XML 配置支持需要依赖项,即使 Spring Data 的功能集未使用也是如此。

开始

这些示例提供了一些有用的示例,说明如何将 Spring LDAP 用于常见用例。spring-doc.cadn.net.cn

确认

启动 Spring LDAP 项目时的最初工作是由 Jayway 赞助的。 该项目的当前维护由 Pivotal 资助,该公司后来被 VMware 收购。spring-doc.cadn.net.cn

感谢 Structure101 提供的开源许可证,该许可证在检查项目结构方面非常方便。spring-doc.cadn.net.cn