|
这个版本仍在开发中,目前尚未被认为是稳定的。要使用最新稳定版本,请使用 Spring LDAP 4.0.2! |
介绍
概述
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声明中包含对该命名空间的引用,就像前面示例中所示。 |
打包概述
至少需要以下内容才能使用 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提供开源许可证,这在检查项目结构时派上了用场。