gpt4 book ai didi

java - 自签名证书的 HttpClient 4.1.x 错误 (javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated )

转载 作者:行者123 更新时间:2023-11-28 22:31:38 27 4
gpt4 key购买 nike

我正在尝试使用 clientAuth=true 的自签名证书配置 Tomcat6,然后使用 HttpClient 4.1.x 在客户端调用 Tomcat 服务器。

我按照 http://virgo47.wordpress.com/2010/08/23/tomcat-web-application-with-ssl-client-certificates/ 中提供的说明进行操作& 当我从浏览器或 openssl 客户端测试时,它工作正常(我运行命令“openssl s_client -cert client.crt -key client.key -CAfile ca.crt -connect localhost:8443”)。

我面临的问题是 HttpClient。我写了下面的代码来创建HttpClient

    private DefaultHttpClient creatHttpClient(KeyStore keyStore, 
char[] keyStorePassword, KeyStore trustStore, char[] trustStorePassword) {
try {

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore); // This contains CA certs
TrustManager[] tm = trustManagerFactory.getTrustManagers();

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, keyStorePassword); // This contain client private key
KeyManager[] km = keyManagerFactory.getKeyManagers();

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(km, tm, new SecureRandom());

SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 30000);

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https",
443, sslSocketFactory));

ClientConnectionManager clientConnectionManager =
new ThreadSafeClientConnManager(schemeRegistry);

final DefaultHttpClient httpClient = new DefaultHttpClient(
clientConnectionManager, params);
return httpClient;
} catch(Exception e) {
throw e;
}
}

为此,我收到了

    javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

我启用了调试

    static
{ System.setProperty("javax.net.debug", "ssl,handshake,trustmanager"); }

我收到了以下调试日志

    main, handling exception: java.lang.RuntimeException: Unexpected error:                      java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
main, SEND TLSv1 ALERT: fatal, description = internal_error
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
main, IOException in getSession(): javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
main, called close()
main, called closeInternal(true)
main, called close()
main, called closeInternal(true)

在这种情况下可能出了什么问题?我不想忽略 ssl 证书错误,而是想正确进行身份验证。

最佳答案

我解决了我自己的问题。问题是我正在使用

加载 keystore 和信任库
InputStream ksin = this.getClass().getResourceAsStream("client.jks");
InputStream tsin = this.getClass().getResourceAsStream("cacerts.jks");

& 在 Junit 测试中使用了这些行,但无法找到 .jks 文件。

我通过输入以下内容来解决这个问题

if(0 == keyStore.size()) { throw new RuntimeException("Keystore is empty"); }
if(0 == trustStore.size()) { throw new RuntimeException("Truststore is empty"); }

在 keystore 和信任库初始化之后。

所以,我将行更改为

InputStream ksin = Thread.currentThread().getContextClassLoader().getResourceAsStream("client.jks");
InputStream tsin = Thread.currentThread().getContextClassLoader().getResourceAsStream("cacerts.jks");

&一切正常。

关于java - 自签名证书的 HttpClient 4.1.x 错误 (javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164831/

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