I am trying to find the way to connect my program to my LDAPS Netscape
Server, but I cannot find the way to do it...
This is my code...
import netscape.ldap.*;
public class SSLSample {
String host;
String user;
String password;
public static final int SSL_PORT = 636;
public static final String FILTER = "&(objectclass=inetOrgPerson)(cn=";
public static final String BASEDN = "o=mycompany";
SSLSample(String h, String u, String p){
host = h;
user = u;
password = p;
}
void run(){
LDAPConnection ld = null;
String dn = null;
try {
ld = new LDAPConnection(new LDAPSSLSocketFactory());
//
ld.connect(host, SSL_PORT);
ld.getSocketFactory().makeSocket(host, SSL_PORT);
String filter = FILTER + user + ")";
LDAPSearchResults res = ld.search (BASEDN, LDAPv2.SCOPE_SUB,
filter, null, false);
if (res != null && res.hasMoreElements ()){
LDAPEntry entry = res.next();
dn = entry.getDN();
}
ld.authenticate(dn, password);
System.out.println("User authenticated: " + dn);
} catch(LDAPException e){
e.printStackTrace();
} catch(Exception e2){
e2.printStackTrace();
} finally {
try {
ld.finalize();
} catch (Exception ex) { }
}
}
public static void main(String [] args){
if (args.length != 3){
System.out.println("Usage: SSLSample <server> <user> <passwd>");
System.exit(-1);
}
SSLSample s = new SSLSample(args[0], args[1], args[2]);
try {
s.run();
} catch (Exception e){
e.printStackTrace();
}
}
}
And this is my execution result:
c:\>java SSLSample server user passwd
netscape.ldap.LDAPException: Failed to create SSL socket (91); Cannot
connect to
the LDAP server
at
netscape.ldap.LDAPSSLSocketFactory.makeSocket(LDAPSSLSocketFactory.ja
va:309)
at SSLSample.run(SSLSample.java:26)
at SSLSample.main(SSLSample.java:57)
Here are a few tips:
- You need a real SSL provider. LDAPSSLSocketFactory assumes
netscape.net.SSLSocket which is in Communicator, unless you pass in the
name of a class. You can use the org.mozilla.jss.ssl.SSLSocket class
that comes with JSS (get it from
http://www.mozilla.org/projects/security/pki/jss/). Or use
netscape.ldap.factory.JSSESocketFactory along with the reference
implementation of JSSE from Sun (included in JDK 1.4, a separate
download before that). JSS is much faster than JSSE. In both cases
you'll need key and certificate files. For JSS you can copy over key3.db
and cert7.db from Communicator.
- No need to do ld.getSocketFactory().makeSocket. That method is called
internally.
- Make sure your server really is running SSL. You can check in the
error log to see if an SSL port was enabled when the server started.
Rob