怎么在LDAP中向SocketFactory发送参数

原学程将引见若何在LDAP中向SocketFactory收送参数的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

成绩描写

上面示例代码的意思其实不主要,我想问的是怎样将传进的certFileName作为参数收送给socketFactory,由于我须要对于分歧的端面应用分歧的certFileName,感谢

public class LdapConnection
{

 private String host = "一.二.三.四"; //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:certscert_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收送参数的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。

0
没有账号?注册  忘记密码?