尝试使用具有证书的以下客户端访问https服务。 当在Java8中使用Java7的抛出套接字连接异常时,此方法可以很好地工作。 是否应该添加任何其他证书?
protected Client getClient() {
if (client == null) {
try {
TrustManager[] trustCertificates = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} };
final SSLContext sslContext = SSLContext.getInstance(SSL);
sslContext.init(null, trustCertificates, new java.security.SecureRandom());
final HostnameVerifier verifiedHosts = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
ClientConfig config = new ClientConfig();
config.register(JacksonJsonProvider.class);
config.register(Log4j2JerseyLoggingFilter.class);
return clientBuilder.sslContext(sslContext).hostnameVerifier(verifiedHosts).withConfig(config).build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
LOG.error("Client Builder Exception " + e);
}
}
return null;
}
获取套接字连接异常。
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:593)
at sun.security.ssl.InputRecord.read(InputRecord.java:529)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1281)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1256)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at org.glassfish.jersey.client.internal.HttpUrlConnector$4.getOutputStream(HttpUrlConnector.java:385)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194)
答案 0 :(得分:0)
Java 7默认不使用TLSv1.2,而是使用Java 8 does。它在Java 7中工作的事实表明该服务器不支持TLSv1.2。也许您需要根据以下内容更改为服务器所需的内容:
final SSLContext sslContext = SSLContext.getInstance("TLSv1");