JAVA,OpenLDAP使用心得.docx

上传人:sk****8 文档编号:3533952 上传时间:2019-06-02 格式:DOCX 页数:11 大小:24.88KB
下载 相关 举报
JAVA,OpenLDAP使用心得.docx_第1页
第1页 / 共11页
JAVA,OpenLDAP使用心得.docx_第2页
第2页 / 共11页
JAVA,OpenLDAP使用心得.docx_第3页
第3页 / 共11页
JAVA,OpenLDAP使用心得.docx_第4页
第4页 / 共11页
JAVA,OpenLDAP使用心得.docx_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、http:/njc JAVA,OpenLDAP 使用心得 中可以找到 schema,pid 以及数据库文件存放的路径我修改了/usr/local/etc/openldap/slapd.conf 文件,但是发现没啥用,原来是忘了把slapd 停止重新启动了。关于停止 slapd,官方给的是:kill -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 命

2、令找到了在/usr/local/var/run/下,把命令改为:kill -INT cat /usr/local/var/run/slapd.pid重新运行 slapd:su root -c /usr/local/libexec/slapd建议执行/usr/local/libexec/slapd -d256 命令,这样既可以在命令行看到出错信息,也可以用Ctrl+C 停止进程关于 rootpw,很多地方都说 rootpw 和密码值之间不能加空格,不然会出错。有个解决的办法:rootpw “secret“ 加了双引号,只要输入的密码和引号里面的对应就可以了。很多人在测试 ldapadd 命令时,

3、都遇到过 ldap_bind: Invalid credentials(49)错误,看看rootdn “cn=Manager,dc=example,dc=com“和自己的 ldif 里面的 dn 参数是不是匹配,如果不匹配就需要修改,修改后记得要停止重启哦(我还不知道怎么重新读取配置的方法,只能用这种笨方法了)折腾了一天,终于初步了解 JAVA 怎么在 OpenLDAP 增加删除数据了。代码如下/* author chenyi*/import java.util.Hashtable;import javax.naming.Context;import javax.naming.NamingEx

4、ception;import javax.naming.directory.*;import java.util.*;public class ChenYi DirContext ctx = null;String account = “Manager“;/操作 LDAP 的帐户。默认就是 Manager。String password = “secret“;/帐户 Manager 的密码。String root = “dc=example,dc=com“; /LDAP 的根节点的 DCpublic ChenYi() init();add();/delete();close();public

5、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);e

6、nv.put(Context.SECURITY_CREDENTIALS, password);try ctx = new InitialDirContext(env);/初始化上下文System.out.println(“认证成功“);/这里可以改成异常抛出。 catch (javax.naming.AuthenticationException e) System.out.println(“认证失败“); catch (Exception e) System.out.println(“认证出错:“ + e);public void add() try String newUserName =

7、 “hi“;BasicAttributes attrs = new BasicAttributes();BasicAttribute objclassSet = new BasicAttribute(“objectClass“);objclassSet.add(“top“);objclassSet.add(“organizationalUnit“);attrs.put(objclassSet);attrs.put(“ou“, newUserName);ctx.createSubcontext(“ou=“ + newUserName + “,“ + root, attrs); catch (Ex

8、ception 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) e.printStackTrace();System.out.println(“Exception in delete():“ + e);public void close() if (ctx != null) try ctx.close(); catch

9、 (NamingException e) System.out.println(“NamingException in close():“ + e);public static void main(String args) new ChenYi();红线标记的地方特别注意,我看很多文章中写的都类似于 env.put(Context.PROVIDER_URL, “ldap:/localhost:7001/“ + root); 经过我一天的折腾发现加上了 root,会报javax.naming.NameNotFoundException: LDAP: error code 32 - No Such

10、 Object;错误 。也许这是新版不兼容旧版程序吧今天终于把添加,删除,修改节点名,属性,遍历节点都弄出来了,先把代码贴出来吧/* author chenyi*/import java.util.Hashtable;import javax.naming.directory.*;import java.util.*;import javax.naming.*;public class ChenYi DirContext dc = null;String account = “Manager“;/操作 LDAP 的帐户。默认就是 Manager。String password = “secre

