自 OpenJDK Java 11 获取 javax.net.ssl.SSLHandshakeException: 收到致命警报: handshake_failure

Pau*_*lor 6 ssl java-11 adoptopenjdk

最近将应用程序从 Oracle Java 9 更新到 AdoptJDk 11.0.6,我现在看到以下代码形式的错误:

 public static String convertWikidataUrl(String wikidataUrl)
    {
        String qPage  = wikidataUrl.substring(wikidataUrl.lastIndexOf('/') + 1);
        String apiUrl = WIKIDATA_IMAGE_API_URL + qPage;
        try
        {
            URL url = new URL(apiUrl);
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            int responseCode = uc.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK)
            {
                MainWindow.logger.severe(":ResponseCode:"+responseCode);

            }

            //Everything ok so continue
            BufferedInputStream bis = new BufferedInputStream(uc.getInputStream());
            JAXBContext jc = getWikidataInitialContext();
            Unmarshaller um = jc.createUnmarshaller();
            Api api = (Api) um.unmarshal(bis);
            if(api.getClaims()!=null
                    && api.getClaims().getProperty()!=null
                    && api.getClaims().getProperty().getClaim()!=null
                    && api.getClaims().getProperty().getClaim().getMainsnak()!=null
                    && api.getClaims().getProperty().getClaim().getMainsnak().getDatavalue()!=null)
            {
                return api.getClaims().getProperty().getClaim().getMainsnak().getDatavalue().getValue();
            }
            else
            {
                return null;
            }
        }
        catch (JAXBException e)
        {
            MainWindow.logger.log(Level.SEVERE, e.getMessage(), e);
        }
        catch (MalformedURLException mue)
        {
            MainWindow.logger.log(Level.SEVERE, mue.getMessage(), mue);
        }
        catch (Exception ex)
        {
            MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

失败并显示:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
    at java.base/sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:291)
    at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:180)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1151)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1062)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402)
    at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)
    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:334)
    at com.jthink.songkong.analyse.musicbrainz.WikipediaImage.convertWikipediaUrl(WikipediaImage.java:49)
    at com.jthink.songkong.analyse.musicbrainz.ArtistArtwork.findArtistImageLink(ArtistArtwork.java:54)
    at com.jthink.songkong.analyse.musicbrainz.ArtistArtworkOnlineLookup.call(ArtistArtworkOnlineLookup.java:63)
    at com.jthink.songkong.analyse.musicbrainz.ArtistArtworkOnlineLookup.call(ArtistArtworkOnlineLookup.java:26)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Run Code Online (Sandbox Code Playgroud)

该问题已在 Windows 上出现,但也可能出现在其他平台上

该错误肯定是由于更改 JRE 造成的,但我不知道问题是从 Java 9 转移到 11,还是从 Oracle 转移到 AdoptJdk,我该如何解决此问题?

更新

  1. 使用 11.0.6 的 MacOS 上不会出现此问题
  2. 在 Windows 上从 11.0.6 更新到 11.0.7 没有效果
  3. 在 Windows 上添加 -Dhttps.protocols=TLSv1.1,TLSv1.2 (以禁用 TLS1.3 支持)可以修复此问题。
  4. 此错误的可能版本https://bugs.openjdk.java.net/browse/JDK-8206923

use*_*461 10

客户端无法打开与服务器的连接,因为它们可能不支持相同的 TLS 版本或设置。

尝试使用以下命令启动应用程序:

  • -Dhttps.protocols=TLSv1.2(Java 8 中默认)
  • -Dhttps.protocols=TLSv1.3(Java 11 中默认)
  • -Dhttps.protocols=TLSv1.0

TLSv1.2 (2008) 是当前受支持的 TLS 版本,它已部署在任何地方并受到所有事物的支持。这是安全的默认设置,也是让事情正常运行的强制条件。

TLSv1.3 (2018) 是即将推出的版本。它正在缓慢地遍及各处(Web 服务器、应用程序、负载均衡器、CDN 等)。过渡应该是无缝的,但事实并非如此。当然,任何软件都不可能在第一次尝试时就完美,并且会出现一些错误和不兼容性。JDK 11 引入了 TLSv1.3 并尝试默认使用它,但由于出现错误,效果并不好,解决方法是强制使用 TLSv1.2。(2021 年更新:TLS 1.3 在过去一两年已被浏览器和主要中间件采用,当您升级软件/基础设施时很可能会出现)

如果您依赖 TLS 进行客户端证书身份验证(通常用于处理高度敏感信息的企业系统(例如银行 API)),则会出现棘手的边缘情况。TLS 1.3 改变了客户端身份验证的方式,因此客户端和服务器软件可能都需要修复才能在新版本下运行。HTTP/2 在设计上破坏了客户端身份验证,RFC 正在等待就解决方案达成一致,同时使用 HTTP/1.1。

TLSv1.0 (1999) 是一个过时的版本,禁止使用并在最新的库版本(OpenSSL 1.1.x、JDK 11 等)中删除。如果您使用多年未维护的遗留企业 Java 应用程序(任何涉及 Java 6 或 7 的内容都是危险信号),那么到 2020 年仍有可能遇到它。这些确实需要升级。