此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.8spring-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 和身份验证凭据。 context-source 上的可配置属性如下(必需属性标有 *):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 服务器进行身份验证时使用的用户名(主体)。 这通常是 admin 用户的专有名称(例如cn=Administrator),但可能因服务器和身份验证方法而异。 如果 requireauthentication-source-ref未显式配置。spring-doc.cadn.net.cn

passwordspring-doc.cadn.net.cn

使用 LDAP 服务器进行身份验证时使用的密码(凭证)。如果 requireauthentication-source-ref未显式配置。spring-doc.cadn.net.cn

url *spring-doc.cadn.net.cn

要使用的 LDAP 服务器的 URL。URL 应采用以下格式:ldap://myserver.example.com:389. 对于 SSL 访问,请使用ldapsprotocol 和相应的端口——例如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 树的过程。但是,在几种情况下,您需要访问基本路径。 有关更多信息,请参阅获取对基本 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,以构造DirContext实例供使用LdapClientLdapTemplate.LDAP 通常用于用户身份验证的唯一目的,ContextSource也可以用于此目的。该过程在使用 Spring LDAP 进行用户身份验证中进行了讨论。

默认情况下,将为只读和读写作创建经过身份验证的上下文。您应该指定usernamepassword的 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发送到DirContextimplementation 构造函数。spring-doc.cadn.net.cn

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

您可以通过提供DirContextAuthenticationStrategyimplementation reference to the Implementation(对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 池支持来获得巨大的性能优势,如池支持中所述。

自定义委托人和凭证管理

虽然用于创建已验证的Context默认是静态定义的(在context-source元素配置在ContextSource),但在几种情况下,这不是所需的行为。一种常见情况是,在为该用户执行 LDAP作时,应使用当前用户的主体和凭证。您可以通过提供对AuthenticationSourceimplementation 添加到context-source元素。authentication-source-ref元素,而不是显式指定usernamepassword.这AuthenticationSourceContextSource对于委托人和凭据,每次执行经过身份验证的Context将被创建。spring-doc.cadn.net.cn

如果使用 Spring Security,则可以确保始终使用当前登录用户的主体和凭据,方法是配置您的ContextSource使用SpringSecurityAuthenticationSource随 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标记AbstractContextSource.默认值为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#createfactory 方法,如下所示: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

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

LdapTemplate配置

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

示例 4.最简单的 ldap-template 声明
<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 组(例如,groupOfNamesobject 类)。在这种情况下,每个组成员属性值都需要是被引用成员的完整 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