Solved

Active Directory, JNDI, and retrieving Data

Posted on 2003-11-27
3
1,192 Views
Last Modified: 2013-11-24
I am having a very difficult time getting my head around LDAP and how to use JNDI.  I have read all the examples and searched the net, but none of the examples ever work for me, no matter how much I manipulate them.  

I am trying to connect to a Windows Server 2003 and retrieve attributes for a particular user.  My company wants to use the Windows authentication for user's wishing to access employee only websites outside the company.  I need to use JNDI in my servlet to authenticate the user.  

This part I have accomplished (see code below).  But I also want to store/retrieve attributes concerning what privileges the user has (what menu's to display, etc.)  But when I attempt to call getAttributes() an error is thrown.  I am assuming it is my arguments in getAttributes, but I don't know what type of arguments to use what is required for it to work properly.  I have used as many variations as I can think of.  Is there a tool to connect to the Active Directory and explore?  I downloaded an ldap browser, which connects, but I can never get in to see anything of value (it asks for a Base DN which I think is the problem).  I have also tried to do searches which also result in an error of similar nature to the one below.

javax.naming.PartialResultException: [LDAP: error code 10 - 0000202B: RefErr: DSID-031006D9, data 0, 1 access points
      ref 1: 'int.mycompany.com'
]; remaining name 'cn=ngalloway,cn=users,DC=int,DC=mycompany,DC=com'
      at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2780)
      at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2697)
      at com.sun.jndi.ldap.LdapCtx.c_getAttributes(LdapCtx.java:1268)
      at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:213)
      at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:121)
      at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:109)
      at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:121)
      at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:116)
      at com.pason.testapps.ldap.LDAPTest.main(LDAPTest.java:69)


/* The Code */

        try
        {
            Hashtable env = new Hashtable();

            env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
            env.put(Context.PROVIDER_URL, MY_HOST);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, "ngalloway@mycompany.com");
            env.put(Context.SECURITY_CREDENTIALS, "mypassword");

            // Get a reference to a directory context
            System.out.println("Getting InitialDirContext");
            DirContext ctx = new InitialDirContext(env);

            Attributes answer = ctx.getAttributes("cn=ngalloway,cn=users,DC=int,DC=mycompany,DC=com");

            for (NamingEnumeration ae = answer.getAll(); ae.hasMore();)
            {
                Attribute attr = (Attribute)ae.next();
                System.out.print(" Attribute: " + attr.getID());
                //Print each value
                for(NamingEnumeration e = attr.getAll(); e.hasMore();)
                {
                    System.out.println(" Value: " + e.next());
                }
            }
0
Comment
Question by:ngalloway
3 Comments
 
LVL 9

Accepted Solution

by:
nimaig earned 300 total points
Comment Utility
I got the same problem.

Putting  :

env.put(Context.REFERRAL, "follow");


solved the problem.
0
 

Author Comment

by:ngalloway
Comment Utility
Well this lets me authenticate.  My getAttributes just hangs there now and doesn't return.  On to that problem.  Thanks.
0
 

Expert Comment

by:zhentinglu
Comment Utility
The solution doesn't fix my problem. In my case, user try to search from the root and the setting is: authentication.extension.ldap.user.baseDN=dc=dev, dc=com.  The following is the tree looklike.

dc=dev, dc=com  (root)
     USER
         u1

The search returned as "cn=u1, cn=USER".

NamingEnumeration ae = context.search(fBaseDN, queryFilter, s);
SearchResult result = (SearchResult)ae.next();
String userObjectName = result.getName();
if ( ae.hasMore() )              (Failed here and exception happens)
{
     ...................
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Logs to delete in Windows VCenter server ? 11 156
strCount chalenge 3 50
Image decoding from Camera 3 42
maven project error 5 16
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now