有可以动态创建LDAP schema的JAVA api吗? 或可以在schema中动态增加 objectclass 或者 attributetype 吗?

hoseli008 2013-05-30 09:28:37
有可以动态创建LDAP schema的JAVA api吗?
或可以在schema中动态增加 objectclass 或者 attributetype 吗?
请高手指点!!!!!
...全文
210 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
li008800il 2013-08-23
  • 打赏
  • 举报
回复
哪位好心人搞定了吗?? 我也有同样的需求啊,求赐教啊!!!! QQ:137786730
gaofeng821013 2013-07-09
  • 打赏
  • 举报
回复
楼主您好,请问您的问题解决了吗,我也遇到了相同的需求,需要通过java修改LDAP的SCHEMA。 如果您解决了,可不可以把解决方法赐教一下。gaofengf@digitalchina.com 不胜感激涕零...
清蒸豆豆 2013-06-10
  • 打赏
  • 举报
回复
两段代码,是连在一起的···
清蒸豆豆 2013-06-10
  • 打赏
  • 举报
回复
   public boolean modifyInformation(String dn) {
       try {
           ModificationItem[] mods = new ModificationItem[1];

           /*添加属性*/
//           Attribute attr0 = new BasicAttribute("description",
//                   "测试");
//           mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0);

           /*修改属性*/
//           Attribute attr0 = new BasicAttribute("description", "尚");
//           mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
//                   attr0);

           /*删除属性*/
           Attribute attr0 = new BasicAttribute("description", "尚");
           mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                   attr0);
           dc.modifyAttributes(dn, mods);
           return true;
       } catch (NamingException ne) {
           ne.printStackTrace();
           System.err.println("Error: " + ne.getMessage());
           return false;
       }

   }

   /**
    * @param base :根节点(在这里是"o=tcl,c=cn")
    * @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
    * @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
    */
   public void searchInformation(String base, String scope, 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 = 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();
//System.out.println("************name = sr.getName():   " + name);
               if (base != null && !base.equals("")) {
                   System.out.println("entry: " + name + "," + base);
               } else {
                   System.out.println("entry: " + name);
               }

               Attributes at = sr.getAttributes();
               NamingEnumeration ane = at.getAll();

               while (ane.hasMore()) {
                   Attribute attr = (Attribute) ane.next();
                   String attrType = attr.getID();
System.out.println("********attrType = attr.getID():  " + attrType);
                   NamingEnumeration values = attr.getAll();
                   Vector vals = new Vector();
                   // Another NamingEnumeration object, this time
                   // to iterate through attribute values.
                   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));
                       }
                   }
                   if(values != null){
					   values.close();
				   }
               }
               if(ane != null){
				   ane.close();
			   }
           }
           if(ne != null){
			   ne.close();
		   }
       } catch (Exception nex) {
           System.err.println("Error: " + nex.getMessage());
           nex.printStackTrace();
       }
   }

   public void searchOneNode(String base){
	   SearchControls sc = new SearchControls();
	   sc.setSearchScope(SearchControls.OBJECT_SCOPE);
	   String filter = "(objectclass=*)";
	   
	   NamingEnumeration ne = null;
	   try {
		   ne = dc.search(base, filter, sc);
		   while(ne.hasMore()){
			   System.out.println();
			   SearchResult sr = (SearchResult) ne.next();
			   String name = sr.getName();
			   if(base != null && !base.equals("")){
				   System.out.println("entry: " + name + "," + base);
			   } else {
				   System.out.println("entry: " + name);
			   }
			   
			   Attributes at = sr.getAttributes();
			   NamingEnumeration ane = at.getAll();
			   
			   while(ane.hasMore()){
				   Attribute attr = (Attribute) ane.next();
				   String attrType = attr.getID();
				   NamingEnumeration values = attr.getAll();
				   Vector vals = new Vector();
				   while(values.hasMore()){
					   Object oneVal = values.next();
					   if(oneVal instanceof String){
						   System.out.println(attrType + ":" + oneVal);
					   }else{
						   System.out.println(attrType + ":" + new String((byte[]) oneVal));
					   }
				   }
				   if(values != null){
					   values.close();
				   }
			   }
			   if(ane != null){
				   ane.close();
			   }
		   }
		   if(ne != null){
			   ne.close();
		   }
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
   }
   
   public void searchAttrOfValue(String base, String attribute){
	   SearchControls sc = new SearchControls();
	   sc.setSearchScope(SearchControls.OBJECT_SCOPE);
	   String filter = "(objectclass=*)";
	   String outValue = null;
	   NamingEnumeration ne = null;
	   try {
		   ne = dc.search(base, filter, sc);
		   while(ne.hasMore()){
			   System.out.println();
			   SearchResult sr = (SearchResult) ne.next();
			   String name = sr.getName();
			   if(base != null && !base.equals("")){
				   System.out.println("entry: " + name + "," + base);
			   } else {
				   System.out.println("entry: " + name);
			   }
			   
			   Attributes at = sr.getAttributes();
			   NamingEnumeration ane = at.getAll();
			   
			   while(ane.hasMore()){
				   Attribute attr = (Attribute) ane.next();
				   String attrType = attr.getID();
				   
				   if(attrType.equals(attribute)){
					   NamingEnumeration values = attr.getAll();
					   Vector vals = new Vector();
					   int num = 1;
					   while(values.hasMore()){
						   Object oneVal = values.next();
						   if(oneVal instanceof String){
							   System.out.println(attrType + "-------:" + oneVal);
							   outValue = attrType + ": " + oneVal;
						   }else{
							   System.out.println(attrType + "+++++++:" + new String(Base64.encode((byte[]) oneVal)));
//							   Base64.encode((byte[]) oneVal);
//							   outValue = attrType + ": " + new String((byte[]) oneVal);
							   outValue = new String(Base64.encode((byte[]) oneVal));
							   fileStore("outFile" + num , outValue);
							   num ++;
						   }
					   }
					   if(values != null){
						   values.close();
					   }
				   }
			   }
			   if(ane != null){
				   ane.close();
			   }
		   }
		   if(ne != null){
			   ne.close();
		   }
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	//********************
//	fileStore("outFile", outValue);
   }
   
   
   public boolean renameEntry(String oldDN, String newDN) {
       try {
           dc.rename(oldDN, newDN);
           return true;
       } catch (NamingException ne) {
           System.err.println("Error: " + ne.getMessage());
           return false;
       }
   }

   public void fileStore(String outFile, String attrValue){
		String str = null;
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			in = new BufferedReader(new StringReader(attrValue));
			out = new PrintWriter(
								new BufferedWriter(
									new FileWriter(outFile + ".crl", false)));
			while((str=in.readLine()) != null){
//				out.println(new Date());
				out.println(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			try {
				if(in != null){
					in.close();
					in = null;
				}
				if(out != null){
					out.close();
					out = null;
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
   
   /*
   private class LdapThread extends Thread{
	   public void run(){
			   try {
				   while (true) {
					searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
					this.sleep(60000);
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   }
   }*/
   
   public void run(){
	   try {
		   while (true) {
			searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
			this.sleep(60000);
		}
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
  /* 
   public static void main(String[] args) {
       OpenLDAPTest1 ldap = new OpenLDAPTest1();
//       ldap.searchInformation("o=tcl,c=cn", "", "(objectclass=*)");
//       ldap.searchOneNode("uid=aaa,o=tcl,c=cn");
//       ldap.searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
//       new LdapThread().start();
       ldap.start();
       ldap.close();
   }*/
} 
清蒸豆豆 2013-06-10
  • 打赏
  • 举报
回复
今天在调代码,突然发现以前的代码,给你贴上吧:
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Hashtable;
import javax.naming.directory.*;

import java.util.*;
import javax.naming.*;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;

import org.bouncycastle.util.encoders.Base64;

public class OpenLDAPTest1 extends Thread{

   DirContext dc = null;
   String account = "test";//操作LDAP的帐户。默认就是Manager。
   String password = "secret";//帐户Manager的密码。
   String root = "o=datech,c=cn"; //LDAP的根节点的DC

   public OpenLDAPTest1() {
       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"属性
       
       //重命名节点"ou=new,o=neworganization,dc=example,dc=com"
       //renameEntry("ou=new,o=neworganization,dc=example,dc=com",
       				//"ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");
       				
       //searchInformation("o=tcl,c=cn", "", "(objectclass=*)");//遍历所有根节点
       
       //遍历指定节点的分节点
       //searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)");
       //close();
   }

   public void init() {
	   if (dc != null) {
           try {
               dc.close();
           } catch (NamingException e) {
               System.out.println("NamingException in close():" + e);
           }
       }
       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.85:389/");
       env.put(Context.SECURITY_AUTHENTICATION, "simple");
       env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
       env.put(Context.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();
           } catch (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.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();
           System.out.println("Exception in delete():" + e);
       }
   }
清蒸豆豆 2013-05-30
  • 打赏
  • 举报
回复
两年前做过一点OpenLDAP的东西,楼主说的应该可以实现, 给你推荐一个我当时学习时的一个地方: http://blog.csdn.net/njchenyi/article/details/2051293 这位博主的资料挺好的。
hoseli008 2013-05-30
  • 打赏
  • 举报
回复
引用 1 楼 mantou0611 的回复:
两年前做过一点OpenLDAP的东西,楼主说的应该可以实现, 给你推荐一个我当时学习时的一个地方: http://blog.csdn.net/njchenyi/article/details/2051293 这位博主的资料挺好的。
首先感谢这位有心人。 那位博主的资料例子写的是从已有的schema定义中获取信息,我想: 用JAVA代码生成SCHEMA(java创建SCHEMA文件,添加objectClass和Attribute、Attribute的类型、描述、匹配规则等,如: EQUALITY numericStringMatch SUBSTR numericStringSubstringsMatch)? 不知道有实例吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