public LdapContext connetLDAP() throws NamingException {
// 连接Ldap需要的信息
String ldapFactory = "com.sun.jndi.ldap.LdapCtxFactory";
String ldapUrl = "ldap:/IP:port";// url
String ldapAccount = "cn=root"; // 用户名
String ldapPwd = "password";//密码
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, ldapFactory);
// LDAP server
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ldapAccount);
env.put(Context.SECURITY_CREDENTIALS, ldapPwd);
env.put("java.naming.referral", "follow");
LdapContext ctxTDS = new InitialLdapContext(env, null);
return ctxTDS;
}