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);

}

}

}

[2870 byte] By [abacaxia] at [2007-10-3 8:09:18]
# 1
Duh.You're going to kick yourself !The correct LDAP port is 389, not 398.
adler_stevena at 2007-7-15 3:13:32 > top of Java-index,Core,Core APIs...
# 2
[ kicking self now ]Thanks for the reply.
abacaxia at 2007-7-15 3:13:32 > top of Java-index,Core,Core APIs...
# 3

I now have port 389 open on the AD server instead of 398.

However, I am still getting an error that seems related to the server...

VerifyPassword example failed.

javax.naming.CommunicationException: simple bind failed: ADserver:389 [Root exception is javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake]

abacaxia at 2007-7-15 3:13:32 > top of Java-index,Core,Core APIs...
# 4

It seems as though you are trying to initiate a SSL session over port 389.

By default, Active Directory supports unencrypted sessions over ports 389 and 3268, and encrypted (SSL) connections over port 636 and 3269.

Make sure you have not inadvertently specified in your client app that you are using ldaps (LDAP over SSL)

Either an error in your LDAP URL, or an error in your Context properties.

adler_stevena at 2007-7-15 3:13:32 > top of Java-index,Core,Core APIs...