建立LDAP服务器的连接
package com.prime.mypackage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LdapQuery {
/*服务提供者*/
private static String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
/*LDAP连接*/
private DirContext dirContext;
/*参数列表*/
private Hashtable enviroment;
/**
* 构造函数
*/
public LdapQuery() {
dirContext = null;
enviroment = new Hashtable();
}
public static void main(String[] args){
LdapQuery lp=new LdapQuery();
try{
lp.init("cn=orcladmin","abc123");
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 读取配置文件,连接LDAP服务器
* @throws LdapException
*/
public DirContext init(String username, String password)
throws Exception {
try {
Properties config = new Properties();
File f = new File("C:/Projects/Java/ldap.property");
if (!f.exists()) {
throw new Exception("没发现配置文件");
}
FileInputStream configFile = new FileInputStream(f);
config.load(configFile);
String host = config.getProperty("host");
String port = config.getProperty("port");
configFile.close();
enviroment.put(Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY);
enviroment.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
if (password != null) {
enviroment.put(Context.SECURITY_AUTHENTICATION, "simple");
enviroment.put(Context.SECURITY_PRINCIPAL, username);
enviroment.put(Context.SECURITY_CREDENTIALS, password);
}
dirContext = new InitialDirContext(enviroment);
if (dirContext != null) {
System.out.println("Connect");
return dirContext;
}
return null;
} catch (Exception e) {
throw new Exception("LdapQuery.init:" + e.toString());
}
}
}
做第一个动作 add()
package com.prime.mypackage;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.naming.directory.DirContext;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls ;
import javax.naming.NamingEnumeration;
import javax.naming.directory.SearchResult;
import javax.naming.directory.Attributes ;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.ModificationItem;
import java.lang.reflect.Method;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class LdapAction
{
DirContext ctx=null;
public static void main(String[] args)
{
LdapAction LA=new LdapAction();
LA.add();
}
public LdapAction()
{
LdapQuery query=new LdapQuery();
try{
ctx=query.init("cn=orcladmin","abc123");
}catch(Exception e)
{
e.printStackTrace();
}
}
public void add(){
try{
String newUserName = "test_add";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
BasicAttribute pass=new BasicAttribute("userpassword");
pass.add("123qweasd");
objclassSet.add("top");
objclassSet.add("person");
objclassSet.add("organizationalPerson");
objclassSet.add("inetOrgPerson");
objclassSet.add("orcluser");
objclassSet.add("orcluserV2");
attrs.put(pass);
attrs.put(objclassSet);
attrs.put("sn", newUserName);
attrs.put("uid", newUserName);
attrs.put("cn", newUserName);
ctx.createSubcontext("uid=" + newUserName+",cn=users,dc=dev,dc=daphne,dc=com,dc=cn", attrs);
}catch(Exception e){
System.out.println("Exception in add():"+e);
}
}
}
待叙~