?
Solved

javax.naming.AuthenticationException

Posted on 2005-04-03
16
Medium Priority
?
1,369 Views
Last Modified: 2013-11-24
Hi i am trying to retieve user attributes from Windows Active Directory using the following code:

import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;
 
 
public class searchgc      {
      public static void main (String[] args)      {
                        Hashtable envGC = new Hashtable();
             
            String adminName = "CN=Administrator,CN=Users,DC=ANTIPODES,DC=COM";
            String adminPassword = "XXXXXXX";
 
            String urlGC = "ldap://mydc.antipodes.com:389";
            
 
            envGC.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
            
 
            //set security credentials, note using simple cleartext authentication
            envGC.put(Context.SECURITY_AUTHENTICATION,"simple");
            envGC.put(Context.SECURITY_PRINCIPAL,adminName);
            envGC.put(Context.SECURITY_CREDENTIALS,adminPassword);
 
             
            //connect to both a GC and  DC
            envGC.put(Context.PROVIDER_URL,urlGC);
                        
            //We need to chase referrals when retrieving attributes from the DC
            //as the object may be in a different domain
            envDC.put(Context.REFERRAL,"follow");
                  
            try {
 
                  //Create the initial directory context for both DC and GC
                  LdapContext ctxGC = new InitialLdapContext(envGC,null);
                                    
            
                  //Now perform a search against the GC
                  //Create the search controls             
                  SearchControls searchCtls = new SearchControls();
            
                  //Specify the attributes to return
                  String returnedAtts[]={"sn"};
                  searchCtls.setReturningAttributes(returnedAtts);
            
                  //Specify the search scope
                  searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
 
                  //specify the LDAP search filter
                  String searchFilter = "(&(objectClass=user)(mail=*)(|(givenName=Albert)(givenName=Isaac)))";
 
                  //Specify the Base for the search
                  //an empty dn for all objects from all domains in the forest
                  String searchBase = "";
 
                  //initialize counter to total the results
                  int totalResults = 0;
 
 
                  //Search for objects in the GC using the filter
                  NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
 
                  //Loop through the search results
                  while (answer.hasMoreElements()) {
                        SearchResult sr = (SearchResult)answer.next();
 
                        totalResults++;
 
                        System.out.println(">>>" + sr.getName());
 
                        // Print out some of the attributes, catch the exception if the attributes have no values
                        Attributes attrs = sr.getAttributes();
                        if (attrs != null) {
                              try {
                                    System.out.println("   name(GC): " + attrs.get("givenName").get() + " " + attrs.get("sn").get());
                                    System.out.println("   mail(GC): " + attrs.get("mail").get());
                              }
                              catch (NullPointerException e)      {
                                    System.err.println("Problem listing attributes from Global Catalog: " + e);
                              }
                        
                        }
                         
                  }
 
                   System.out.println("Total results: " + totalResults);
                  ctxGC.close();
                  
            }
            catch (NamingException e) {
                  System.err.println("Problem searching directory: " + e);
            }
 
      }      
}

GIVING ME THE FOLLOWING ERROR:

Problem searching directory: javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C09030B, comment: AcceptSecurityContext error, data 525, v893]

Please help me with the problem it is really urgent
thank u
ravi
0
Comment
Question by:dhulipala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 

Author Comment

by:dhulipala
ID: 13690912
I AM SORRY I TYPED THE TITLE WRONG IT IS  javax.naming.AuthenticationException
0
 
LVL 92

Expert Comment

by:objects
ID: 13690924
check the authentication details are correct
0
 

Author Comment

by:dhulipala
ID: 13690928


I am sorry i didnt understand your answer exactly ... what are authentication details...

thanks
-ravi
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13690935
that the username and password you are supplying are correct
0
 

Author Comment

by:dhulipala
ID: 13690960


Actually i have question regarding that... I am tyring to access my attributes from my university server whose URL is of type    .xx.xx.edu
I am a user of the server. I am giving the following details :

String urlGC = "ldap://xxx.xx.xx.edu:389";

String adminName = "CN=my username,CN=Users,DC=xxx,DC=xx,DC=xx,DC=edu";
 String adminPassword = "my password";

Am i doing in the right way or not??

Please help,
ravi
0
 
LVL 92

Expert Comment

by:objects
ID: 13691019
Looks correct (assuming the details you are supplying are correct)

Some background at:
http://java.sun.com/products/jndi/tutorial/ldap/security/ldap.html
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13691083
replace ur adminName:
>>>CN=my username,CN=Users,DC=xxx,DC=xx,DC=xx,DC=edu

by
adminName=Administrator@mydc.antipodes.com
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13691085
I meant use
adminName=myusername@xxx.xx.xx.edu
0
 

Author Comment

by:dhulipala
ID: 13691200
Hi ... What should be the search filterlike ... with
String searchFilter = "(&(objectClass=user)(|(givenName=shashank)(givenName=nagula)))";

I think it is giving this exception:
Problem searching directory: javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100198, problem 2001 (NO_OBJEC
T), data 0, best match of:
        ''
]; remaining name ''


Please help
ravi
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13691219
So, you are not getting the authentication exception anymore.
What adminName are you using?
0
 

Author Comment

by:dhulipala
ID: 13691306
Since i am testing this on my university server(xxx.xx.xx.edu). I am using my account login name as the adminName.
whis is like c5733s5dhulipalar
thanks
ravi.
0
 
LVL 10

Accepted Solution

by:
kupra1 earned 2000 total points
ID: 13691318
kool... so your authentication problem has been resolved. Now, you are getting this NameNotFoundException because your searchBase is empty.
0
 

Author Comment

by:dhulipala
ID: 13691427
Hi good thing is i am not getting any exceptions after giving searchBase ... at the same time i am not getting any result..
None of the System.out.println are printing out after this part:

while (answer.hasMoreElements()) {

                        System.out.println("Ravi:");
                        SearchResult sr = (SearchResult)answer.next();
 
                        totalResults++;
 
                        System.out.println(">>>" + sr.getName());
 
                        // Print out some of the attributes, catch the exception if the attributes have no values
                        Attributes attrs = sr.getAttributes();
                        if (attrs != null) {
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13692364
This is because "answer" is empty. It has got no elements. To verify this, change the "while" to "if" and then add an else block.
if(answer.hasMoreElements()) {
       System.out.println("Answer is not empty");
       //
} else {
       System.out.println("Answer is empty");
}
0
 

Author Comment

by:dhulipala
ID: 13693326

Hi your earlier suggestions in eliminating exceptions and stuff helped a lot in solving the problem ... the code worked

thanks again
ravi.
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13693335
Thanks a lot. I think that you should now look at your other similar open question too.
Thanks for the points again.

Prabhakar
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
A solution for Fortify Path Manipulation.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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