使用java jndi连接ldaps服务器

使用java jndi连接ldaps服务器,通过base64的一个字符串证书,该如何连接

看这个https://blog.csdn.net/woloqun/article/details/90212763

照以下步骤进行操作:

将Base64字符串证书解码为字节数组。
您可以使用Java的Base64类来解码Base64字符串。示例代码:

String base64Certificate = "Your_Base64_Certificate_String";
byte[] certificateBytes = Base64.getDecoder().decode(base64Certificate);

创建一个包含连接参数的Hashtable对象。
在JNDI中,您需要提供连接参数,例如服务器地址、端口和证书等。示例代码:

Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://your_ldaps_server:port");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "your_username");
env.put(Context.SECURITY_CREDENTIALS, "your_password");
env.put("java.naming.ldap.factory.socket", "your_custom_socket_factory_class_name");
env.put("java.naming.ldap.factory.socket", "your_custom_socket_factory_class_name");
env.put("javax.net.ssl.trustStore", "path_to_truststore_file");
env.put("javax.net.ssl.trustStorePassword", "truststore_password");
env.put("javax.net.ssl.keyStore", "path_to_keystore_file");

您需要将以下值替换为实际的连接参数:

your_ldaps_server:port:您的LDAPS服务器地址和端口号。
your_username:您的用户名。
your_password:您的密码。
your_custom_socket_factory_class_name:如果您使用自定义的Socket Factory类,请提供其完整类名。否则,您可以忽略这两行。
path_to_truststore_file和truststore_password:如果您使用自定义的信任库,请提供其路径和密码。否则,您可以忽略这两行。
创建一个InitialDirContext对象并使用前面创建的Hashtable对象进行初始化。
如果连接成功,将创建一个InitialDirContext对象,您可以使用它执行LDAP操作。

  • 这篇博客: [Java安全]JNDI注入学习中的 LDAP的限制 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 在JDK 8u191、7u201、6u211之后,com.sun.jndi.ldap.object.trustURLCodebase属性的默认值被设置为false,对LDAP Reference远程工厂类的加载增加了限制。

    所以,当JDK版本介于8u191、7u201、6u211与6u141、7u131、8u121之间时,我们就可以利用LDAP+Reference的技巧来进行JNDI注入的利用。

    8u191之后有师傅提出了绕过,这个以后再谈了。