|
这个版本仍在开发中,目前尚未被认为是稳定的。要使用最新稳定版本,请使用 Spring LDAP 4.0.2! |
介绍
本部分提供了一个相对快速的 Spring LDAP 介绍。它包括以下内容:
概述
Spring LDAP 旨在简化 Java 中的 LDAP 编程。该库提供的一些功能包括:
-
JdbcTemplate-style LDAP 编程的模板简化。 -
基于 JPA 或 Hibernate 的注解式对象和目录映射。
-
Spring Data仓库支持,包括对QueryDSL的支持。
-
工具,用于简化构建LDAP查询和区分名称。
-
适当的LDAP连接池配置。
-
客户端侧LDAP补偿事务支持。
传统 Java LDAP 与LdapClient
考虑一个方法,应该搜索存储以查找所有人员并返回他们的名字列表。 通过使用JDBC,我们将创建一个connection,并通过一个statement运行一个query。然后我们将遍历result set,获取所需的column,并将其添加到列表中。
与JNDI连接的LDAP数据库进行操作时,我们会创建一个context,并通过使用search filter执行search。然后,我们会遍历搜索结果的naming enumeration,获取所需的attribute,并将其添加到列表中。
传统的在Java LDAP中实现人名搜索方法的方式如下所示。请注意标记bold的代码 - 这是实际执行与该方法业务目的相关的任务的代码。其余的是 plumbing(基础设施代码)。
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 LDAP 0 和 1 类,我们可以用以下代码获得完全相同的功能:
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();
);
}
}
传统的示例中样板代码的量显著更少。The LdapClient 搜索方法会确保创建一个 DirContext 实例,执行搜索,通过使用给定的 AttributesMapper 将属性映射为字符串,将这些字符串收集到一个内部列表中,最后返回该列表。它还会确保 NamingEnumeration 和 DirContext 正确关闭,并处理可能发生的任何异常。
当然,这作为一个 Spring 框架的子项目,我们使用 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进行了相当重大的现代化改造,但已经采取了极大的谨慎措施,尽可能确保向后兼容。 与Spring LDAP 1.3.x一起工作的代码,在大多数情况下,应该可以在不进行任何修改的情况下使用2.0库编译和运行。
该异常是由一小部分类被移动到新包中,以实现一些重要的重构。被移动的类通常不属于预期的公共API,迁移过程应该是平滑的。每当在升级后找不到Spring LDAP类时,你应该在你的IDE中组织导入。
你应该期望会遇到一些弃用警告,尽管如此,还有许多其他 API 改进。 2.0 版本的推荐做法是远离弃用的类和方法,并迁移到新的、改进的 API 工具。
以下列表简要描述了 Spring LDAP 2.0 中最重要的更改:
-
Java 6 现在是 Spring LDAP 所必需的。从 Spring 2.0 版本开始的版本仍然得到支持。
-
核心API已更新,加入了Java 5+特性,如泛型和可变参数。
因此,整个spring-ldap-tiger模块已弃用,我们鼓励您迁移到使用核心Spring LDAP类。
核心接口的参数化会在现有代码上产生大量编译警告,我们鼓励您采取适当措施去除这些警告。 -
The ODM (对象-目录映射) 功能已移至核心,
LdapOperations和LdapTemplate中有新的方法使用这种自动从和到 ODM 注解类的转换。请参阅 对象-目录映射 (ODM) 以获取更多信息。 -
现在(终于)提供了一个自定义的XML命名空间,以简化Spring LDAP的配置。请参见[配置]以获取更多信息。
-
Spring LDAP 现在提供了对 Spring Data Repository 和 QueryDSL 的支持。请参见 Spring LDAP Repositories 以获取更多信息。
-
Name实例作为属性值现在在DirContextAdapter和ODM中关于区分名称相等性的处理是正确的。 见DirContextAdapter和区分名称作为属性值和ODM和区分名称作为属性值了解更多信息。 -
DistinguishedName及其关联类已弃用,推荐使用标准的 JavaLdapName。 请参阅 动态构建区分名 以了解该库在处理LdapName对象时的用法。 -
添加了流畅的LDAP查询构建支持。这使得在使用Spring LDAP进行LDAP搜索时获得更愉快的编程体验。 查看 构建LDAP查询 和 高级LDAP查询 了解更多关于LDAP查询构建器支持的信息。
-
The old
authenticatemethods inLdapTemplate已被弃用,取而代之的是几个新的authenticate方法,这些方法使用LdapQuery对象,并在认证失败时抛出异常,使用户更容易了解认证尝试失败的原因。 -
示例 samples 已经打磨并更新,以充分利用 2.0 的特性。 相当多的精力投入到了提供一个有用的 LDAP 用户管理应用程序 的示例中。
-
添加
LdapClient.create(LdapTemplate)以简化从LdapClient构造一个LdapTemplate
打包概述
至少需要以下内容才能使用 Spring LDAP:
-
spring-ldap-core: The Spring LDAP 库 -
spring-core: 内部使用的Spring框架实用工具类 -
spring-beans: 用于操作 Java beans 的接口和类 -
slf4j: 一个简单的日志门面,用于内部使用
除必需依赖项外,以下可选依赖项对于某些功能是必需的:
-
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 LDAP 的 XML 配置支持即使在不使用 Spring Data 功能集的情况下也需要该依赖项。 |
快速开始
这些 示例 提供了一些如何使用 Spring LDAP 处理常见用例的有用示例。
支持
如果您有疑问,请在Stack Overflow上提出,并使用spring-ldap标签。项目网页是spring.io/spring-ldap/。
致谢
感谢Structure101提供开源许可证,这在检查项目结构时派上了用场。