Die Bedeutung des folgenden Beispielcodes ist nicht wichtig, was ich fragen möchte ist, wie man den "certFileName" als Parameter in SocketFactory sendet, denn ich muss ein anderes certFile verwenden in den verschiedenen endPoint dankWie man einen Parameter an die SocketFactory in Ldap sendet
public class LdapConnection
{
private String host = "1.2.3.4"; //the correct ip...
private String baseDn = "dc=x,dc=y,dc=com"; //the correct base DN
private String username = "myUsername";
private String password = "myPassword";
private String connectionUrl = null;
public void connectLdaps() throws Exception
{
connectionUrl = "ldaps://" + host + "/" + baseDn;
System.out.println("Trying to connect to " + connectionUrl + " using LDAPS protocol");
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put("java.naming.ldap.derefAliases", "finding");
env.put(Context.PROVIDER_URL, connectionUrl);
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put("java.naming.ldap.factory.socket", MySocketFactory.class.getName());
new InitialLdapContext(env, null);
System.out.println("Connected successfully!");
}
public static void main(String[] args) throws Exception
{
LdapConnection ldapConnection = new LdapConnection();
ldapConnection.connectLdaps();
}
}
public class MySocketFactory extends SocketFactory
{
private static MySocketFactory instance = null;
private SSLContext sslContext = null;
//I want to send this var from outside
**private static String certFileName = "C:\\certs\\cert_by_hostname.cer";**
public static SocketFactory getDefault()
{
if (instance == null)
{
try
{
instance = new MySocketFactory();
instance.initFactory();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Returning null socket factory");
}
}
return instance;
}
}
wie auf diese Weise sein: was soll ich in der "Außenseite" Code tun
public class MySocketFactory extends SocketFactory
{
private MySocketFactory instance = null;
private SSLContext sslContext = null;
//I want to send this var from outside
private String certFileName = null
public MySocketFactory(String varCert)
{
certFileName = varCert;
}
}
dank!
@TerryGardner, warum entfernen Sie weiterhin 'ldap' Tags (siehe [diese Frage auf Meta] (http://meta.stackexchange.com/q/118827/148833))? – Bruno
@Bruno Einige dieser Entfernungen machen Sinn, z. Wenn es um die Frage geht, wie man einen bestimmten Server wie OpenLDAP konfiguriert, und hat keine Relevanz für LDAP an sich. Dies ist ein weiterer vernünftiger Fall: Es handelt sich wirklich um die Interaktion zwischen JSSE und JNDI, überhaupt nichts mit LDAP * per se * zu tun. Ich denke, ich habe die Tags zu dieser Frage auch selbst bearbeitet. – EJP
@ downvoter bitte erklären. Sonst lernt niemand etwas. – EJP