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

配置

配置 Spring LDAP 的推荐方法是使用自定义 XML 配置命名空间。要使其可用,您需要在 bean 文件中包含 Spring LDAP 命名空间声明,如下所示:spring-doc.cadn.net.cn

<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">

ContextSource配置

ContextSource通过使用<ldap:context-source>标记。 最简单的context-source声明要求您指定服务器 URL、用户名和密码,如下所示:spring-doc.cadn.net.cn

示例 1.最简单的上下文源声明
<ldap:context-source
    username="cn=Administrator"
    password="secret"
    url="ldap://localhost:389" />

前面的示例创建了一个LdapContextSource使用默认值(请参阅本段后面的表)以及指定的 URL 和身份验证凭据。 上下文源上的可配置属性如下(标有 * 的必需属性):spring-doc.cadn.net.cn

表 1.ContextSource 配置属性
属性 默认值 描述

idspring-doc.cadn.net.cn

contextSourcespring-doc.cadn.net.cn

已创建 Bean 的 ID。spring-doc.cadn.net.cn

usernamespring-doc.cadn.net.cn

向 LDAP 服务器进行身份验证时要使用的用户名(主体)。 这通常是管理员用户的可分辨名称(例如,cn=Administrator),但可能因服务器和身份验证方法而异。 如果出现以下情况,则为必填项authentication-source-ref未显式配置。spring-doc.cadn.net.cn

passwordspring-doc.cadn.net.cn

向 LDAP 服务器进行身份验证时要使用的密码(凭据)。如果出现以下情况,则为必填项authentication-source-ref未显式配置。spring-doc.cadn.net.cn

url *spring-doc.cadn.net.cn

要使用的 LDAP 服务器的 URL。URL 应采用以下格式:ldap://myserver.example.com:389. 对于 SSL 访问,请使用ldaps协议和适当的端口 - 例如ldaps://myserver.example.com:636. 如果需要故障转移功能,可以指定多个 URL,用逗号 (,).spring-doc.cadn.net.cn

basespring-doc.cadn.net.cn

LdapUtils.emptyLdapName()spring-doc.cadn.net.cn

基础 DN。配置此属性后,向 LDAP作提供和接收的所有可分辨名称都相对于指定的 LDAP 路径。 这可以显着简化针对 LDAP 树的工作。但是,在某些情况下,您需要访问基本路径。 有关这方面的更多信息,请参阅获取对基本 LDAP 路径的引用spring-doc.cadn.net.cn

anonymous-read-onlyspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

定义是否使用匿名(未经身份验证)上下文执行只读作。 请注意,将此参数设置为true与补偿事务一起不支持并被拒绝。spring-doc.cadn.net.cn

referralspring-doc.cadn.net.cn

nullspring-doc.cadn.net.cn

定义用于处理引荐的策略,如此处所述。有效值为:spring-doc.cadn.net.cn

native-poolingspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

指定是否应使用本机 Java LDAP 连接池。考虑改用 Spring LDAP 连接池。有关详细信息,请参阅池化支持spring-doc.cadn.net.cn

authentication-source-refspring-doc.cadn.net.cn

一个SimpleAuthenticationSource实例。spring-doc.cadn.net.cn

的 IDAuthenticationSource实例(请参阅自定义主体和凭证管理)。spring-doc.cadn.net.cn

authentication-strategy-refspring-doc.cadn.net.cn

一个SimpleDirContextAuthenticationStrategy实例。spring-doc.cadn.net.cn

的 IDDirContextAuthenticationStrategy要使用的实例(请参阅习惯DirContext身份验证处理).spring-doc.cadn.net.cn

base-env-props-refspring-doc.cadn.net.cn

Map的自定义环境属性应随发送到DirContext在施工上。spring-doc.cadn.net.cn

DirContext认证

什么时候DirContext创建实例是为了在 LDAP 服务器上执行作,这些上下文通常需要进行身份验证。 Spring LDAP 提供了多种用于配置此设置的选项。spring-doc.cadn.net.cn

本节涉及在ContextSource,以构造DirContextLdapClientLdapTemplate.LDAP 通常仅用于用户身份验证,并且ContextSource也可以用于此目的。使用 Spring LDAP 进行用户身份验证中讨论了该过程。

默认情况下,为只读和读写作创建经过身份验证的上下文。您应该指定usernamepasswordLDAP 用户的 LDAP 用户,用于context-source元素。spring-doc.cadn.net.cn

如果username是 LDAP 用户的可分辨名称 (DN),它需要是 LDAP 树根目录中用户的完整 DN,无论baseLDAP 路径已在context-source元素。

某些 LDAP 服务器设置允许匿名只读访问。如果要使用匿名上下文进行只读作,请将anonymous-read-only属性设置为true.spring-doc.cadn.net.cn

习惯DirContext身份验证处理

