Java Netscape LDAP 删除一个属性

发布于 2024-08-27 16:28:16 字数 299 浏览 11 评论 0原文

我有 LDAP 架构,用户在哪里。我需要删除一个名为“notify”的属性,该属性的值是:电话号码或邮件,或者删除用户的属性。我找到了方法

LDAPConnection myCon = new LDAPConnection("localhost",389);
myCon.delete("uid=test1, ou=People, o=domain.com, o=isp");

,但这会删除整个用户,我只需要删除该用户的一个属性“notifyTo”。我需要删除整个属性,而不仅仅是其值。

感谢您的回复

I have LDAP schema where are users. I need remove one attribute named "notify" which have values: phone number or mail or remove attribute from user. I found method

LDAPConnection myCon = new LDAPConnection("localhost",389);
myCon.delete("uid=test1, ou=People, o=domain.com, o=isp");

but this remove whole user and i need remove only one attribute "notifyTo" of this user. I need remove whole attribute not only its value.

Thanks for reply

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情归归情 2024-09-03 16:28:16

您需要调用 修改 LDAPConnection 类的方法 :-)

从 javadocs:

公共无效修改(java.lang.String
DN,
LDAP修改模式)
抛出 LDAPException 对现有条目进行单个更改
在目录中(例如,更改
属性的值,添加一个新的
属性值,或删除
现有属性值)。使用
LDAPModification 对象指定
更改 make 和 LDAPAttribute
指定属性值的对象
改变。 LDAPModification 对象
允许您添加属性值,
更改属性值,或删除
属性值。

例如,以下部分
代码更改 Barbara Jensen 的电子邮件
目录中的地址
[电子邮件受保护]

javadocs 中的示例代码:

String myEntryDN = "cn=Barbara Jensen,ou=Product Development,o=Ace Industry,c=US";
LDAPAttribute attrEmail = new LDAPAttribute( "mail", "[email protected]" );
LDAPModification singleChange = new LDAPModification( LDAPModification.REPLACE, attrEmail );

myConn.modify( myEntryDN, singleChange );

此示例用于删除条目属性之一的一个值。您需要删除所有值:-)

You need to call modify method on LDAPConnection class :-)

From the javadocs:

public void modify(java.lang.String
DN,
LDAPModification mod)
throws LDAPException Makes a single change to an existing entry
in the directory (for example, changes
the value of an attribute, adds a new
attribute value, or removes an
existing attribute value). Use the
LDAPModification object to specify the
change to make and the LDAPAttribute
object to specify the attribute value
to change. The LDAPModification object
allows you add an attribute value,
change an attibute value, or remove an
attribute value.

For example, the following section of
code changes Barbara Jensen's email
address in the directory to
[email protected].

Example code from javadocs:

String myEntryDN = "cn=Barbara Jensen,ou=Product Development,o=Ace Industry,c=US";
LDAPAttribute attrEmail = new LDAPAttribute( "mail", "[email protected]" );
LDAPModification singleChange = new LDAPModification( LDAPModification.REPLACE, attrEmail );

myConn.modify( myEntryDN, singleChange );

This sample is for removing one value of one of your entry's attributes. You need to delete all values :-)

相守太难 2024-09-03 16:28:16

没有 Netscape API 的解决方案:

import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
....
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
DirContext dctx = new InitialDirContext(env);
// next 3 lines only if authentication needed
dctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
dctx.addToEnvironment(Context.SECURITY_PRINCIPAL, "<userDN>");
dctx.addToEnvironment(Context.SECURITY_CREDENTIALS, "<password>");

Attributes attrs= new BasicAttributes();
Attribute attr= new BasicAttribute("<attrName>");
attrs.put(attr);
dctx.modifyAttributes ("<entryDN>", DirContext.REMOVE_ATTRIBUTE, attrs);

Solution without Netscape API:

import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
....
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
DirContext dctx = new InitialDirContext(env);
// next 3 lines only if authentication needed
dctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
dctx.addToEnvironment(Context.SECURITY_PRINCIPAL, "<userDN>");
dctx.addToEnvironment(Context.SECURITY_CREDENTIALS, "<password>");

Attributes attrs= new BasicAttributes();
Attribute attr= new BasicAttribute("<attrName>");
attrs.put(attr);
dctx.modifyAttributes ("<entryDN>", DirContext.REMOVE_ATTRIBUTE, attrs);
迷迭香的记忆 2024-09-03 16:28:16

老问题但好问题,来自文档(Directory SDK for Java 4.0程序员指南)并补充 SourceRebels 的答案:

要从条目中删除属性,您可以执行以下操作之一:

  • 将属性的值替换为无值(构造无值的 LDAPAttribute 对象)
  • 指定您要从属性中删除一个值,并且不指定任何值(构造
    没有值的 LDAPAttribute 对象)
  • 删除属性的所有值

Old question but good question, from the docs (Directory SDK for Java 4.0 Programmer's Guide) and complementing SourceRebels' answer:

To remove an attribute from an entry, you can do one of the following:

  • replace the values of the attribute with no values (construct the LDAPAttribute object with no values)
  • specify that you want to remove a value from the attribute, and specify no value (construct the
    LDAPAttribute object with no values)
  • remove all values of the attribute
埋情葬爱 2024-09-03 16:28:16

您可以在 LDAPModificationSet 中将该属性设置为 LDAPModification.DELETE

如果属性为“notifyTo”,

LDAPConnection myCon = new LDAPConnection("localhost",389);
LDAPModificationSet mods = new LDAPModificationSet();
mods.add(LDAPModification.DELETE, new LDAPAttribute("notifyTo"));
myCon.modify("uid=test1, ou=People, o=domain.com, o=isp", mods);

您可以添加、替换或删除用户的任意数量的属性。所有这些都可以在 LDAPModificationSet 中指定要执行的操作。
如果要替换用户的“email”属性,请将其添加到 LDAPModificationSet 中并最后调用modify() 方法。

mods.add(LDAPModification.REPLACE, new LDAPAttribute("email","[email protected]"));

删除属性时,只需确保该属性已存在于用户 LDAP 条目中,否则调用修改()方法时将抛出 NO_SUCH_ATTRIBUTE(错误代码 16)LDAPException。

You can set that attribute as LDAPModification.DELETE in the LDAPModificationSet

If the attribute is "notifyTo",

LDAPConnection myCon = new LDAPConnection("localhost",389);
LDAPModificationSet mods = new LDAPModificationSet();
mods.add(LDAPModification.DELETE, new LDAPAttribute("notifyTo"));
myCon.modify("uid=test1, ou=People, o=domain.com, o=isp", mods);

You can add, replace or delete any number of attributes from the user. All these can be specified in the LDAPModificationSet actions to be performed.
If you want to replace an attribute "email" for the user, add it to the LDAPModificationSet and call the modify() method in the end.

mods.add(LDAPModification.REPLACE, new LDAPAttribute("email","[email protected]"));

While deleting an attribute,just make sure that the attribute is already present in the user LDAP entry, otherwise a NO_SUCH_ATTRIBUTE(Error Code 16) LDAPException would be thrown when the modify() method is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文