java - 使用密钥库 api 的内存密钥库“无法找到到所请求目标的有效证书路径”
问题描述
我正在尝试为单个 ldap 请求创建一个内存密钥库。Ldap 连接和证书可以更改,因此我无法将它们存储在任何地方。
Spring Ldap 对多个 ldap 连接不是很友好
public LdapContextSource buildLdapContext(final LdapConnection connection) {
final LdapContextSource context = new LdapContextSource();
context.setBase(connection.getBaseDN());
context.setUrl(connection.getConnectionUrl());
context.setPassword(connection.getAdminPassword());
context.setUserDn(connection.getUserDN());
if(connection.getProtocol() == LdapProtocol.LDAPS) {
final DefaultTlsDirContextAuthenticationStrategy authenticationStrategy = new DefaultTlsDirContextAuthenticationStrategy();
authenticationStrategy.setSslSocketFactory(ldapSslSocketFactoryBuilder.buildSslSocketFactory(connection));
context.setAuthenticationStrategy(authenticationStrategy);
}
context.afterPropertiesSet();
return context;
}
public SSLSocketFactory buildSslSocketFactory(final LdapConnection connection) {
try {
final KeyStore store = buildKeyStore(connection);
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(store);
final SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, tmf.getTrustManagers(), null);
return ctx.getSocketFactory();
} catch(Exception e) {
throw new LdapException(e.getMessage(), e);
}
}
private KeyStore buildKeyStore(final LdapConnection ldapConnection) {
try {
// Load in-memory keystore
final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null);
// Decode certificate
byte[] decoded = Base64.decodeBase64(ldapConnection.getSslCertificate()
.replaceAll(X509Factory.BEGIN_CERT, "")
.replaceAll(X509Factory.END_CERT, "")
.trim().getBytes(StandardCharsets.UTF_8));
// Load certificate
CertificateFactory certificateFactory = CertificateFactory.getInstance("x.509");
Certificate cert = certificateFactory.generateCertificate(new ByteArrayInputStream(decoded));
keystore.setCertificateEntry(ldapConnection.getConnectionUrl(), cert);
return keystore;
} catch(Exception e) {
log.error(e.getMessage(), e);
throw new LdapException(e.getMessage(), e);
}
}
我希望存储的公钥用于连接到 ldap 服务器,但我得到“无法找到请求目标的有效证书路径”
解决方案
我设法解决了这个问题。该代码正常工作,问题是我们使用的是无法验证的自签名证书。我们所要做的就是将 hte 证书放入 cacerts 文件中。自签名证书是他们自己的证书颁发机构,因此必须放入 cacerts 文件中,该文件位于 $JAVA_HOME/jre/lib/security/cacerts。
推荐阅读
- r - R 中的装袋 - 如何获得装袋回归以便将其与初始模型进行比较?
- javascript - 使用 Async 和 MongoDb 按顺序填充集合文档
- android - 房间查询:在列表中查找总是返回 null
- ffmpeg - 解码/编码 mp4 视频时丢弃 P 帧
- python - python:读取内联标签txt文件并将其格式化为列
- microsoft-cognitive - 如何通过发送到认知服务来防止保留或重复使用数据?
- c# - 我需要有关功能的帮助
- c# - C# SetWinEventHook 在鼠标移动时停止发送消息
- html - 替代样式表未显示在 Firefox 的“查看 > 页面样式”下
- powershell - 如何混合编码和非编码脚本?