searching(selecting) any data from LDAP server using JNDI












2















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









share|improve this question

























  • I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

    – Jin Lee
    Nov 16 '18 at 5:40








  • 1





    Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

    – EricLavault
    Nov 16 '18 at 10:44











  • @EricLavault I c. okay. I'll do that~

    – Jin Lee
    Nov 16 '18 at 11:12











  • are you using any framework ? like f.e. spring ?

    – user3529850
    Nov 16 '18 at 11:41











  • @user3529850 I'm using JNDI API (for LDAP connection) no Spring though

    – Jin Lee
    Nov 18 '18 at 22:41
















2















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









share|improve this question

























  • I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

    – Jin Lee
    Nov 16 '18 at 5:40








  • 1





    Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

    – EricLavault
    Nov 16 '18 at 10:44











  • @EricLavault I c. okay. I'll do that~

    – Jin Lee
    Nov 16 '18 at 11:12











  • are you using any framework ? like f.e. spring ?

    – user3529850
    Nov 16 '18 at 11:41











  • @user3529850 I'm using JNDI API (for LDAP connection) no Spring though

    – Jin Lee
    Nov 18 '18 at 22:41














2












2








2








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









share|improve this question
















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






java ldap jndi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 5:06









atline

3,159102138




3,159102138










asked Nov 16 '18 at 0:46









Jin LeeJin Lee

194213




194213













  • I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

    – Jin Lee
    Nov 16 '18 at 5:40








  • 1





    Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

    – EricLavault
    Nov 16 '18 at 10:44











  • @EricLavault I c. okay. I'll do that~

    – Jin Lee
    Nov 16 '18 at 11:12











  • are you using any framework ? like f.e. spring ?

    – user3529850
    Nov 16 '18 at 11:41











  • @user3529850 I'm using JNDI API (for LDAP connection) no Spring though

    – Jin Lee
    Nov 18 '18 at 22:41



















  • I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

    – Jin Lee
    Nov 16 '18 at 5:40








  • 1





    Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

    – EricLavault
    Nov 16 '18 at 10:44











  • @EricLavault I c. okay. I'll do that~

    – Jin Lee
    Nov 16 '18 at 11:12











  • are you using any framework ? like f.e. spring ?

    – user3529850
    Nov 16 '18 at 11:41











  • @user3529850 I'm using JNDI API (for LDAP connection) no Spring though

    – Jin Lee
    Nov 18 '18 at 22:41

















I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

– Jin Lee
Nov 16 '18 at 5:40







I solved this problem by using 'SearchControls' 'searchFilter' 'setSearchScope' 'SearchResult'. Thank you~

– Jin Lee
Nov 16 '18 at 5:40






1




1





Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

– EricLavault
Nov 16 '18 at 10:44





Hi, if you think you got an answer then write it as an answer with a few explanations and accept it, so that your post gains some visibility and people having the same issue can benefit from it.

– EricLavault
Nov 16 '18 at 10:44













@EricLavault I c. okay. I'll do that~

– Jin Lee
Nov 16 '18 at 11:12





@EricLavault I c. okay. I'll do that~

– Jin Lee
Nov 16 '18 at 11:12













are you using any framework ? like f.e. spring ?

– user3529850
Nov 16 '18 at 11:41





are you using any framework ? like f.e. spring ?

– user3529850
Nov 16 '18 at 11:41













@user3529850 I'm using JNDI API (for LDAP connection) no Spring though

– Jin Lee
Nov 18 '18 at 22:41





@user3529850 I'm using JNDI API (for LDAP connection) no Spring though

– Jin Lee
Nov 18 '18 at 22:41












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329878%2fsearchingselecting-any-data-from-ldap-server-using-jndi%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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





    share|improve this answer






























      1














      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





      share|improve this answer




























        1












        1








        1







        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





        share|improve this answer















        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 11:35

























        answered Nov 16 '18 at 11:17









        Jin LeeJin Lee

        194213




        194213
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329878%2fsearchingselecting-any-data-from-ldap-server-using-jndi%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma