gpt4 book ai didi

java - 我的带有内置 ssl 证书的库如何也允许使用默认证书

转载 作者:太空狗 更新时间:2023-10-29 22:41:35 32 4
gpt4 key购买 nike

我正在为内部客户端分发一个库 jar,该库包含一个证书,用于调用我们网络内部的服务。

信任管理器设置如下

    TrustManagerFactory trustManagerFactory = 
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keystore = KeyStore.getInstance("JKS");
InputStream keystoreStream =
clazz.getClassLoader().getResourceAsStream("certs.keystore"); // (on classpath)
keystore.load(keystoreStream, "pa55w0rd".toCharArray());
trustManagerFactory.init(keystore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext context = SSLContext.getInstance("SSL");
context.init(null, trustManagers, null);

SSLSocketFactory socketFact = context.getSocketFactory();
connection.setSSLSocketFactory(socketFact);

除了用户需要其他证书或默认证书的情况外,所有这些都可以正常工作。

我试过了 Registering multiple keystores in JVM没有运气(我无法为我的案例概括它)

如何在使用我的证书的同时允许用户库也使用他们自己的证书?

最佳答案

您正在配置与充当信任库(您信任的服务器证书)的自定义 keystore 的连接。您没有覆盖默认的 JVM 行为,因此包含您的库的其他应用程序可以建立的其余连接不会受到影响。

因此您不需要多个 keystore 管理器,事实上,您的代码可以完美运行。

我在下面附上了一个完整的示例,其中使用了一个包含 Google 根 CA 的 keystore google.jks,以及一个使用默认 JVM 信任库的连接。这是输出

request("https://www.google.com/", "test/google.jks", "pa55w0rd"); //OK 
request("https://www.aragon.es/", "test/google.jks", "pa55w0rd"); // FAIL sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
request("https://www.aragon.es/", null, null); //OK

问题不在您所附的代码中,因此请检查您的代码中的以下内容:

  • 信任库 certs.keystore 确实在您的类路径中找到

  • Truststore 设置未使用 -Djavax.net.ssl.trustStore

    在 JVM 级别设置
  • 发现的错误(请将其包含在您的问题中)确实与 SSL 连接有关


package test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;


public class HTTPSCustomTruststore {

public final static void main (String argv[]) throws Exception{
request("https://www.google.com/", "test/google.jks", "pa55w0rd"); //Expected OK
request("https://www.aragon.es/","test/google.jks","pa55w0rd"); // Expected FAIL
request("https://www.aragon.es/",null,null); //using default truststore. OK
}

public static void configureCustom(HttpsURLConnection connection, String truststore, String pwd)throws Exception{
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keystore = KeyStore.getInstance("JKS");
InputStream keystoreStream = HTTPSCustomTruststore.class.getClassLoader().getResourceAsStream(truststore);
keystore.load(keystoreStream, pwd.toCharArray());
trustManagerFactory.init(keystore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext context = SSLContext.getInstance("SSL");
context.init(null, trustManagers, new java.security.SecureRandom());

SSLSocketFactory socketFact = context.getSocketFactory();
connection.setSSLSocketFactory(socketFact);
}


public static void request(String urlS, String truststore, String pwd) {
try {
URL url = new URL(urlS);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if (truststore != null) {
configureCustom((HttpsURLConnection) conn, truststore, pwd);
}
conn.connect();

int statusCode = conn.getResponseCode();
if (statusCode != 200) {
System.out.println(urlS + " FAIL");
} else {
System.out.println(urlS + " OK");
}
} catch (Exception e) {
System.out.println(urlS + " FAIL " + e.getMessage());
}
}
}

关于java - 我的带有内置 ssl 证书的库如何也允许使用默认证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359169/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com