java - 使用 Java 套接字连接到未签名的 HTTPs 站点

标签 java sockets ssl-certificate

我正在尝试设置一个 Java 程序来连接到我自己使用 SSL 的网站。我让它完全适用于标准 HTTP 站点,但不适用于 HTTP。我的问题是我的证书没有签名。

我怎样才能让 java 忽略它未签名的事实并让我连接呢?当我尝试连接时出现此错误:

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 com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at util.Watchlist.getWatchListWebsiteReaderSSL(Watchlist.java:51)
    at util.Watchlist.updateWLDP(Watchlist.java:64)
    at util.Watchlist.getWLDP(Watchlist.java:24)
    at commands.devices.cpt.WldpMessage.run(WldpMessage.java:52)
    at cli.Parser.Parse(Parser.java:74)
    at fromUI.UISocketReader.run(UISocketReader.java:54)
    at java.lang.Thread.run(Unknown Source)
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(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 25 more

最佳答案

您需要创建一个接受未签名证书的 TrustManager,并将其与用于 HTTPS 连接的 SSLContext 相关联。 http://www.exampledepot.com/egs/javax.net.ssl/trustall.html 有一个简单的例子

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
  new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    public void checkClientTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {}
    public void checkServerTrusted(
        java.security.cert.X509Certificate[] certs, String authType) {}
  }
};

// Install the all-trusting trust manager
try {
  SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the 
// truststore
try {
  URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}

请注意,链接示例不会对服务器的 SSL 证书进行任何验证,因此您将无法完全信任所连接网站的身份。

关于java - 使用 Java 套接字连接到未签名的 HTTPs 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453144/

相关文章:

java - 修复 javax.net.ssl.SSLPeerUnverifiedException : No peer certificate

java - 阻止 Android Studio 使用支持库

java - 缓冲输出写入文件错误

android - 可以在服务器端使用节点js与scoket.io(1.0.4)一起使用的Android库

android - Python 服务器和 Android 应用程序之间的连接

ssl-certificate - 如何将 .p7b 转换为 .jks

ssl - 证书 CN 作为方括号中的 IPv6 地址

java - Flying Saucer/OpenPdf相同的字符串html内容但不同的字节[]

java - MYSQL日期比较问题异常

c - C中通过socket接收和发送数据