此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring LDAP 3.3.3! |
配置
配置 Spring LDAP 的推荐方法是使用自定义 XML 配置命名空间。要使其可用,您需要在 bean 文件中包含 Spring LDAP 命名空间声明,如下所示:
<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、用户名和密码,如下所示:
<ldap:context-source
username="cn=Administrator"
password="secret"
url="ldap://localhost:389" />
前面的示例创建了一个LdapContextSource
使用默认值(请参阅本段后面的表)以及指定的 URL 和身份验证凭据。
上下文源上的可配置属性如下(标有 * 的必需属性):
属性 | 默认值 | 描述 |
---|---|---|
|
|
已创建 Bean 的 ID。 |
|
向 LDAP 服务器进行身份验证时要使用的用户名(主体)。
这通常是管理员用户的可分辨名称(例如, |
|
|
向 LDAP 服务器进行身份验证时要使用的密码(凭据)。如果出现以下情况,则为必填项 |
|
|
要使用的 LDAP 服务器的 URL。URL 应采用以下格式: |
|
|
|
基础 DN。配置此属性后,向 LDAP作提供和接收的所有可分辨名称都相对于指定的 LDAP 路径。 这可以显着简化针对 LDAP 树的工作。但是,在某些情况下,您需要访问基本路径。 有关这方面的更多信息,请参阅获取对基本 LDAP 路径的引用 |
|
|
定义是否使用匿名(未经身份验证)上下文执行只读作。
请注意,将此参数设置为 |
|
|
定义用于处理引荐的策略,如此处所述。有效值为:
|
|
|
指定是否应使用本机 Java LDAP 连接池。考虑改用 Spring LDAP 连接池。有关详细信息,请参阅池化支持。 |
|
一个 |
的 ID |
|
一个 |
的 ID |
|
对 |
DirContext
认证
什么时候DirContext
创建实例是为了在 LDAP 服务器上执行作,这些上下文通常需要进行身份验证。
Spring LDAP 提供了多种用于配置此设置的选项。
本节涉及在ContextSource ,以构造DirContext 供LdapClient 和LdapTemplate .LDAP 通常仅用于用户身份验证,并且ContextSource 也可以用于此目的。使用 Spring LDAP 进行用户身份验证中讨论了该过程。 |
默认情况下,为只读和读写作创建经过身份验证的上下文。您应该指定username
和password
LDAP 用户的 LDAP 用户,用于context-source
元素。
如果username 是 LDAP 用户的可分辨名称 (DN),它需要是 LDAP 树根目录中用户的完整 DN,无论base LDAP 路径已在context-source 元素。 |
某些 LDAP 服务器设置允许匿名只读访问。如果要使用匿名上下文进行只读作,请将anonymous-read-only
属性设置为true
.
习惯DirContext
身份验证处理
Spring LDAP 中使用的默认身份验证机制是SIMPLE
认证。这意味着主体(由username
属性)和凭据(由password
) 在Hashtable
发送到DirContext
实现构造函数。
在很多情况下,这种处理是不够的。例如,LDAP 服务器通常设置为仅接受安全 TLS 通道上的通信。可能需要使用特定的 LDAP 代理身份验证机制或其他问题。
您可以通过提供DirContextAuthenticationStrategy
实现引用context-source
元素。为此,请将authentication-strategy-ref
属性。
TLS的
Spring LDAP 为需要 TLS 安全通道通信的 LDAP 服务器提供了两种不同的配置选项:DefaultTlsDirContextAuthenticationStrategy
和ExternalTlsDirContextAuthenticationStrategy
.
两种实现都在目标连接上协商 TLS 通道,但它们在实际身份验证机制上有所不同。
哪里DefaultTlsDirContextAuthenticationStrategy
在安全通道上应用 SIMPLE 身份验证(通过使用指定的username
和password
)、ExternalTlsDirContextAuthenticationStrategy
使用 EXTERNAL SASL 身份验证,应用使用系统属性进行身份验证配置的客户端证书。
由于不同的 LDAP 服务器实现对 TLS 通道的显式关闭的响应不同(某些服务器要求正常关闭连接,而其他服务器不支持),因此 TLSDirContextAuthenticationStrategy
实现支持使用shutdownTlsGracefully
参数。如果此属性设置为false
(默认值),则不会发生显式 TLS 关闭。如果是true
,Spring LDAP 在关闭目标上下文之前尝试正常关闭 TLS 通道。
使用 TLS 连接时,您需要确保本机 LDAP 池化功能(如使用native-pooling 属性)已关闭。如果出现以下情况,这一点尤为重要shutdownTlsGracefully 设置为false .但是,由于 TLS 通道协商过程非常昂贵,因此您可以通过使用 Spring LDAP 池化支持(如池化支持中所述)来获得巨大的性能优势。 |
自定义主体和凭据管理
而用户名(即用户 DN)和密码用于创建经过身份验证的Context
默认情况下是静态定义的(在context-source
元素配置在ContextSource
),在某些情况下这不是理想的行为。一个常见的方案是,在为当前用户执行 LDAP作时,应使用当前用户的主体和凭据。您可以通过提供对AuthenticationSource
实现到context-source
元素使用authentication-source-ref
元素,而不是显式指定username
和password
.这AuthenticationSource
被ContextSource
对于每次经过身份验证的主体和凭证Context
是要创造的。
如果您使用 Spring Security,则可以通过配置ContextSource
与SpringSecurityAuthenticationSource
随 Spring Security 一起提供。以下示例显示了如何执行此作:
<beans>
...
<ldap:context-source
url="ldap://localhost:389"
authentication-source-ref="springSecurityAuthenticationSource"/>
<bean id="springSecurityAuthenticationSource"
class="org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource" />
...
</beans>
我们没有指定任何username 或password 为。我们context-source 当使用AuthenticationSource .仅当使用默认行为时,才需要这些属性。 |
使用SpringSecurityAuthenticationSource ,您需要使用 Spring Security 的LdapAuthenticationProvider 根据 LDAP 对用户进行身份验证。 |
本机 Java LDAP 池
内部 Java LDAP 提供程序提供了一些非常基本的池化功能。您可以使用pooled
标记 onAbstractContextSource
.默认值为false
(从 1.3 版开始)——也就是说,本机 Java LDAP 池被关闭。LDAP 连接池的配置是通过使用System
属性,因此您需要在 Spring Context 配置之外手动处理此问题。您可以在此处找到本机池配置的详细信息。
内置的 LDAP 连接池存在几个严重缺陷,这就是为什么 Spring LDAP 提供了一种更复杂的 LDAP 连接池方法,如池化支持中所述。如果您需要池化功能,这是推荐的方法。 |
无论池配置如何,都会ContextSource#getContext(String principal, String credentials) 方法始终显式不使用本机 Java LDAP 池,以便重置密码尽快生效。 |
LdapClient
配置
LdapClient
是用于调用 LDAP 后端的新接口。它改进了LdapTemplate
通过以下方式:
-
提供内置
Stream
支持 -
提供以绑定©、搜索®、修改 (U)、取消绑定 (D) 和身份验证为中心的简化 API。
LdapClient 尚不支持 ODM。
如果这是您需要的东西,LdapTemplate 有这个能力。
双LdapClient 和LdapTemplate 如果需要,可以在同一应用程序中很好地共存。 |
一LdapClient
通过使用LdapClient#create
工厂方法如下:
<bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
<constructor-arg ref="contextSource" />
</bean>
此元素引用默认的ContextSource
,其 ID 应为contextSource
(默认值为context-source
元素)。
你LdapClient
实例可以配置为如何处理某些已检查的异常以及任何默认值SearchControls
应该用于查询。
LdapTemplate
配置
这LdapTemplate
通过使用<ldap:ldap-template>
元素。最简单的ldap-template
declaration 本身是元素:
<ldap:ldap-template />
该元素本身会创建一个LdapTemplate
实例,引用默认ContextSource
,其 ID 应为contextSource
(默认值为context-source
元素)。
下表描述了ldap-template
:
属性 | 默认值 | 描述 |
---|---|---|
|
|
已创建 Bean 的 ID。 |
|
|
的 ID |
|
|
搜索的默认计数限制。0 表示没有限制。 |
|
|
搜索的默认时间限制,以毫秒为单位。0 表示没有限制。 |
|
|
搜索的默认搜索范围。有效值为:
|
|
|
指定 |
|
|
指定是否 |
|
的 ID |
获取对基本 LDAP 路径的引用
如前所述,您可以为ContextSource
,指定 LDAP 树中所有作都相对的根。这意味着您只在整个系统中使用相对可分辨的名称,这通常相当方便。但是,在某些情况下,您可能需要访问基本路径,以便能够相对于 LDAP 树的实际根构建完整的 DN。一个示例是使用 LDAP 组(例如,groupOfNames
对象类别)。在这种情况下,每个组成员属性值都需要是引用成员的完整 DN。
出于这个原因,Spring LDAP 具有一种机制,通过该机制,可以在启动时为任何 Spring 控制的 bean 提供基本路径。
要通知 Bean 基本路径,需要满足两件事。首先,想要基本路径引用的 bean 需要实现BaseLdapNameAware
接口。 其次,您需要定义一个BaseLdapPathBeanPostProcessor
在应用程序上下文中。以下示例演示如何实现BaseLdapNameAware
:
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
:
<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
财产。