首页

通过定义LdapUtil工具类实现JAVA关于LDAP协议目录数据保存和删除等操作代码示例

标签:LdapUtil,LDAP工具类,LDAP协议,轻量目录访问协议,树状层次结构,存储数据     发布时间:2018-10-14   

一、前言

关于基于jdk开发包javax.naming.directory.DirContext、javax.naming.directory.InitialDirContext目录协议定义LdapUtil工具类,对LDAP轻量目录访问协议数据进行保存、删除操作,详情代码示例部分。

二、代码示例

import java.io.File;@b@import java.io.FileWriter;@b@import java.io.IOException;@b@import java.util.ArrayList;@b@import java.util.HashMap;@b@import java.util.Hashtable;@b@import java.util.Iterator;@b@import java.util.ResourceBundle;@b@import java.util.Set;@b@@b@import javax.naming.Context;@b@import javax.naming.NamingException;@b@import javax.naming.directory.BasicAttribute;@b@import javax.naming.directory.BasicAttributes;@b@import javax.naming.directory.DirContext;@b@import javax.naming.directory.InitialDirContext;@b@@b@public class LdapUtil {@b@	@b@	private static final ResourceBundle bundle = ResourceBundle.getBundle("ldap");@b@	private static final String LDAP_USERNAME = bundle.getString("LDAP_USERNAME");@b@	private static final String LDAP_PASSWORD = bundle.getString("LDAP_PASSWORD");@b@	private static final String LDAP_IP = bundle.getString("LDAP_IP");@b@	private static final String LDAP_PORT = bundle.getString("LDAP_PORT");@b@	private static final String LDAP_ROOT = bundle.getString("LDAP_ROOT");@b@	private static final String LDAP_OBJCLASS_NAME = bundle.getString("LDAP_OBJCLASS_NAME");@b@	private static final String LDAP_ERROR_LOG_PATH = bundle.getString("LDAP_ERROR_LOG_PATH");@b@	@b@	/**@b@	 * LDAP保存一条数据方法,字段名和值由hash来传入,hash的第一个键值对必须是dn@b@	 * @param hash@b@	 * @return@b@	 */@b@	public static HashMap save(HashMap hash){@b@		Hashtable env = new Hashtable();@b@		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");@b@		env.put(Context.PROVIDER_URL, "ldap://" + LDAP_IP + ":" + LDAP_PORT + "/" + LDAP_ROOT);@b@		env.put(Context.SECURITY_AUTHENTICATION, "simple");@b@		env.put(Context.SECURITY_PRINCIPAL, "cn=" + LDAP_USERNAME + "," + LDAP_ROOT);@b@		env.put(Context.SECURITY_CREDENTIALS, LDAP_PASSWORD);@b@		HashMap ret = new HashMap();@b@		ArrayList array = new ArrayList();@b@		DirContext ctx = null;@b@		String dn = "";@b@		StringBuffer errors = new StringBuffer();@b@		try {@b@			ctx = new InitialDirContext(env);@b@			System.out.println("ldap连接成功");@b@			BasicAttributes attrsbu = new BasicAttributes();@b@			BasicAttribute objclassSet = new BasicAttribute("objectclass");@b@			objclassSet.add(LDAP_OBJCLASS_NAME);@b@			attrsbu.put(objclassSet);@b@			Set s = hash.keySet();@b@			Iterator i = s.iterator();@b@			boolean flag = false;@b@			if(i.hasNext()){@b@				Object obj = i.next();@b@				dn = obj.toString() + "=" + hash.get(obj);@b@				flag = true;@b@			}@b@			while(i.hasNext()){@b@				Object obj = i.next();@b@				attrsbu.put(obj.toString(),hash.get(obj));@b@			}@b@			if(flag){@b@				ctx.createSubcontext(dn, attrsbu);@b@			}@b@			else{@b@				errors.append("没有传入DN");@b@				array.add("没有传入DN");@b@			}@b@		} catch (javax.naming.AuthenticationException e) {@b@			errors.append(e.getMessage());@b@			e.printStackTrace();@b@			array.add(e.getMessage());@b@		} catch (Exception e) {@b@			errors.append("LDAP连接出错:" + e.getMessage());@b@			e.printStackTrace();@b@			array.add("LDAP连接出错:" + e.getMessage());@b@		}@b@		if (ctx != null) {@b@			try {@b@				ctx.close();@b@			}@b@			catch (NamingException e) {@b@				e.printStackTrace();@b@			}@b@		}@b@		if(!errors.toString().equals("")){@b@			File folder = new File(LDAP_ERROR_LOG_PATH);@b@			if(folder.exists() || folder.mkdirs()){@b@				File file = new File(LDAP_ERROR_LOG_PATH + "\\" + TimeUtil.getCurrentDateTime("-","-") + ".log");@b@				FileWriter fw = null;@b@				try {@b@					fw = new FileWriter(file);@b@					fw.write(dn + " -----> " + errors.toString());@b@				} catch (IOException e) {@b@					e.printStackTrace();@b@				}@b@				finally{@b@					try {@b@						fw.close();@b@					} catch (IOException e) {@b@						e.printStackTrace();@b@					}@b@				}@b@			}@b@		}@b@		ret.put(dn,array);@b@		return ret;@b@	}@b@	@b@	public static HashMap delete(String dn){@b@		Hashtable env = new Hashtable();@b@		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");@b@		env.put(Context.PROVIDER_URL, "ldap://" + LDAP_IP + ":" + LDAP_PORT + "/" + LDAP_ROOT);@b@		env.put(Context.SECURITY_AUTHENTICATION, "simple");@b@		env.put(Context.SECURITY_PRINCIPAL, "cn=" + LDAP_USERNAME + "," + LDAP_ROOT);@b@		env.put(Context.SECURITY_CREDENTIALS, LDAP_PASSWORD);@b@		HashMap ret = new HashMap();@b@		ArrayList array = new ArrayList();@b@		DirContext ctx = null;@b@		StringBuffer errors = new StringBuffer();@b@		try {@b@			ctx = new InitialDirContext(env);@b@			ctx.destroySubcontext(dn);@b@		} catch (javax.naming.AuthenticationException e) {@b@			errors.append(e.getMessage());@b@			e.printStackTrace();@b@			array.add(e.getMessage());@b@		} catch (Exception e) {@b@			errors.append("LDAP连接出错:" + e.getMessage());@b@			e.printStackTrace();@b@			array.add("LDAP连接出错:" + e.getMessage());@b@		}@b@		if (ctx != null) {@b@			try {@b@				ctx.close();@b@			}@b@			catch (NamingException e) {@b@				e.printStackTrace();@b@			}@b@		}@b@		if(!errors.toString().equals("")){@b@			File folder = new File(LDAP_ERROR_LOG_PATH);@b@			if(folder.exists() || folder.mkdirs()){@b@				File file = new File(LDAP_ERROR_LOG_PATH + "\\" + TimeUtil.getCurrentDateTime("-","-") + ".log");@b@				FileWriter fw = null;@b@				try {@b@					fw = new FileWriter(file);@b@					fw.write(dn + " -----> " + errors.toString());@b@				} catch (IOException e) {@b@					e.printStackTrace();@b@				}@b@				finally{@b@					try {@b@						fw.close();@b@					} catch (IOException e) {@b@						e.printStackTrace();@b@					}@b@				}@b@			}@b@		}@b@		ret.put(dn,array);@b@		return ret;@b@	}@b@	@b@	public static void main(String [] args){ @b@		@b@		LdapUtil.delete("loginname=wjm");@b@	}@b@}