java - 为 ssl 身份验证编写 java 客户端

标签 java rest tomcat ssl apache-httpclient-4.x

<分区>

我已经按照这些命令创建了一个 openssl 证书

D:\softwares\apache-tomcat-8.0.24\bin>openssl genrsa -out serverprivatekey.pem 2048

D:\softwares\apache-tomcat-8.0.24\bin>openssl req -new -x509 -key serverprivatekey.pem -out servercert.pem -days 1095

D:\softwares\apache-tomcat-8.0.24\bin>openssl pkcs12 -export -out serverkeystore.pkcs12 -in servercert.pem -inkey serverprivatekey.pem

D:\softwares\apache-tomcat-8.0.24\bin>keytool -importkeystore -srckeystore serverkeystore.pkcs12 -srcstoretype PKCS12 -destkeystore keystore.jks -deststoretype JKS

我配置了tomcat的server.xml文件并启用了HTTPS。当我在网络浏览器上打开该网站时,它运行得非常好。

现在我正在尝试使用 Java8 和 Apache HttpClient 4.5 为此编写一个 Java 客户端。我写了下面的代码

  CloseableHttpClient httpclient = HttpClients.createDefault();
  HttpGet httpGet = new HttpGet("https://localhost:8443/first/rest/hello");
  CloseableHttpResponse response = httpclient.execute(httpGet);
  System.out.println( response.getStatusLine());

我遇到了以下异常。我必须在 java keystore 中添加证书吗?或者是什么 ?我已经这样做了,但是那个 keystore 是从 tomcat 主目录执行的。不知道该怎么办。

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.vogella.jersey.first.Test.usingApacheHttp(Test.java:34)
    at com.vogella.jersey.first.Test.main(Test.java:27)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
    ... 21 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 27 more

最佳答案

您的 Java 客户端信任库不信任服务器证书。您必须:

  1. 将其从服务器 keystore 导出到客户端信任库,或更好
  2. 获得 CA 签名。

关于java - 为 ssl 身份验证编写 java 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442375/

相关文章:

JSF 资源版本控制

java - 如果我尝试通过 DatagramChannel 发送空 UDP 数据包但底层套接字缓冲区已满,会发生什么情况?

javascript - node-restful 不将文档 "contents"保存到 MongoDb

r - 使用R构建RESTful API

C++ boost 错误的文件描述符

tomcat - 在应用服务器上运行maven项目

Java (maven) 将 SSL 与 Shiro 结合使用 - Tomcat 7 的 https 端口插件无法正常工作

java - 使用 java MethodHandles 实现鸭子类型(duck typing)

java - 无法在 Selenium 中截取屏幕截图

java - 将 GWT CodeServer 与 Jetty 分开