1. 程式人生 > >Java驗證Ldap賬號和讀取Ldap中所有使用者

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