11、t“;/帐户 Manager 的密码。String root = “dc=example,dc=com“; /LDAP 的根节点的 DCpublic ChenYi() init();/add();/添加节点/delete(“ou=hi,dc=example,dc=com“);/删除“ou=hi,dc=example,dc=com“节点/modifyInformation(“ou=hi,dc=example,dc=com“);/修改“ou=hi,dc=example,dc=com“属性/renameEntry(“ou=new,o=neworganization,dc=example,dc=com

12、“,“ou=neworganizationalUnit,o=neworganization,dc=example,dc=com“);/重命名节点“ou=new,o=neworganization,dc=example,dc=com“searchInformation(“dc=example,dc=com“, “, “(objectclass=*)“);/遍历所有根节点/searchInformation(“o=neworganization,dc=example,dc=com“,“,“(objectclass=*)“);/遍历指定节点的分节点close();public void init()

13、 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(Conte

14、xt.SECURITY_CREDENTIALS, password);try dc = new InitialDirContext(env);/初始化上下文System.out.println(“认证成功“);/这里可以改成异常抛出。 catch (javax.naming.AuthenticationException e) System.out.println(“认证失败“); catch (Exception e) System.out.println(“认证出错:“ + e);public void close() if (dc != null) try dc.close(); cat

15、ch (NamingException e) System.out.println(“NamingException in close():“ + e);public void add() try String newUserName = “hi“;BasicAttributes attrs = new BasicAttributes();BasicAttribute objclassSet = new BasicAttribute(“objectClass“);objclassSet.add(“top“);objclassSet.add(“organizationalUnit“);attrs

16、.put(objclassSet);attrs.put(“ou“, newUserName);dc.createSubcontext(“ou=“ + newUserName + “,“ + root, attrs); catch (Exception e) e.printStackTrace();System.out.println(“Exception in add():“ + e);public void delete(String dn) try dc.destroySubcontext(dn); catch (Exception e) e.printStackTrace();Syste

17、m.out.println(“Exception in delete():“ + e);public boolean modifyInformation(String dn) try ModificationItem mods = new ModificationItem1;/*添加属性*/ Attribute attr0 = new BasicAttribute(“description“,/ “测试“);/ mods0 = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0);/*修改属性*/ Attribute attr0 = new

