【LDAP】Java连接Ldap

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;  
}  

posted @ 2018-03-26 18:54  CryDongle  阅读(40)  评论(0编辑  收藏  举报