日期:2014-05-18 浏览次数:20734 次
/**
* Ldap连接
*
* @throws Exception
*/
private static InitialLdapContext LdapConnect() throws Exception {
try {
String url ="ldap://localhost:389";
String name ="cn=root";
String pwd = "root";
System.out.println(url+" "+name+" "+pwd);
Hashtable hashtable = new Hashtable();
hashtable.put(Context.AUTHORITATIVE, "true");
hashtable.put("com.sun.jndi.ldap.connect.pool", "true");
hashtable.put("java.naming.ldap.version", "3");
hashtable.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
hashtable.put(Context.REFERRAL, "follow");
hashtable.put(Context.PROVIDER_URL, url);
hashtable.put(Context.SECURITY_AUTHENTICATION, "Simple");
hashtable.put(Context.SECURITY_PRINCIPAL, name);
hashtable.put(Context.SECURITY_CREDENTIALS, pwd);
ctx = new InitialLdapContext(hashtable, null);
System.out.println("Get LdapConnect:ctx=" + ctx);
} catch (Exception e) {
System.out.println("LdapConnect Sys Error:" + e.getMessage());
throw new Exception(e.getMessage());
}
return ctx;
}
/**
* 新增
* @param phone 固话
* @param indicate 标示号
*/
public static void getAdd(String phone,String indicate){
try {
Attributes attrs = new BasicAttributes();
Attribute objectClassAttr = new BasicAttribute("objectclass");
objectClassAttr.add("top");
objectClassAttr.add("person");
objectClassAttr.add("ePerson");
objectClassAttr.add("SHTELIndicate");
attrs.put(objectClassAttr);
attrs.put("phone", phone);
attrs.put("cn", phone);
attrs.put("sn", phone);
attrs.put("indicate", indicate);
LdapConnect().createSubcontext("cn=" + phone+ ",ou=indicate,dc=account,dc=mboss,dc=com", attrs);
System.out.println("cg");
} catch (Exception e) {
e.printStackTrace();
}
}