已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2009-08-12LDAP代码操作DemoLDAP操作代码样例 初始化LDAP 目录服务上下文 该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap:/localhost:8389),认证方式采用simple类型,即用户名/密码方式。 private static void initialContext() throws NamingException if(singleton = null) singleton = new LDAPConnection(); /* * 在实际编码中,这些环境变量应尽可能通过配置文件读取 */ /LDAP服务地址 singleton.sLDAP_URL = ldap:/localhost:8389; /管理员账号 singleton.sMANAGER_DN = uid=linly,ou=People,dc=jsoso,dc=net; /管理员密码 singleton.sMANAGER_PASSWORD = coffee; /认证类型 singleton.sAUTH_TYPE = simple; /JNDI Context工厂类 singleton.sCONTEXT_FACTORY = com.sun.jndi.ldap.LdapCtxFactory; singleton.envProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, singleton.sCONTEXT_FACTORY); singleton.envProps.setProperty(Context.PROVIDER_URL, singleton.sLDAP_URL); singleton.envProps.setProperty(Context.SECURITY_AUTHENTICATION, singleton.sAUTH_TYPE); singleton.envProps.setProperty(Context.SECURITY_PRINCIPAL, singleton.sMANAGER_DN); singleton.envProps.setProperty(Context.SECURITY_CREDENTIALS, singleton.sMANAGER_PASSWORD); /* * 绑定ldap服务器 */ singleton.dirCtx = new InitialDirContext(singleton.envProps); 通过一个Hashtable或者Properties对象为LDAP的Context设置参数,而后初始化InitialDirContext,即可绑定LDAP服务。这相当于JDBC中获取数据库的Connection对象。 绑定/创建LDAP条目对象 用户可以使用bind方法创建新的LDAP条目,下面的代码创建一个DN:ou=Employee , dc=jsoso ,dc=net的OrganizationUnit类LDAP条目如下: public boolean createOrganizationUnit() String ldapGroupDN = ou=Employee , dc=jsoso ,dc=net; try /* * 查找是否已经存在指定的OU条目 * 如果存在,则打印OU条目的属性信息 * 如果不存在,则程序会抛出NamingException异常,进入异常处理 */ Attributes attrs = dirContext.getAttributes(ldapGroupDN); System.out.println(Find the group , attributes list :); NamingEnumeration nEnum = attrs.getIDs(); for( ; nEnum.hasMore() ; ) String attrID = nEnum.next(); Attribute attr = (Attribute)attrs.get(attrID); System.out.println(attr.toString(); return false; catch (NamingException e) /* * 没有找到对应的Group条目,新增Group条目 */ /创建objectclass属性 Attribute objclass = new BasicAttribute(objectclass); objclass.add(top); objclass.add(organizationalunit); /创建cn属性 Attribute cn = new BasicAttribute(ou, Employee); /创建Attributes,并添加objectclass和cn属性 Attributes attrs = new BasicAttributes(); attrs.put(objclass); attrs.put(cn); /将属性绑定到新的条目上,创建该条目 try dirContext.bind(ldapGroupDN, null, attrs); System.out.println(Group created successful); return true; catch (NamingException e1) e1.printStackTrace(); return false; 获取条目属性 下面一段代码获取entryDN参数指定条目中的属性集合,并打印到控制台 /* * 获取一个指定的LDAP Entry * param entryDN */ public void find(String entryDN) try Attributes attrs = dirContext.getAttributes(entryDN); if (attrs != null) NamingEnumeration nEnum = attrs.getIDs(); for( ; nEnum.hasMore() ; ) String attrID = nEnum.next(); Attribute attr = (Attribute)attrs.get(attrID); System.out.println(attr.toString(); System.out.println(); else System.out.println(No found binding.); catch(NamingException ne) ne.printStackTrace(); 修改条目属性 修改DN=user.getDistinguishedName()的条目中的cn、givenname、sn和userpassword四个属性值。 (注:参数DirContext.REPLACE_ATTRIBUTE有另外两个常量:DirContext.ADD_ATTRIBUTE;DirContext.REMOVE_ATTRIBUTE,分别表示新增属性和删除属性。) /* * 修改用户信息 * param user * return * throws Exception */ public boolean modifyUser(LDAPUser user) throws Exception /用户对象为空 if (user = null) throw new Exception(No user information!n); /检查uid String userDN = user.getDistinguishedName(); if (userDN = null & userDN.length() = 0) throw new NamingException(No userDN you specify!n); /判断用户条目是否已经存在 if(!isUserexist(userDN) return false; /设置属性 Attributes attrs = new BasicAttributes(); setBasicAttribute(attrs, cn, user.getCommomName(); setBasicAttribute(attrs, givenname, user.getFirstName(); setBasicAttribute(attrs, sn, user.getLastName(); setBasicAttribute(attrs, userpassword, user.getPassword(); /修改属性 try dirContext.modifyAttributes(user.getDistinguishedName(),DirContext.REPLACE_ATTRIBUTE, attrs); System.out.println(User( + user.getDistinguishedName() + ) information modified.n); return true; catch(NamingException ne) ne.printStackTrace(); return false; 根据属性集搜索条目 根据属性集matchingAttributes中的匹配值,在上下文DN= ou=People,dc=jsoso ,dc=net中搜索它的所有子树中的匹配条目。 (注:SearchControls的SCOPE参数详见SearchControls SCOPE补充说明) /* * 通过属性搜索LDAP范例 * return */ public void searchByAttribute(Attributes matchingAttributes) String baseDN = ou=People,dc=jsoso ,dc=net; SearchControls cons = new SearchControls(); cons.setSearchScope(SearchControls.SUBTREE_SCOPE); try Name baseName = new LdapName(baseDN); NamingEnumeration ne = dirContext.search(baseName, matchingAttributes); SearchResult entry = null; for(;ne.hasMore();) entry = ne.next(); showEntry(entry); catch (NamingException e) e.printStackTrace(); 根据过滤器搜索条目 根据过滤器条件,在上下文DN = ou=People,dc=jsoso ,dc=net中,搜索它的所有子树中的匹配条目。 (注:过滤器filter的相关语法详见LDAP filter语法补充说明) /* * 通过过滤器搜索LDAP范例 * return */ public void searchByFilter(String filter) String baseDN = ou=People,dc=jsoso ,dc=net; SearchControls cons = new SearchControls(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建筑节能工程热工性能检测报告编制方法选择
- 200吨转炉课程设计
- 数据统计与医院智慧化管理
- 幼儿园急救知识
- 《网络游戏风险控制管理指南》
- 机场设计与规划
- 环境艺术设计职业规划
- 数字字体设计
- 旅游项目设计
- 产品研发设计全流程解析
- 小升初小学数学《找规律》大题量练习总复习试卷练习题一
- 2026年北京市西城区初三下学期二模语文试卷及答案
- 非结核分枝杆菌肺病诊疗专家共识(2026版)
- 北京市海淀区2026届高三高考二模语文试卷(含答案)
- 2026年食品安全管理员资格考试试题【带答案】
- 2026年4月自考13000英语(专升本)试题及答案
- 2026年国家电网中级职称考试(政工专业)综合试题及答案
- 2026中国武夷实业股份有限公司招聘笔试历年参考题库附带答案详解
- 2026年融资专员考核笔题库及完整答案详解(夺冠)
- 2026年哈尔滨市道里区中考一模物理试卷和答案
- 民俗文化融入幼儿园课程的实践研究
评论
0/150
提交评论