本文介紹了如何在LDAP中向SocketFactory發送參數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
下面示例代碼的意思并不重要,我想問的是如何將傳入的certFileName作為參數發送給socketFactory,因為我需要對不同的端點使用不同的certFileName,謝謝
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;
}
}
可能是這樣的:我應該在”外部”代碼中做什么
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;
}
}
謝謝!
推薦答案
您根本不需要編寫套接字工廠。請參閱JSSE參考指南。只需設置適當的系統屬性。
這篇關于如何在LDAP中向SocketFactory發送參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,