Spring LDAP 中使用的默认身份验证机制是SIMPLE认证。这意味着主体(由username属性)和凭据(由password) 在Hashtable发送到DirContext实现构造函数。spring-doc.cadn.net.cn

在很多情况下,这种处理是不够的。例如,LDAP 服务器通常设置为仅接受安全 TLS 通道上的通信。可能需要使用特定的 LDAP 代理身份验证机制或其他问题。spring-doc.cadn.net.cn

您可以通过提供DirContextAuthenticationStrategy实现引用context-source元素。为此,请将authentication-strategy-ref属性。spring-doc.cadn.net.cn

TLS的

Spring LDAP 为需要 TLS 安全通道通信的 LDAP 服务器提供了两种不同的配置选项:DefaultTlsDirContextAuthenticationStrategyExternalTlsDirContextAuthenticationStrategy. 两种实现都在目标连接上协商 TLS 通道,但它们在实际身份验证机制上有所不同。 哪里DefaultTlsDirContextAuthenticationStrategy在安全通道上应用 SIMPLE 身份验证(通过使用指定的usernamepassword)、ExternalTlsDirContextAuthenticationStrategy使用 EXTERNAL SASL 身份验证,应用使用系统属性进行身份验证配置的客户端证书。spring-doc.cadn.net.cn

由于不同的 LDAP 服务器实现对 TLS 通道的显式关闭的响应不同(某些服务器要求正常关闭连接,而其他服务器不支持),因此 TLSDirContextAuthenticationStrategy实现支持使用shutdownTlsGracefully参数。如果此属性设置为false(默认值),则不会发生显式 TLS 关闭。如果是true,Spring LDAP 在关闭目标上下文之前尝试正常关闭 TLS 通道。spring-doc.cadn.net.cn

使用 TLS 连接时,您需要确保本机 LDAP 池化功能(如使用native-pooling属性)已关闭。如果出现以下情况,这一点尤为重要shutdownTlsGracefully设置为false.但是,由于 TLS 通道协商过程非常昂贵,因此您可以通过使用 Spring LDAP 池化支持(如池化支持中所述)来获得巨大的性能优势。

自定义主体和凭据管理

而用户名(即用户 DN)和密码用于创建经过身份验证的Context默认情况下是静态定义的(在context-source元素配置在ContextSource),在某些情况下这不是理想的行为。一个常见的方案是,在为当前用户执行 LDAP作时,应使用当前用户的主体和凭据。您可以通过提供对AuthenticationSource实现到context-source元素使用authentication-source-ref元素,而不是显式指定usernamepassword.这AuthenticationSourceContextSource对于每次经过身份验证的主体和凭证Context是要创造的。spring-doc.cadn.net.cn

如果您使用 Spring Security,则可以通过配置ContextSourceSpringSecurityAuthenticationSource随 Spring Security 一起提供。以下示例显示了如何执行此作:spring-doc.cadn.net.cn

示例 2.使用 SpringSecurityAuthenticationSource
<beans>
...
    <ldap:context-source
        url="ldap://localhost:389"
        authentication-source-ref="springSecurityAuthenticationSource"/>

    <bean id="springSecurityAuthenticationSource"
        class="org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource" />
...
</beans>
我们没有指定任何usernamepassword为。我们context-source当使用AuthenticationSource.仅当使用默认行为时,才需要这些属性。
使用SpringSecurityAuthenticationSource,您需要使用 Spring Security 的LdapAuthenticationProvider根据 LDAP 对用户进行身份验证。

本机 Java LDAP 池

内部 Java LDAP 提供程序提供了一些非常基本的池化功能。您可以使用pooled标记 onAbstractContextSource.默认值为false(从 1.3 版开始)——也就是说,本机 Java LDAP 池被关闭。LDAP 连接池的配置是通过使用System属性,因此您需要在 Spring Context 配置之外手动处理此问题。您可以在此处找到本机池配置的详细信息。spring-doc.cadn.net.cn

内置的 LDAP 连接池存在几个严重的缺陷,这就是为什么 Spring LDAP 提供了一种更复杂的 LDAP 连接池方法,如池支持中所述。如果需要池化功能,建议采用此方法。
无论池配置如何,都会ContextSource#getContext(String principal, String credentials)方法始终显式不使用本机 Java LDAP 池,以便重置密码尽快生效。

高深ContextSource配置

本节介绍配置ContextSource.spring-doc.cadn.net.cn

习惯DirContext环境属性

在某些情况下,除了可直接配置的属性之外,您可能还希望指定其他环境设置属性context-source. 您应该在Map并在base-env-props-ref属性。spring-doc.cadn.net.cn

LdapClient配置

LdapClient是用于调用 LDAP 后端的新接口。它改进了LdapTemplate通过以下方式:spring-doc.cadn.net.cn

LdapClient尚不支持 ODM。如果这是您需要的,LdapTemplate有这个能力。 双LdapClientLdapTemplate如果需要,可以在同一应用程序中很好地共存。

