Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1374
  • Last Modified:

javax.naming.AuthenticationException

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
dhulipala
Asked:
dhulipala
  • 7
  • 6
  • 3
1 Solution
 
dhulipalaAuthor Commented:
I AM SORRY I TYPED THE TITLE WRONG IT IS  javax.naming.AuthenticationException
0
 
objectsCommented:
check the authentication details are correct
0
 
dhulipalaAuthor Commented:


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

thanks
-ravi
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
that the username and password you are supplying are correct
0
 
dhulipalaAuthor Commented:


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
 
objectsCommented:
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
 
kupra1Commented:
replace ur adminName:
>>>CN=my username,CN=Users,DC=xxx,DC=xx,DC=xx,DC=edu

by
adminName=Administrator@mydc.antipodes.com
0
 
kupra1Commented:
I meant use
adminName=myusername@xxx.xx.xx.edu
0
 
dhulipalaAuthor Commented:
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
 
kupra1Commented:
So, you are not getting the authentication exception anymore.
What adminName are you using?
0
 
dhulipalaAuthor Commented:
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
 
kupra1Commented:
kool... so your authentication problem has been resolved. Now, you are getting this NameNotFoundException because your searchBase is empty.
0
 
dhulipalaAuthor Commented:
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
 
kupra1Commented:
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
 
dhulipalaAuthor Commented:

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

thanks again
ravi.
0
 
kupra1Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now