来年剃光头 2015-10-29 07:14 采纳率: 0%
浏览 4306

使用Closeablehttpclient.execute,ubuntu下出现以下证书问题

错误log
javax.net.ssl.SSLException: Certificate for <> doesn't match any of the subject alternative names: []
at org.apache.http.conn.ssl.DefaultHostnameVerifier.matchDNSName(DefaultHostnameVerifier.java:158) ~[httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.DefaultHostnameVerifier.verify(DefaultHostnameVerifier.java:109) ~[httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.DefaultHostnameVerifier.verify(DefaultHostnameVerifier.java:87) ~[httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:461) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395) [httpclient-4.5.jar:4.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) [httpclient-4.5.jar:4.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) [httpclient-4.5.jar:4.5]
代码段为:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
httpPost.setEntity(entity);
CloseableHttpResponse resp=null;
try {
resp = httpClient.execute(httpPost);
HttpEntity respEntity = resp.getEntity();
String body = EntityUtils.toString(respEntity, charset);

在执行到 resp = httpClient.execute(httpPost);在ubuntu下开发就会抛出异常,而在windows下开发则没有此类错误,请教大神这是什么问题。
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 14:48
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题是由于Apache HttpClient无法解析Ubuntu系统中的自签名的SSL证书。Ubuntu系统中默认不支持自签名的SSL证书,因此在尝试连接时会遇到错误。

    解决方法如下:

    1. 在Ubuntu系统中安装OpenSSL:sudo apt-get install openssh-server

    2. 创建一个名为ca.crt的新文件,并输入你的CA证书内容,然后将其保存为ca.crt。这将用于生成你的自签名的SSL证书。

    3. 使用以下命令创建你的自签名SSL证书:

      openssl req -newkey rsa:2048 -nodes -x509 -days 365 -out ca.crt -subj "/CN=yourdomain.com"
      
    4. ca.crt添加到系统的SSL/CERTIFICATE_PATH环境变量中(通常位于/etc/ssl/certs/):

      sudo nano /etc/ssl/certificate_paths
      

      添加以下行:

      /etc/ssl/certs/ca.crt
      
    5. 在Ubuntu系统中配置好后,你可以使用以下命令验证你的证书是否有效:

      openssl s_client -connect yourdomain.com:443 -servername yourdomain.com -cert ca.crt -key ca.key
      

    如果你仍然遇到问题,可以考虑使用第三方库如nativelibsodium来处理自签名证书的问题。

    评论

报告相同问题?