这个版本仍在开发中,目前尚未被认为是稳定的。要使用最新稳定版本,请使用 Spring LDAP 4.0.2spring-doc.cadn.net.cn

介绍

概述

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

传统 Java LDAP 与LdapClient

考虑一个方法,应该搜索存储以查找所有人员并返回他们的名字列表。 通过使用JDBC,我们将创建一个connection,并通过一个statement运行一个query。然后我们将遍历result set,获取所需的column,并将其添加到列表中。spring-doc.cadn.net.cn

与JNDI连接的LDAP数据库进行操作时,我们会创建一个context,并通过使用search filter执行search。然后,我们会遍历搜索结果的naming enumeration,获取所需的attribute,并将其添加到列表中。spring-doc.cadn.net.cn

传统的在Java LDAP中实现人名搜索方法的方式如下所示。请注意标记bold的代码 - 这是实际执行与该方法业务目的相关的任务的代码。其余的是 plumbing(基础设施代码)。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 LDAP 0 和 1 类,我们可以用以下代码获得完全相同的功能: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();
         );
   }
}

传统的示例中样板代码的量显著更少。The LdapClient 搜索方法会确保创建一个 DirContext 实例,执行搜索,通过使用给定的 AttributesMapper 将属性映射为字符串,将这些字符串收集到一个内部列表中,最后返回该列表。它还会确保 NamingEnumerationDirContext 正确关闭,并处理可能发生的任何异常。spring-doc.cadn.net.cn

当然,这作为一个 Spring 框架的子项目,我们使用 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.RC1 以及 2.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.xsd,而 spring-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