【问题标题】:how to delete a particular attribute from LDAP using spring template如何使用弹簧模板从 LDAP 中删除特定属性
【发布时间】:2015-04-12 17:08:26
【问题描述】:

我在删除过程中使用以下代码:

public void delete(Person p) {
  Name dn = buildDn(p);
  ldapTemplate.unbind(dn);
}

但使用unbind() 方法,它会删除所有属性,但我只想从 LDAP 中删除特定属性,例如人的角色。

【问题讨论】:

    标签: spring unbind spring-ldap


    【解决方案1】:

    对于属性修改,您应该使用DirContextAdapter,如reference documentation 中所述。适用于您要求的部分是第 3.2.2 节(更新);要删除属性值,您应该使用removeAttributeValue

    【讨论】:

      【解决方案2】:

      共有三种模式:

      • MOD_ADD: 这用于添加属性值。如果属性已经存在(并且模式允许多个值),则新的 值将被添加,旧值将保留。
      • MOD_DELETE:属性值将被删除,如果存在的话。
      • MOD_REPLACE: 给定的属性值将替换该属性名称的所有其他值。换句话说,所有旧值 该属性将被删除,然后该值将被添加。

      你应该使用 MOD_REPLACE

      >>> mod_attrs = [ (ldap.MOD_DELETE, 'cn','Francis Bacon') ]
      >>> l.modify_s('uid=francis,ou=users,dc=example,dc=com', mod_attrs)
      (103, [])
      >>>
      

      这将从 cn 属性中仅删除属性值 Francis Bacon。如果不存在这样的值,则会引发 NO_SUCH_ATTRIBUTE 异常。否则,该值将被丢弃。

      来源:https://www.packtpub.com/books/content/python-ldap-applications-part-3-more-ldap-operations-and-ldap-url-library

      【讨论】:

        【解决方案3】:

        如果您不知道字段值,则使用 None 作为这样的值(以@Anouar Mokhtari 为例):

        mod_attrs = [ (ldap.MOD_DELETE, 'cn', None) ]
        l.modify_s('uid=francis,ou=users,dc=example,dc=com', mod_attrs)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-14
          • 2014-08-22
          • 2015-12-15
          相关资源
          最近更新 更多