介绍
本节相对快速地介绍了 Spring LDAP。它包括以下内容:
概述
Spring LDAP 旨在简化 Java 中的 LDAP 编程。该库提供的一些功能是:
-
JdbcTemplate
-style 模板简化为 LDAP 编程。 -
JPA 或 Hibernate 风格的基于注释的对象和目录映射。
-
Spring Data 存储库支持,包括对 QueryDSL 的支持。
-
用于简化构建 LDAP 查询和可分辨名称的实用程序。
-
正确的 LDAP 连接池。
-
客户端 LDAP 补偿事务支持。
传统 Java LDAP 与LdapClient
考虑一种方法,该方法应该搜索所有人员的某个存储并在列表中返回他们的名字。通过使用 JDBC,我们将创建一个连接并使用语句运行查询。然后,我们将循环遍历结果集并检索我们想要的列,将其添加到列表中。
使用 JNDI 对 LDAP 数据库进行作,我们将创建一个上下文并使用搜索过滤器执行搜索。然后,我们将循环遍历生成的命名枚举,检索我们想要的属性,并将其添加到列表中。
在 Java LDAP 中实现此人名搜索方法的传统方法类似于下一个示例。请注意标记为粗体的代码 - 这是实际执行与该方法的业务目的相关的任务。剩下的就是管道。
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;
}
}
通过使用 Spring LDAPAttributesMapper
和LdapClient
类,我们使用以下代码获得完全相同的功能:
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
search 方法确保DirContext
创建实例,执行搜索,使用给定的AttributesMapper
, 收集内部列表中的字符串,最后返回该列表。它还确保NamingEnumeration
和DirContext
正确关闭,并且处理可能发生的任何异常。
当然,这是一个 Spring Framework 子项目,我们使用 Spring 来配置我们的应用程序,如下所示:
<?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.0 中的新增功能
虽然在 2.0 版中对 Spring LDAP API 进行了相当大的现代化改造,但已经非常小心地确保尽可能向后兼容。 当您使用 2.0 库时,除了少数例外,与 Spring LDAP 1.3.x 一起使用的代码应该编译和运行,而无需进行任何修改。
例外是少数类已移至新包中,以便进行一些重要的重构。 移动的类通常不是预期的公共 API 的一部分,迁移过程应该是顺利的。每当升级后找不到 Spring LDAP 类时,您应该在 IDE 中组织导入。
不过,您应该会遇到一些弃用警告,并且还有很多其他 API 改进。 为了尽可能多地利用 2.0 版本,建议放弃已弃用的类和方法,并迁移到新的、改进的 API 实用程序。
以下列表简要描述了 Spring LDAP 2.0 中最重要的更改:
-
Spring LDAP 现在需要 Java 6。仍然支持 2.0 及更高版本开始的 Spring 版本。
-
中央 API 已更新为 Java 5+ 功能,例如泛型和变量。 因此,整个
spring-ldap-tiger
模块已被弃用,我们鼓励您迁移到使用核心 Spring LDAP 类。 核心接口的参数化会导致现有代码出现大量编译警告,我们鼓励您采取适当的措施来消除这些警告。 -
ODM(对象目录映射)功能已移至核心,并且
LdapOperations
和LdapTemplate
使用这种与 ODM 注释类之间的自动转换。有关更多信息,请参阅对象目录映射 (ODM)。 -
现在(最终)提供了一个自定义 XML 命名空间来简化 Spring LDAP 的配置。有关详细信息,请参阅 [配置]。
-
Spring LDAP 现在提供对 Spring Data Repository 和 QueryDSL 的支持。有关更多信息,请参阅 Spring LDAP 存储库。
-
Name
现在,在DirContextAdapter
和 ODM。 看DirContextAdapter
和可分辨名称作为属性值和 ODM 和可分辨名称作为属性值了解更多信息。 -
DistinguishedName
和相关类已被弃用,取而代之的是标准 JavaLdapName
. 有关库在使用LdapName
对象。 -
添加了 Fluent LDAP 查询构建支持。这在 Spring LDAP 中使用 LDAP 搜索时提供了更愉快的编程体验。 有关 LDAP 查询生成器支持的更多信息,请参阅构建 LDAP 查询和高级 LDAP 查询。
-
旧的
authenticate
方法LdapTemplate
已被弃用,取而代之的是几个新的authenticate
使用LdapQuery
对象,并在身份验证失败时抛出异常,使用户更容易找出导致身份验证尝试失败的原因。 -
这些示例已经过完善和更新,以利用 2.0 中的功能。为了提供 LDAP 用户管理应用程序的有用示例,已经投入了相当多的精力。
-
添加
LdapClient.create(LdapTemplate)
以简化构造LdapClient
从LdapTemplate
包装概述
至少,要使用 Spring LDAP,您需要以下内容:
-
spring-ldap-core
:Spring LDAP 库 -
spring-core
:框架内部使用的杂项实用程序类 -
spring-beans
:用于作 Java Bean 的接口和类 -
slf4j
:一个简单的Logging立面,内部使用
除了必需的依赖项外,某些功能还需要以下可选依赖项:
-
spring-data-ldap
:用于存储库支持等的基本基础结构 -
spring-context
:如果您的应用程序是使用 Spring Application Context 连接的,则需要。spring-context
增加了应用程序对象使用一致的 API 获取资源的能力。如果您打算使用BaseLdapPathBeanPostProcessor
. -
spring-tx
:如果您计划使用客户端补偿事务支持,则需要。 -
spring-jdbc
:如果您计划使用客户端补偿事务支持,则需要。 -
commons-pool
:如果计划使用池化功能,则需要。 -
spring-batch
:如果您计划将 LDIF 解析功能与 Spring Batch 一起使用,则需要。
spring-data-ldap 传递添加spring-repository.xsd 哪spring-ldap.xsd 使用。
因此,即使未使用 Spring Data 的功能集,Spring LDAP 的 XML 配置支持也需要依赖项。 |
开始
这些示例提供了一些有用的示例,说明如何在常见用例中使用 Spring LDAP。
支持
如果您有任何问题,请在Stack Overflow 与spring-ldap
标记.
项目网页 spring.io/spring-ldap/。
确认
感谢 Structure101 提供的开源许可证,该许可证在控制项目结构方面派上用场。