LdapClient通过使用LdapClient#create工厂方法如下:spring-doc.cadn.net.cn

示例 3.最简单的 LdapClient 声明
<bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
   <constructor-arg ref="contextSource" />
</bean>

此元素引用默认的ContextSource,其 ID 应为contextSource(默认值为context-source元素)。spring-doc.cadn.net.cn

LdapClient实例可以配置为如何处理某些已检查的异常以及任何默认值SearchControls应该用于查询。spring-doc.cadn.net.cn

LdapTemplate配置

LdapTemplate通过使用<ldap:ldap-template>元素。最简单的ldap-templatedeclaration 本身是元素:spring-doc.cadn.net.cn

示例 4.最简单的 ldap 模板声明
<ldap:ldap-template />

该元素本身会创建一个LdapTemplate实例,引用默认ContextSource,其 ID 应为contextSource(默认值为context-source元素)。spring-doc.cadn.net.cn

下表描述了ldap-template:spring-doc.cadn.net.cn

表 2.LdapTemplate 配置属性
属性 默认值 描述

idspring-doc.cadn.net.cn

ldapTemplatespring-doc.cadn.net.cn

已创建 Bean 的 ID。spring-doc.cadn.net.cn

context-source-refspring-doc.cadn.net.cn

contextSourcespring-doc.cadn.net.cn

的 IDContextSource实例。spring-doc.cadn.net.cn

count-limitspring-doc.cadn.net.cn

0spring-doc.cadn.net.cn

搜索的默认计数限制。0 表示没有限制。spring-doc.cadn.net.cn

time-limitspring-doc.cadn.net.cn

0spring-doc.cadn.net.cn

搜索的默认时间限制,以毫秒为单位。0 表示没有限制。spring-doc.cadn.net.cn

search-scopespring-doc.cadn.net.cn

SUBTREEspring-doc.cadn.net.cn

搜索的默认搜索范围。有效值为:spring-doc.cadn.net.cn

ignore-name-not-foundspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

指定NameNotFoundException在搜索中应忽略。将此属性设置为true使由无效搜索库引起的错误被静默吞下。spring-doc.cadn.net.cn

ignore-partial-resultspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

指定是否PartialResultException在搜索中应忽略。某些 LDAP 服务器在引荐方面存在问题。这些通常应该自动遵循。但是,如果这不起作用,它就会表现出来PartialResultException.将此属性设置为true提供了此问题的解决方法。spring-doc.cadn.net.cn

odm-refspring-doc.cadn.net.cn

的 IDObjectDirectoryMapper实例。默认值是默认配置的DefaultObjectDirectoryMapper.spring-doc.cadn.net.cn

获取对基本 LDAP 路径的引用

如前所述,您可以为ContextSource,指定 LDAP 树中所有作都相对的根。这意味着您只在整个系统中使用相对可分辨的名称,这通常相当方便。但是,在某些情况下,您可能需要访问基本路径,以便能够相对于 LDAP 树的实际根构建完整的 DN。一个示例是使用 LDAP 组(例如,groupOfNames对象类别)。在这种情况下,每个组成员属性值都需要是引用成员的完整 DN。spring-doc.cadn.net.cn

出于这个原因,Spring LDAP 具有一种机制,通过该机制,可以在启动时为任何 Spring 控制的 bean 提供基本路径。 要通知 Bean 基本路径,需要满足两件事。首先,想要基本路径引用的 bean 需要实现BaseLdapNameAware接口。 其次,您需要定义一个BaseLdapPathBeanPostProcessor在应用程序上下文中。 以下示例演示如何实现BaseLdapNameAware:spring-doc.cadn.net.cn

示例 5.实施BaseLdapNameAware
public class PersonService implements PersonService, BaseLdapNameAware {
   ...
   private LdapName basePath;

   public void setBaseLdapPath(LdapName basePath) {
      this.basePath = basePath;
   }
   ...
   private LdapName getFullPersonDn(Person person) {
      return LdapNameBuilder.newInstance(basePath)
          .add(person.getDn())
          .build();
   }
   ...
}

以下示例显示如何定义BaseLdapPathBeanPostProcessor:spring-doc.cadn.net.cn

示例 6.在 ApplicationContext 中指定 BaseLdapPathBeanPostProcessor
<beans>
   ...
   <ldap:context-source
          username="cn=Administrator"
          password="secret"
          url="ldap://localhost:389"
          base="dc=261consulting,dc=com" />
   ...
   <bean class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" />
</beans>

默认行为BaseLdapPathBeanPostProcessor是使用单个定义的基本路径BaseLdapPathSource (AbstractContextSource) 在ApplicationContext.如果多个BaseLdapPathSource定义,您需要通过设置baseLdapPathSourceName财产。spring-doc.cadn.net.cn