18、BasicAttribute(“description“, “陈轶“);/ mods0 = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,/ attr0);/*删除属性*/Attribute attr0 = new BasicAttribute(“description“,“陈轶“);mods0 = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,attr0);dc.modifyAttributes(dn, mods);return true; catch (NamingException

19、ne) ne.printStackTrace();System.err.println(“Error: “ + ne.getMessage();return false;/* param base :根节点(在这里是“dc=example,dc=com“)* param scope :搜索范围, 分为“base“( 本节点),“one“(单层),“(遍历)* param filter :指定子节点( 格式为“(objectclass=*)“,*是指全部,你也可以指定某一特定类型的树节点)*/public void searchInformation(String base, String sc

20、ope, String filter) SearchControls sc = new SearchControls();if (scope.equals(“base“) sc.setSearchScope(SearchControls.OBJECT_SCOPE); else if (scope.equals(“one“) sc.setSearchScope(SearchControls.ONELEVEL_SCOPE); else sc.setSearchScope(SearchControls.SUBTREE_SCOPE);NamingEnumeration ne = null;try ne

21、 = dc.search(base, filter, sc);/ Use the NamingEnumeration object to cycle through/ the result set.while (ne.hasMore() System.out.println();SearchResult sr = (SearchResult) ne.next();String name = sr.getName();if (base != null else System.out.println(“entry: “ + name);Attributes at = sr.getAttribute

22、s();NamingEnumeration ane = at.getAll();while (ane.hasMore() Attribute attr = (Attribute) ane.next();String attrType = attr.getID();NamingEnumeration values = attr.getAll();Vector vals = new Vector();/ Another NamingEnumeration object, this time/ to iterate through attribute values.while (values.has

23、More() Object oneVal = values.nextElement();if (oneVal instanceof String) System.out.println(attrType + “: “ + (String) oneVal); else System.out.println(attrType + “: “ + new String(byte) oneVal); catch (Exception nex) System.err.println(“Error: “ + nex.getMessage();nex.printStackTrace();public bool

24、ean renameEntry(String oldDN, String newDN) try dc.rename(oldDN, newDN);return true; catch (NamingException ne) System.err.println(“Error: “ + ne.getMessage();return false;public static void main(String args) new ChenYi(); 经过几天的努力,把获取 objectClass 定义和获取 Attribute 定义的代码弄出来,这样就方便了以后根据自定义 schema 动态的获取 s

25、chema 中的 objectClass 和 Attribute。特别是对于做添加修改界面应该有点用处,修改了 schema 并不需要修改代码做代码调整,只需要根据获取的属性个数挨个排好,让别人填入值,并且可以检测 MUST 的是不是已经填写了。/* 获取指定 objectClass 的定义* param name*/public void getObjectClassDefinition(String name) try / Get the schema tree rootDirContext schema = dc.getSchema(“);/ Get the schema object

26、for “person“DirContext personSchema = (DirContext) schema.lookup(“ClassDefinition/“ + name);Attributes a = personSchema.getAttributes(“);NamingEnumeration ane = a.getAll();while (ane.hasMore() Attribute attr = (Attribute) ane.next();String attrType = attr.getID();NamingEnumeration values = attr.getA

27、ll();while (values.hasMore() Object oneVal = values.nextElement();if (oneVal instanceof String) System.out.println(attrType + “: “ + (String) oneVal); else System.out.println(attrType + “: “ + new String(byte) oneVal); catch (Exception e) e.printStackTrace();/* 获取指定 DN 的 objectClass 定义* param DN*/pu

28、blic void getDNObjectClassDefinition(String DN) try / Get context containing class definitions for the “cn=Ted Geisel“ entryDirContext tedClasses = dc.getSchemaClassDefinition(DN);/ Enumerate the class definitionsNamingEnumeration enum1 = tedClasses.search(“, null);while (enum1.hasMore() Object o =

29、enum1.next();System.out.println(SearchResult) o).getName();Attributes a = (SearchResult) o).getAttributes();NamingEnumeration ane = a.getAll();while (ane.hasMore() Attribute attr = (Attribute) ane.next();String attrType = attr.getID();NamingEnumeration values = attr.getAll();while (values.hasMore()

30、Object oneVal = values.nextElement();if (oneVal instanceof String) System.out.println(attrType + “: “ + (String) oneVal); else System.out.println(attrType + “: “ + new String(byte) oneVal); catch (Exception e) e.printStackTrace();/* 获取指定名字的 Attribute 定义* param name*/public void getAttributeDefinitio

31、n(String name) try / Get the schema tree rootDirContext schema = dc.getSchema(“);/ Get the schema object for “person“DirContext personSchema = (DirContext) schema.lookup(“AttributeDefinition/“ + name);Attributes a = personSchema.getAttributes(“);NamingEnumeration ane = a.getAll();while (ane.hasMore(

32、) Attribute attr = (Attribute) ane.next();String attrType = attr.getID();NamingEnumeration values = attr.getAll();while (values.hasMore() Object oneVal = values.nextElement();if (oneVal instanceof String) System.out.println(attrType + “: “ + (String) oneVal); else System.out.println(attrType + “: “

33、+ new String(byte) oneVal); catch (Exception e) e.printStackTrace();/* 获取指定 DN 中指定名字的 Attribute 定义* param DN* param name*/public void getDNAttributeDefinition(String DN, String name) try / Get an attribute of that typeAttributes attrs = dc.getAttributes(DN, new Stringname);Attribute cnAttr = attrs.g

34、et(name);/ Get its attribute type definitionDirContext cnSchema = cnAttr.getAttributeDefinition();/ Get cnSchemas attributesAttributes cnAttrs = cnSchema.getAttributes(“);NamingEnumeration ane = cnAttrs.getAll();while (ane.hasMore() Attribute attr = (Attribute) ane.next();String attrType = attr.getID();NamingEnumeration values = attr.getAll();while (values.hasMore() Object oneVal = values.nextElement();if (oneVal instanceof String) System.out.println(attrType + “: “ + (String) oneVal); else System.out.println(attrType + “: “ + new String(byte) oneVal); catch (Exception e)

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

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。