此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.8! |
配置
配置 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 和身份验证凭据。
context-source 上的可配置属性如下(必需属性标有 *):
属性 | 违约 | 描述 |
---|---|---|
|
|
创建的 Bean 的 ID。 |
|
使用 LDAP 服务器进行身份验证时使用的用户名(主体)。
这通常是 admin 用户的专有名称(例如 |
|
|
使用 LDAP 服务器进行身份验证时使用的密码(凭证)。如果 require |
|
|
要使用的 LDAP 服务器的 URL。URL 应采用以下格式: |
|
|
|
基本 DN.配置此属性后,提供给 LDAP作和从 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 用户,用于在context-source
元素。
如果username 是 LDAP 用户的可分辨名称 (DN),则它必须是 LDAP 树根目录中用户的完整 DN,无论base LDAP 路径已在context-source 元素。 |
某些 LDAP 服务器设置允许匿名只读访问。如果要使用匿名上下文进行只读作,请将anonymous-read-only
属性设置为true
.
习惯DirContext
身份验证处理
Spring LDAP 中使用的默认身份验证机制是SIMPLE
认证。这意味着委托人(由username
属性)和凭证(由password
) 在Hashtable
发送到DirContext
implementation 构造函数。
在很多情况下,这种处理是不够的。例如,LDAP 服务器通常设置为仅接受安全 TLS 通道上的通信。可能需要使用特定的 LDAP 代理身份验证机制或其他问题。
您可以通过提供DirContextAuthenticationStrategy
implementation reference to the Implementation(对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 池支持来获得巨大的性能优势,如池支持中所述。 |
自定义委托人和凭证管理
虽然用于创建已验证的Context
默认是静态定义的(在context-source
元素配置在ContextSource
),但在几种情况下,这不是所需的行为。一种常见情况是,在为该用户执行 LDAP作时,应使用当前用户的主体和凭证。您可以通过提供对AuthenticationSource
implementation 添加到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
标记AbstractContextSource
.默认值为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
支持 -
提供以 bind © 、 search ® 、 modify (U)、解绑 (D) 和鉴权为中心的简化 API。
LdapClient 尚不支持 ODM。
如果这是你需要的,LdapTemplate 具有此容量。
双LdapClient 和LdapTemplate 如果需要,可以很好地共存于同一个应用程序中。 |
一LdapClient
是使用LdapClient#create
factory 方法,如下所示:
<bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
<constructor-arg ref="contextSource" />
</bean>
此元素引用默认的ContextSource
,其 ID 应为contextSource
(默认的context-source
元素)。
你LdapClient
instance 可以配置为如何处理某些已检查的异常以及任何默认的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
object 类)。在这种情况下,每个组成员属性值都需要是被引用成员的完整 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
财产。