Java验证Ldap账号和读取Ldap中所有用户

一、验证Ldap用户

public class LDAPAuthentication{    
    private final String URL = "ldap://192.168.1.205:389/";
    private final String BASEDN = "cn=demo1,dc=sys,dc=com";  // 根据自己情况进行修改
    private final String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private LdapContext ctx = null;
    private final Control[] connCtls = null;
  
    private void LDAP_connect() {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        env.put(Context.PROVIDER_URL, URL + BASEDN);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        String root = "cn=demo1,dc=sys,dc=com";  // 根,根据自己情况修改
        env.put(Context.SECURITY_PRINCIPAL, root);   // 管理员
        env.put(Context.SECURITY_CREDENTIALS, "123456");  // 管理员密码
         
        try {
            ctx = new InitialLdapContext(env, connCtls);
            System.out.println( "认证成功" ); 
            System.out.println(ctx);
             
        } catch (javax.naming.AuthenticationException e) {
            System.out.println("认证失败:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("认证出错:");
            e.printStackTrace();
        }
         
        if (ctx != null) {
            try {
                ctx.close();
            }
            catch (NamingException e) {
                e.printStackTrace();
            }
 
        }
    }
}

二、读取Ldap用户信息

public class demo {

	public static void main(String[] args) {
		readLdap();
	}
	
	public static void readLdap(){

		Hashtable<String, String> env = new Hashtable<String, String>();
		DirContext ctx = null;
		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://" + "192.168.1.205" + ":" + "389");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, "CN=" + "admin" + "," + "dc=sys,dc=com");
		env.put(Context.SECURITY_CREDENTIALS, "123456");
		try {
			ctx = new InitialDirContext(env);
		} catch (NamingException e) {
			e.printStackTrace();
			return;
		}
		
		Map<String,String> map = new HashMap<String, String>();
		try {
			if(ctx != null){
				NamingEnumeration<NameClassPair> list = ctx.list("dc=sys,dc=com");
				while(list.hasMore()){
					NameClassPair ncp = list.next();
					String cn = ncp.getName();
					if(cn.indexOf("=") != -1){
						int index = cn.indexOf("=");
						cn = cn.substring(index + 1,cn.length());
						map.put(cn, ncp.getNameInNamespace());
					}
				}
			}
		} catch (NamingException e) {
			e.printStackTrace();
			return;
		}
		
		try {
			if(ctx != null)
				ctx.close();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
		Iterator<Entry<String,String>> it = map.entrySet().iterator();
		while(it.hasNext()){
			Entry<String,String> entry = it.next();
			System.out.println("Key:"+entry.getKey());
			System.out.println("Value:"+entry.getValue());
		}
	}
}

猜你喜欢

转载自blog.csdn.net/aliaichidantong/article/details/80016449