Search code examples
javaldapjndi

searching(selecting) any data from LDAP server using JNDI


I want to select(search) any kind of data from my LDAP server. I have a connection but I don't know how to retrieve any data. Can anyone give a tip so that I can see anything such as data?

Issue:

When I run the following code, I got:

Exception in thread "main" java.lang.ClassCastException: com.sun.jndi.ldap.LdapCtx cannot be cast to javax.activation.DataSource`

What I tried?

DataSource has two imports:

  • 1) javax.sql
  • 2) javax.activation

I changed different import but didn't work.

My another question is " Are those two imports the same? "

Source code:

public class LDAPJndi {

    //JNDI API를 사용하여 서버와 연결 (Connecting to LDAP server using JNDI)
    public static  DirContext connectJndi() throws NamingException {

        Hashtable<String, String> env = new Hashtable<String, String>(); 
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.0.60:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=govmanager");
        env.put(Context.SECURITY_CREDENTIALS, "GOVmoi!manager");
        DirContext ctx = new InitialDirContext(env); 
        return ctx;
    } 

    public static void lookupJndi() throws NamingException {
        // a method for looking up any data
        DirContext ctx = connectJndi();
        Object o = ctx.lookup("c=kr");
        System.out.println(o);
        //above didn't work, so tried the bottom
        DataSource dataSource = (DataSource) ctx.lookup("c=kr"); //c=kr is BaseDN
        System.out.println(dataSource);
    }
}

Solution

  • I solved the problem like below. I just added many comments to experiment. They might confuse some people, so you may delete them as you wish. And I think the recommended imports by Eclipse work the same in this case. Hope it helped!

    public static  DirContext connectJndi() throws NamingException {
    
        Hashtable<String, String> env = new Hashtable<String, String>(); 
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://192.168.0.60:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "your id");
        env.put(Context.SECURITY_CREDENTIALS, "your password");
        //DirContext ctx = new InitialDirContext(env); 
        LdapContext ctx = new InitialLdapContext(env,null);
        return ctx;
    } 
    
    
    public static void lookupJndi() throws NamingException {
        //DirContext ctx = connectJndi();
        LdapContext ctx = (LdapContext) connectJndi();
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        //ctls.setReturningAttributes(new String[] {"cn"});
        //String searchFilter=String.format("(cn=%s)", "cn" );
        String searchFilter="(objectClass=*)";
        //String searchFilter="(o=*)";
    
        NamingEnumeration<javax.naming.directory.SearchResult> results
         = ctx.search("c=kr", searchFilter,ctls);
        while(results.hasMoreElements()){
            javax.naming.directory.SearchResult sr = results.next();
            Attributes attrs = sr.getAttributes();
            //System.out.println(sr);
            System.out.println(attrs);
        }   
    } // method