该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用 Spring LDAP 4.0.0spring-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;
   }
}

通过使用春季LDAP属性映射器LdapClient我们通过以下代码获得了完全相同的功能: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();
         );
   }
}

模板代码的数量明显少于传统示例。 这LdapClient搜索方法确保DirContext实例被创建,执行搜索,并将属性映射到字符串,使用给定的属性映射器, 在内部列表中收集字符串,最后返回列表。它还确保命名枚举DirContext是适当闭合的,且 处理可能发生的例外情况。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的更新日志。 春季LDAP 2.2的亮点如下:spring-doc.cadn.net.cn

2.1版本的新内容

关于2.1版本的完整细节,请参见2.1.0.RC1的更新日志,2.1.0的重点如下。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

以下列表简要描述了春季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上下文: 如果你的应用是通过Spring Application Context进行布线连接,则需要。Spring上下文增加了应用程序对象通过使用一致API获取资源的能力。如果你打算使用,BaseLdapPathBeanPostProcessor.spring-doc.cadn.net.cn

  • 春季-德克萨斯如果你计划使用客户端补偿交易支持,这是必需的。spring-doc.cadn.net.cn

  • 春季-JDBC如果你计划使用客户端补偿交易支持,这是必需的。spring-doc.cadn.net.cn

  • 公共池:如果你打算使用池化功能,必须这样做。spring-doc.cadn.net.cn

  • 春季批次如果你计划将LDIF解析功能与Spring Batch结合使用,则需要。spring-doc.cadn.net.cn

Spring-data-LDAP传递加法spring-repository.xsdSpring-ldap.xsd使用。 因此,即使 Spring Data 的功能集未被使用,Spring LDAP 的 XML 配置支持仍需依赖该功能。

开始

示例提供了一些关于如何使用 Spring LDAP 进行常见用例的实用示例。spring-doc.cadn.net.cn

支持

如果你有问题,就在网上提问Stack Overflow 与春季-ldap标记. 项目网页已 spring.io/spring-ldap/spring-doc.cadn.net.cn

确认

春季LDAP项目启动时的初步工作由Jayway赞助。 当前项目的维护由Pivotal资助,后者已被VMware收购。spring-doc.cadn.net.cn

感谢 Structure101 提供的开源许可证,帮助我们管理项目结构。spring-doc.cadn.net.cn