对于最新稳定版本,请使用 Spring LDAP 4.0.0spring-doc.cadn.net.cn

对象-目录映射(ODM)

对象-关系映射框架(如 Hibernate 和 JPA)为开发者提供了使用注释将关系数据库表映射到 Java 对象的能力。 Spring LDAP 项目通过多种方法在Ldap运营: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树中有包含以下对象类的条目:inet组织人、组织人、人、顶部. 如果你只想更改定义在对象类,你可以注释你的@Entry@Entry(objectClasses = { “person”, “top” }). 然而,如果你想管理定义在inetOrgPersonObjectclass,你需要使用以下内容:@Entry(objectClasses = { “inetOrgPerson”, “organizationalPerson”, “person”, “top” }).spring-doc.cadn.net.cn

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

首先,该@Id注释将条目名称映射到字段。该域必须是 的实例javax.naming.Name.spring-doc.cadn.net.cn

其次,@Attribute注释将实体字段映射到 LDAP 属性。 当属性名和字段名不同时,这很方便。 使用@Attribute你必须声明该字段所映射的属性名称。 可选地,你也可以保证并精确匹配,通过包含 LDAP 属性的语法 OID。 最后@Attribute还提供类型声明,允许你指示该属性在 LDAP JNDI 提供者眼中是基于二进制还是基于字符串。spring-doc.cadn.net.cn

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

最后,是@DnAttribute注释还将实体字段映射到条目区分名称的组成部分。spring-doc.cadn.net.cn

考虑一个带有以下注释的类:spring-doc.cadn.net.cn

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

以及如下这样的DN:spring-doc.cadn.net.cn

uid=carla,dc=springframework,dc=org

然后春季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

使用指数同时,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);
    }
}

当你通过使用来修改组成员时成员,addMember移除成员然后呼唤ldapTemplate.update(), 属性修改通过使用区分名称相等计算,意味着文本格式 在判断它们是否相等时,区分名称被忽略。spring-doc.cadn.net.cn