|
对于最新稳定版本,请使用 Spring LDAP 4.0.0! |
介绍
本节为春季LDAP提供了相对简要的介绍。内容包括:
概述
Spring LDAP 旨在简化 Java 中的 LDAP 编程。图书馆提供的一些功能包括:
-
Jdbc模板-风格模板对LDAP编程的简化。 -
采用JPA或休眠式的基于标注的对象和目录映射。
-
支持 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;
}
}
通过使用春季LDAP属性映射器和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搜索方法确保DirContext实例被创建,执行搜索,并将属性映射到字符串,使用给定的属性映射器,
在内部列表中收集字符串,最后返回列表。它还确保命名枚举和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 进行了相当大的现代化改造,但我们非常注重尽可能地实现向后兼容性。 在 Spring LDAP 1.3.x 上运行的代码,除了少数例外,应该能在使用 2.0 库时编译并运行,无需修改。
例外是少数类被迁移到新包中,以便实现几个重要的重构。 迁移的类通常不属于预期的公共 API,迁移过程应该很顺利。升级后找不到 Spring LDAP 类时,你应该在 IDE 中整理导入。
不过你应该预期会遇到一些弃用警告,同时还有很多其他API改进。 为了尽可能充分利用2.0版本,建议摒弃弃用的类和方法,迁移到新的、改进的API工具。
以下列表简要描述了春季LDAP 2.0中最重要的变化:
-
Spring LDAP 现在要求使用 Java 6。2.0及以上的春季版本仍然被支持。
-
中央API已更新,加入了Java 5+功能,如generics和varargs。 因此,整个
春-LDAP-虎该模块已被弃用,我们鼓励你迁移到使用核心的Spring LDAP类。 核心接口的参数化会导致现有代码出现大量编译警告,我们鼓励您采取适当措施消除这些警告。 -
ODM(对象-目录映射)功能已移至核心,并且新增了
Ldap运营和LdapTemplate这些工具使用这种自动转换到ODM注释类。更多信息请参见对象-目录映射(ODM)。 -
现在(终于)提供了一个自定义的 XML 命名空间,以简化 Spring LDAP 的配置。更多信息请参见[configuration]。
-
Spring LDAP 现已支持 Spring Data Repository 和 QueryDSL。更多信息请参见春季LDAP仓库。
-
名称作为属性值的实例现在在区分名称相等方面得到了正确处理DirContextAdapter以及立体机动。 看DirContextAdapter以及作为属性价值的杰出名称以及ODM和杰出名称作为属性值以获取更多信息。 -
杰出名号相关类已被弃用,取而代之的是标准 JavaLdapName(LdapName). 关于该库如何帮助处理LdapName(LdapName)对象。 -
新增了流利的 LDAP 查询构建支持。这使得在春季 LDAP 中使用 LDAP 搜索时的编程体验更加愉快。 有关 LDAP 查询构建器支持的更多信息,请参见 Building LDAP Queries 和 Advanced LDAP Queries。
-
旧的
证实方法LdapTemplate已被弃用,取而代之的是几个新的证实适用于LdapQuery对象并对认证失败抛出异常,使用户更容易查明导致认证失败的原因。 -
采样内容经过打磨和更新,以利用2.0版本的相关功能。 在提供一个有用的LDAP用户管理应用示例方面,已经投入了相当多的精力。
包装概述
至少,使用Spring LDAP需要以下条件:
-
Spring-LDAP-核心:春季LDAP库 -
Spring芯:框架内部使用的杂项效用类 -
春豆:作 Java 豆子的接口和类 -
SLF4J:一个简单的Logging立面,内部使用。
除了必要的依赖关系外,以下可选依赖关系是某些功能所必需的:
-
Spring-data-LDAP:基础基础设施支持仓库等 -
Spring上下文: 如果你的应用是通过Spring Application Context进行布线连接,则需要。Spring上下文增加了应用程序对象通过使用一致API获取资源的能力。如果你打算使用,BaseLdapPathBeanPostProcessor. -
春季-德克萨斯如果你计划使用客户端补偿交易支持,这是必需的。 -
春季-JDBC如果你计划使用客户端补偿交易支持,这是必需的。 -
公共池:如果你打算使用池化功能,必须这样做。 -
春季批次如果你计划将LDIF解析功能与Spring Batch结合使用,则需要。
Spring-data-LDAP传递加法spring-repository.xsd哪Spring-ldap.xsd使用。
因此,即使 Spring Data 的功能集未被使用,Spring LDAP 的 XML 配置支持仍需依赖该功能。 |
开始
示例提供了一些关于如何使用 Spring LDAP 进行常见用例的实用示例。
支持
如果你有问题,就在网上提问Stack Overflow 与春季-ldap标记.
项目网页已 spring.io/spring-ldap/。
确认
感谢 Structure101 提供的开源许可证,帮助我们管理项目结构。