The ContextSource
基数的目的是指定基数all上的操作ContextSource
,通常设置为域控制器 DN。
您可以使用 ODM,无需指定基础@Entry
(或使用树中较高位置的基本 DN),但在这种情况下,您通常会使用@DnAttribute
注释,以便框架自动为您构建 DN(主要在将条目保留回 LDAP 时需要)。
如果我们假设您的用户具有以下结构:
dc=example,dc=com,ou=USERS
dc=example,dc=com,ou=TESTERS
现在,如果您指定基数dc=example,dc=com
在 ContextSource 上,您可以让 ODM 自动处理此问题,如下简要描述:
@Entry(objectclasses={"person"})
public class Person {
@Id
private Name dn;
@DnAttribute(name="ou", index=0)
@Transient // Indicates that this is not an attribute on the entry
private String userType;
@Attribute(name="cn")
private String name;
// More attributes here
}
上面的代码将处理 LDAP 条目与 Person 类之间的自动映射。现在,如果您想找到所有人,请执行以下操作:
List<Person> allPersons = ldapTemplate.findAll(Person.class);
如果你想找到所有测试人员,你会这样做:
List<Person> testers = ldapTemplate.find(
query().base("ou=TESTERS"),
Person.class);