dhulipala
asked on
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=ANTIPO DES,DC=COM ";
String adminPassword = "XXXXXXX";
String urlGC = "ldap://mydc.antipodes.com :389";
envGC.put(Context.INITIAL_ CONTEXT_FA CTORY,"com .sun.jndi. ldap.LdapC txFactory" );
//set security credentials, note using simple cleartext authentication
envGC.put(Context.SECURITY _AUTHENTIC ATION,"sim ple");
envGC.put(Context.SECURITY _PRINCIPAL ,adminName );
envGC.put(Context.SECURITY _CREDENTIA LS,adminPa ssword);
//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,n ull);
//Now perform a search against the GC
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"sn"};
searchCtls.setReturningAtt ributes(re turnedAtts );
//Specify the search scope
searchCtls.setSearchScope( SearchCont rols.SUBTR EE_SCOPE);
//specify the LDAP search filter
String searchFilter = "(&(objectClass=user)(mail =*)(|(give nName=Albe rt)(givenN ame=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("Proble m listing attributes from Global Catalog: " + e);
}
}
}
System.out.println("Total results: " + totalResults);
ctxGC.close();
}
catch (NamingException e) {
System.err.println("Proble m searching directory: " + e);
}
}
}
GIVING ME THE FOLLOWING ERROR:
Problem searching directory: javax.naming.Authenticatio nException : [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
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
String adminPassword = "XXXXXXX";
String urlGC = "ldap://mydc.antipodes.com
envGC.put(Context.INITIAL_
//set security credentials, note using simple cleartext authentication
envGC.put(Context.SECURITY
envGC.put(Context.SECURITY
envGC.put(Context.SECURITY
//connect to both a GC and DC
envGC.put(Context.PROVIDER
//We need to chase referrals when retrieving attributes from the DC
//as the object may be in a different domain
envDC.put(Context.REFERRAL
try {
//Create the initial directory context for both DC and GC
LdapContext ctxGC = new InitialLdapContext(envGC,n
//Now perform a search against the GC
//Create the search controls
SearchControls searchCtls = new SearchControls();
//Specify the attributes to return
String returnedAtts[]={"sn"};
searchCtls.setReturningAtt
//Specify the search scope
searchCtls.setSearchScope(
//specify the LDAP search filter
String searchFilter = "(&(objectClass=user)(mail
//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
System.out.println(" mail(GC): " + attrs.get("mail").get());
}
catch (NullPointerException e) {
System.err.println("Proble
}
}
}
System.out.println("Total results: " + totalResults);
ctxGC.close();
}
catch (NamingException e) {
System.err.println("Proble
}
}
}
GIVING ME THE FOLLOWING ERROR:
Problem searching directory: javax.naming.Authenticatio
Please help me with the problem it is really urgent
thank u
ravi
check the authentication details are correct
ASKER
I am sorry i didnt understand your answer exactly ... what are authentication details...
thanks
-ravi
that the username and password you are supplying are correct
ASKER
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,D
String adminPassword = "my password";
Am i doing in the right way or not??
Please help,
ravi
Looks correct (assuming the details you are supplying are correct)
Some background at:
http://java.sun.com/products/jndi/tutorial/ldap/security/ldap.html
Some background at:
http://java.sun.com/products/jndi/tutorial/ldap/security/ldap.html
replace ur adminName:
>>>CN=my username,CN=Users,DC=xxx,D C=xx,DC=xx ,DC=edu
by
adminName=Administrator@my dc.antipod es.com
>>>CN=my username,CN=Users,DC=xxx,D
by
adminName=Administrator@my
I meant use
adminName=myusername@xxx.x x.xx.edu
adminName=myusername@xxx.x
ASKER
Hi ... What should be the search filterlike ... with
String searchFilter = "(&(objectClass=user)(|(gi venName=sh ashank)(gi venName=na gula)))";
I think it is giving this exception:
Problem searching directory: javax.naming.NameNotFoundE xception: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100198, problem 2001 (NO_OBJEC
T), data 0, best match of:
''
]; remaining name ''
Please help
ravi
String searchFilter = "(&(objectClass=user)(|(gi
I think it is giving this exception:
Problem searching directory: javax.naming.NameNotFoundE
T), data 0, best match of:
''
]; remaining name ''
Please help
ravi
So, you are not getting the authentication exception anymore.
What adminName are you using?
What adminName are you using?
ASKER
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.
whis is like c5733s5dhulipalar
thanks
ravi.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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) {
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) {
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");
}
if(answer.hasMoreElements(
System.out.println("Answer
//
} else {
System.out.println("Answer
}
ASKER
Hi your earlier suggestions in eliminating exceptions and stuff helped a lot in solving the problem ... the code worked
thanks again
ravi.
Thanks a lot. I think that you should now look at your other similar open question too.
Thanks for the points again.
Prabhakar
Thanks for the points again.
Prabhakar
ASKER