JNDI error
My following piece of code is returning the error:
javax.naming.CommunicationException: ADserver:398 [Root exception is java.net.ConnectException: Connection refused: connect]
I checked with our sysadmin and each of these ports are open. I know the admin & password are correct too. Any ideas?
Code is below:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.NoSuchAttributeException;
import javax.naming.directory.SearchControls;
public class V
{
public static void main( String[] args )
{
String hostURL = "ldap://ADserver:398";
//String hostURL = "ldap://ADserver:636";
String loginDN = "CN=admin,CN=Users,DC=ADserver";
String password = "password";
//String keystore = "C:\\j2sdk1.4.2_12\\jre\\lib\\security";
//System.setProperty("javax.net.ssl.trustStore",keystore);
String userDN= "Joe Baker";
String userPWD = "password";
try {
/* Setup environment properties */
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, hostURL);
env.put(Context.SECURITY_PRINCIPAL, loginDN );
env.put(Context.SECURITY_CREDENTIALS, password );
// env.put(Context.SECURITY_PROTOCOL, "ssl");
// create the initial directory context
DirContext ctx = new InitialDirContext(env);
System.out.println();
System.out.println("User DN: " + userDN );
System.out.println("Verifying " + userDN + "'s passwod...");
// check user's password
// do compare in JNDI:
//1. search scope: SearchControls.OBJECT_SCOPE
//2. return no attributes
//3. set filter to be name-value pair
SearchControls ctls = new SearchControls();
ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
ctls.setReturningAttributes( new String[0] );
NamingEnumeration sre = ctx.search( userDN, "userPassword="
+ userPWD, ctls );
if ( sre != null && sre.hasMoreElements())
System.out.println("password is correct");
else
System.out.println("password is incorrect");
// close the context
ctx.close();
}
catch( NoSuchAttributeException nae ) {
System.err.println("VerifyPassword example failed.");
nae.printStackTrace();
}
catch (NamingException e) {
System.err.println("VerifyPassword example failed.");
e.printStackTrace();
}
finally {
System.exit(0);
}
}
}

