关于ldap:关于openLADP的进一步了解Id与DnAttribute

25次阅读

共计 3056 个字符,预计需要花费 8 分钟才能阅读完成。

前言:本周次要对 gitlabWebhook 转 github 的我的项目扫了开头,并没有遇到什么问题,所以就上周 LDAP 中的疑难进行了进一步的理解。
承接上文中的问题:@Id 和 @DnAttributeID 之间是什么关系。为什么在 ldapAdmin 中没有找到 @ID 对应的字段。

@Data
@Entry(base = "", objectClasses="inetOrgPerson")
public class Person {

  /**
   * 此字段不能少
   */
  @Id
  private Name id;

  @DnAttribute(value = "uid", index = 3)
  private String uid;

  @DnAttribute(value = "title", index = 4)
  private String title;

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

  @Attribute(name = "sn")
  private String myUerName;

  private String userPassword;

}

就拿下面这个实体来说,咱们能够用如下办法查问它外部的属性:

List<Person> list = ldapTemplate.search("", filter, new AttributesMapper() {
      @SneakyThrows
      @Override
      public Object mapFromAttributes(Attributes attributes) throws NamingException {NamingEnumeration<? extends Attribute> att = attributes.getAll();
        while (att.hasMore()) {Attribute a = att.next();
          System.out.println(a.getID() + "=" + a.get());
        }
}

然而构造中并没有打印出 id 这一项,然而如果没有这一项在发明实体时会报如下谬误。

报的很间接,就是短少 ID,那么咱们就要去 ldapTemplate.creat 函数外面去打点,看看报错是怎么来的。
测试后发现报错呈现在这里:

那么咱们再把 ID 属性增加完再来看这里。

因为咱们生成 ID 时没有给 ID 赋值,他会主动生成 ID,而他主动生成的 ID 就是由咱们在试题中申明的 @DnAttribute 注解来的,依据所给的优先级进行排序生成。

就相似于 @ID 是由 @DnAttribute 对立形成的联结主键,只不过因为 LDAP 是树形存储,主键之间须要有优先程序之分,来区别那一项是哪一项的下级节点。

那么他的存储模式是什么呢?
拿上面的实体来举例:

  @Test
  public void addPerson() {Person person = new Person();
    person.setUid("uid:17");
    person.setTitle("13131000001");
    person.setMyUerName("liMing");
    person.setCommonName("liming");
    person.setUserPassword("123456");
    ldapTemplate.create(person);
  }

他在库中的存储模式如下,因为 UID 的优先级要比 title 高,所以在父实体中。

那么咱们如果不给 @DnAttribute 属性赋初值呢?
就会报如下谬误:

总结来说的话就是 @Id 是为了结构该实体的 DN 而存在的,LDAP 中 Dn 的结构是源于咱们一开始对根底 dn 的配置以及存储实体的 @Id 字段。

也就是说这一部分是依据 @ID 字段生成的

那么天经地义 @DnAttribute 注解字段也就不能随便更改了,如果依照之前的一般属性批改办法:

  @Test
  public void update(){
    String oldPersonDn = "uid=uid:17";
    Person newPerson = new Person();
    newPerson.setUid("uid=uid:29");
      ldapTemplate.modifyAttributes(oldPersonDn, new ModificationItem[] {new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("uid", newPerson.getUid().trim())),
      });
  }

会报如下谬误:

[LDAP: error code 64 - value of naming attribute 'uid' is not present in entry]

也就是说 @DnAttribute 注解后,LDAP 并没有把它当做一般属性。
如果咱们要批改 DN 的话(当然个别状况下不应批改 dn)能够应用如下办法批改:

ldapTemplate.rename(oldPersonDn, newPersonDn);

遇到的其余小问题:

在初始化前台时呈现了这样的报错:

Error creating bean with name 'entityManagerFactory' defined in class path resource [...]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service
. . . . . .
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service 

去网上查阅之后根本都是说是配置文件没有配置齐全。
上面是我的配置文件(application.properties):

server.port=8088
dingTalkUrlPre=https://oapi.dingtalk.com/robot/send
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.url="jdbc:mysql://localhost:3310/setting?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect="org.hibernate.dialect.MySQL57Dialect"
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=mysql

并没有发现有哪些项没有配置齐全,之后才发现是因为 spring.jpa.properties.hibernate.dialect 和另一个配置项前面的参数用引号括起来了,在 application.yml 中是否增加引号并不影响辨认,然而在 application.properties 中却不行,之前并没有关注过这些,导致了这个谬误。

正文完
 0