JNDI使用Ldap访问AD

package test;

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TestJndiLdap {
    public static void main(String[] args) {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.0.23:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "[email protected]");
        // env.put(Context.SECURITY_PRINCIPAL, "cn=administrator,cn=Users,dc=lcl,dc=com");
        env.put(Context.SECURITY_CREDENTIALS, "1234abcd@");
        InitialContext context = null;
        try {
            context = new InitialContext(env);
            System.out.println("user check ok");
        } catch (AuthenticationException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        } finally {
            try {
                context.close();
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    }
}

 

你可能感兴趣的:(Security,sun)