此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.8spring-doc.cadn.net.cn

对象目录映射 (ODM)

对象关系映射框架(如 Hibernate 和 JPA)使开发人员能够使用注释将关系数据库表映射到 Java 对象。 Spring LDAP 项目通过LdapOperations:spring-doc.cadn.net.cn

附注

使用对象映射方法管理的实体类需要使用来自org.springframework.ldap.odm.annotations包。可用的注释包括:spring-doc.cadn.net.cn

@Entry@Id需要在托管类上声明注释。@Entry用于指定实体映射到哪些对象类,以及(可选)类所表示的 LDAP 条目的目录根。 需要声明映射了字段的所有对象类。请注意,在创建托管类的新条目时, 仅使用声明的对象类。spring-doc.cadn.net.cn

为了将目录条目视为与托管实体匹配,目录条目声明的所有对象类都必须由@Entry注解。 例如,假设您的 LDAP 树中有具有以下对象类的条目:inetOrgPerson,organizationalPerson,person,top. 如果您只对更改personobject 类中,您可以为@Entry@Entry(objectClasses = { "person", "top" }). 但是,如果要管理inetOrgPersonobject类,您需要使用以下代码:@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" }).spring-doc.cadn.net.cn

所有实体字段都按其字段名称映射到 LDAP 属性。其余注释 —@Id,@Attribute,@Transient@DnAttribute— 影响映射的发生方式。spring-doc.cadn.net.cn

首先,@Idannotation 将条目的专有名称映射到字段。该字段必须是javax.naming.Name.spring-doc.cadn.net.cn

其次,@Attributeannotation 将实体字段映射到 LDAP 属性。 当属性名称与字段名称不同时,这很方便。 要使用@Attribute中,您必须声明字段映射到的属性的名称。 (可选)您还可以通过包含 LDAP 属性的语法 OID 来保证和完全匹配。 最后@Attribute还提供类型声明,它允许您指示 LDAP JNDI 提供程序是将该属性视为基于二进制的还是基于字符串的。spring-doc.cadn.net.cn

第三,@Transientannotation 表示给定的实体字段未映射到 LDAP 属性。spring-doc.cadn.net.cn

最后,@DnAttributeannotation 还会将 Entity Fields 映射到条目的可分辨名称的组件。spring-doc.cadn.net.cn

考虑具有以下 Comments 的类:spring-doc.cadn.net.cn

@DnAttribute(name="uid")
String uid;

以及如下所示的 DN:spring-doc.cadn.net.cn

uid=carla,dc=springframework,dc=org

然后 Spring LDAP 将填充uiduid=carla而不是寻找uid属性。spring-doc.cadn.net.cn

Only fields of type `String` can be annotated with `@DnAttribute`. Other types are not supported.

您也可以提供如下所示的索引:spring-doc.cadn.net.cn

@DnAttribute(index=1)
String uid;

@DnAttribute(index=0)
String department;

这对于具有多个组件的 DN 非常方便:spring-doc.cadn.net.cn

uid=carla,department=engineering,dc=springframework,dc=org

使用index还允许 Spring LDAP 在创建或查找要更新或删除的实体时为您计算 DN。 对于更新方案,如果属于可分辨名称的属性已更改,这也会自动处理树中的移动条目。spring-doc.cadn.net.cn

Note that while both attributes are present on `@DnAttribute`, if `index` is specified, then `name` is ignored.
请记住,默认情况下,所有字段都映射到 LDAP 属性。@DnAttribute不会改变这一点;换句话说,使用@DnAttribute也将映射到 LDAP 属性,除非您还使用@Transient.

执行

当所有组件都已正确配置和注释后,LdapTemplate可以按如下方式使用:spring-doc.cadn.net.cn

示例 1.执行
@Entry(objectClasses = { "person", "top" }, base="ou=someOu")
public class Person {
   @Id
   private Name dn;

   @Attribute(name="cn")
   @DnAttribute(value="cn", index=1)
   private String fullName;

   // No @Attribute annotation means this will be bound to the LDAP attribute
   // with the same value
   private String description;

   @DnAttribute(value="ou", index=0)
   @Transient
   private String company;

   @Transient
   private String someUnmappedField;
   // ...more attributes below
}


public class OdmPersonRepo {
   @Autowired
   private LdapTemplate ldapTemplate;

   public Person create(Person person) {
      ldapTemplate.create(person);
      return person;
   }

   public Person findByUid(String uid) {
      return ldapTemplate.findOne(query().where("uid").is(uid), Person.class);
   }

   public void update(Person person) {
      ldapTemplate.update(person);
   }

   public void delete(Person person) {
      ldapTemplate.delete(person);
   }

   public List<Person> findAll() {
      return ldapTemplate.findAll(Person.class);
   }

   public List<Person> findByLastName(String lastName) {
      return ldapTemplate.find(query().where("sn").is(lastName), Person.class);
   }

   public Stream<Person> streamFindByLastName(String lastName) {
      return ldapTemplate.findStream(query().where("sn").is(lastName), Person.class);
   }
}

ODM 和专有名称作为属性值

LDAP 中的安全组通常包含一个多值属性,其中每个值都是可分辨名称 系统中的用户。处理这些类型的属性时涉及的困难在DirContextAdapter和作为属性值的可分辨名称.spring-doc.cadn.net.cn

ODM 还支持javax.naming.Name属性值,使组修改变得容易,如下例所示:spring-doc.cadn.net.cn

示例 2.组表示示例
@Entry(objectClasses = {"top", "groupOfUniqueNames"}, base = "cn=groups")
public class Group {

    @Id
    private Name dn;

    @Attribute(name="cn")
    @DnAttribute("cn")
    private String name;

    @Attribute(name="uniqueMember")
    private Set<Name> members;

    public Name getDn() {
        return dn;
    }

    public void setDn(Name dn) {
        this.dn = dn;
    }

    public Set<Name> getMembers() {
        return members;
    }

    public void setMembers(Set<Name> members) {
        this.members = members;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addMember(Name member) {
        members.add(member);
    }

    public void removeMember(Name member) {
        members.remove(member);
    }
}

当您使用setMembers,addMemberremoveMember然后调用ldapTemplate.update(), 属性修改是使用专有名称相等性计算的,这意味着 在确定它们是否相等时,将忽略 distinguished names。spring-doc.cadn.net.cn