java - 使用 JNDI 将任何数据(条目)添加到 LDAP 服务器
问题描述
我想将任何数据(条目)添加到 LDAP 服务器。我尝试了以下代码。一种方法可以连接哪个有效,另一种方法是添加不起作用的条目。
有什么建议或解决方案吗?
我附上了我的 LDAP 服务器的屏幕截图:
public static DirContext connectJndi() throws NamingException {
//method to connect to LDAP server using JNDI
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.60:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "******");
env.put(Context.SECURITY_CREDENTIALS, "*******");
//DirContext ctx = new InitialDirContext(env);
LdapContext ctx = new InitialLdapContext(env,null);
return ctx; // connection method works.
}
//this method doesn't work.
public static void insertJndi( ) throws NamingException {
LdapContext ctx = (LdapContext) connectJndi(); //connecting to the server
LDAPAttributeSet attrs = new LDAPAttributeSet();
String objectclass_values[] = { "top", "person", "op","11" };
LDAPAttribute attr = new LDAPAttribute("objectclass",objectclass_values);
//Attribute objectClass = new BasicAttribute("objectClass");
attrs.add(attr);
LDAPEntry myEntry = new LDAPEntry("cn=jin,c=kr", attrs);
ctx.add(myEntry);
解决方案
我尝试了其他 API 并且它有效。我打算删除这个问题,但这可能会对某些人有所帮助,所以我在这里复制我的代码。如果没有,我会删除它。
public static void insertJndi( ) throws NamingException {
DirContext ctx = connectJndi();
Attributes attributes = new BasicAttributes();
Attribute objectClass = new BasicAttribute("objectClass");
objectClass.add("anything");
attributes.put(objectClass);
Attribute a = new BasicAttribute("a");
Attribute b = new BasicAttribute("b");
a.add("jin");
b.add("lee");
attributes.put(a);
attributes.put(b);
ctx.createSubcontext("cn=a002,ou=b,ou=c,ou=d,o=government of Mars,c=Earth", attributes);
System.out.println("it worked");
推荐阅读
- python - 使用 Python 查找 Selenium 元素
- javascript - 关于护照序列化/反序列化用户中的回调函数的困惑
- linux - 由于某些未知原因,在此邮件脚本中获取此 KeyError
- ag-grid - 如何在 ag-grid 中捕获行悬停事件?
- repast-simphony - Repast:批量运行导致 UTF-8 损坏的字符
- c# - 需要顺序运行等待子任务的并发处理(在控制台应用程序中)
- jenkins - Jenkins Prometheus 插件不生成任何数据
- c++ - 如何使用自定义运算符 < 制作折叠表达式
我正在尝试基于参数包和折叠表达式制作打印功能。我的实现无法在 clang 10.0 上编译。这是代码:
#include <iostream> #include <set> std::ostream &operator<<(std::ostream
- java - 使用 RestClient 从 Scala 中的 elasticsearch 中删除文档
- c# - 操纵杆行为不正常团结