JAVAOpenLDAP使用心得.docx

上传人:公**** 文档编号:548968214 上传时间:2022-08-29 格式:DOCX 页数:11 大小:24.88KB
返回 下载 相关 举报
JAVAOpenLDAP使用心得.docx_第1页
第1页 / 共11页
JAVAOpenLDAP使用心得.docx_第2页
第2页 / 共11页
JAVAOpenLDAP使用心得.docx_第3页
第3页 / 共11页
JAVAOpenLDAP使用心得.docx_第4页
第4页 / 共11页
JAVAOpenLDAP使用心得.docx_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《JAVAOpenLDAP使用心得.docx》由会员分享,可在线阅读,更多相关《JAVAOpenLDAP使用心得.docx(11页珍藏版)》请在金锄头文库上搜索。

1、http:/njcJAVA,OpenLDAP使用心得 -INT cat /usr/local/var/slapd.pid但是我执行以后提示bash: kill: cat /usr/local/var/slapd.pid: arguments must be process or job IDs用find /usr -name slapd.pid命令找到了在/usr/local/var/run/下,把命令改为:kill -INT cat /usr/local/var/run/slapd.pid重新运行slapd:su root -c /usr/local/libexec/slapd建议执行/us

2、r/local/libexec/slapd -d256 命令,这样既可以在命令行看到出错信息,也可以用Ctrl+C停止进程关于rootpw,很多地方都说rootpw和密码值之间不能加空格,不然会出错。有个解决的办法:rootpw secret 加了双引号,只要输入的密码和引号里面的对应就可以了。很多人在测试ldapadd命令时,都遇到过ldap_bind: Invalid credentials(49)错误,看看rootdn cn=Manager,dc=example,dc=com和自己的ldif里面的dn参数是不是匹配,如果不匹配就需要修改,修改后记得要停止重启哦(我还不知道怎么重新读取配置

3、的方法,只能用这种笨方法了)折腾了一天,终于初步了解JAVA怎么在OpenLDAP增加删除数据了。代码如下/* author chenyi*/import java.util.Hashtable;import javax.naming.Context;import javax.naming.NamingException;import javax.naming.directory.*;import java.util.*;public class ChenYi DirContext ctx = null; String account = Manager;/操作LDAP的帐户。默认就是Mana

4、ger。 String password = secret;/帐户Manager的密码。 String root = dc=example,dc=com; /LDAP的根节点的DC public ChenYi() init(); add(); /delete(); close(); public void init() Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.ldap.LdapCtxFactory);env.put(Context.PROVIDER_URL, l

5、dap:/192.168.100.221:389/); env.put(Context.SECURITY_AUTHENTICATION, simple); env.put(Context.SECURITY_PRINCIPAL, cn= + account + , + root); env.put(Context.SECURITY_CREDENTIALS, password); try ctx = new InitialDirContext(env);/初始化上下文 System.out.println(认证成功);/这里可以改成异常抛出。 catch (javax.naming.Authent

6、icationException e) System.out.println(认证失败); catch (Exception e) System.out.println(认证出错: + e); public void add() try String newUserName = hi; BasicAttributes attrs = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute(objectClass); objclassSet.add(top); objclassSet.add(organizat

7、ionalUnit); attrs.put(objclassSet); attrs.put(ou, newUserName); ctx.createSubcontext(ou= + newUserName + , + root, attrs); catch (Exception e) e.printStackTrace(); System.out.println(Exception in add(): + e); public void delete() try ctx.destroySubcontext(ou=hi,dc=example,dc=com); catch (Exception e

8、) e.printStackTrace(); System.out.println(Exception in delete(): + e); public void close() if (ctx != null) try ctx.close(); catch (NamingException e) System.out.println(NamingException in close(): + e); public static void main(String args) new ChenYi(); 红线标记的地方特别注意,我看很多文章中写的都类似于env.put(Context.PROV

9、IDER_URL, ldap:/localhost:7001/ + root); 经过我一天的折腾发现加上了root,会报javax.naming.NameNotFoundException: LDAP: error code 32 - No Such Object;错误。也许这是新版不兼容旧版程序吧今天终于把添加,删除,修改节点名,属性,遍历节点都弄出来了,先把代码贴出来吧/* author chenyi*/import java.util.Hashtable;import javax.naming.directory.*;import java.util.*;import javax.na

10、ming.*;public class ChenYi DirContext dc = null; String account = Manager;/操作LDAP的帐户。默认就是Manager。 String password = secret;/帐户Manager的密码。 String root = dc=example,dc=com; /LDAP的根节点的DC public ChenYi() init(); /add();/添加节点 /delete(ou=hi,dc=example,dc=com);/删除ou=hi,dc=example,dc=com节点 /modifyInformatio

11、n(ou=hi,dc=example,dc=com);/修改ou=hi,dc=example,dc=com属性 /renameEntry(ou=new,o=neworganization,dc=example,dc=com,ou=neworganizationalUnit,o=neworganization,dc=example,dc=com);/重命名节点ou=new,o=neworganization,dc=example,dc=com searchInformation(dc=example,dc=com, , (objectclass=*);/遍历所有根节点 /searchInform

12、ation(o=neworganization,dc=example,dc=com,(objectclass=*);/遍历指定节点的分节点 close(); public void init() Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.ldap.LdapCtxFactory); env.put(Context.PROVIDER_URL, ldap:/192.168.100.221:389/); env.put(Context.SECURITY_AUTHENTICATION, simple); env.put(Context.SECURITY_PRINCIPAL, cn= + account + , + root); env.put(Context.SECURITY_CREDENTIALS, password

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号