SSL握手异常:没有适当的协议。

61

我最近给我的网站添加了SSL,现在可以通过https访问。现在当我的Java应用程序尝试使用缓冲读取器从我的网站进行请求和读取时,它会生成这个堆栈跟踪。

我没有使用自签名证书,该证书来自Namecheap,使用COMODO SSL作为CA来签署我的证书。我正在使用Java 8。

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at sun.security.ssl.Handshaker.activate(Handshaker.java:503)
at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1482)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1351)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)

我的代码非常基础,只是尝试使用缓冲读取器读取我的网站上的页面

 private void populateDataList() {
    try {
        URL url = new URL("https://myURL.com/Data/Data.txt");
        URLConnection con = url.openConnection();
        con.setRequestProperty("Connection", "close");
        con.setDoInput(true);
        con.setUseCaches(false);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line;
        int i = 0;
        while((line = in.readLine()) != null) {
            this.url.add(i, line);
            i++;
        }
    }   catch (Exception e) {
        e.printStackTrace();
    }

}

我尝试将我的SSL证书添加到JVM的密钥库中,甚至尝试使用此代码接受每个证书(我知道这违背了SSL的目的)

 private void trustCertificate() {
    TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                }
            }
    };
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (GeneralSecurityException e) {
    }
    try {
        URL url = new URL("https://myURL.com/index.php");
        URLConnection con = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }

    } catch (Exception e) {

    }
}

我被难住了,非常感谢任何帮助!


你可能需要提供更多的细节。使用自签名证书吗?Java 版本是什么? - D-Klotz
我没有使用自签名证书,我的证书来自Namecheap,他们使用COMODO SSL作为CA来签署我的证书。我正在使用Java 8。 - ChrisianBartram
1
(1) 证书与此错误无关。(2) 您是否使用Sun/Oracle版本的Java 8,如果是,是哪个更新版本,还是其他Java?在JRE中是否进行了任何配置更改,特别是在文件$JRE/lib/security/java.security中?(3) 是否设置了任何涉及https的系统属性,特别是https.protocols?(4) 尝试使用sysprop javax.net.debug=ssl运行并发布结果,但如果您的信任存储有大量证书(默认情况下),则可以将该部分缩小到最小。 - dave_thompson_085
1
请指明您正在使用的Java版本。如果您添加了“-Djavax.net.debug=ssl:handshake:verbose”,它将允许您更详细地检查握手问题。 - John Yeary
13个回答

1
我在一个tomcat7服务器上遇到了同样的情况,版本为5.7.34-0ubuntu0.18.04.1,openjdk版本为"1.8.0_292"。我尝试了很多方法,比如在server.xml文件中禁用SSL,更改连接字符串等等,但最终我所做的只是编辑文件java.security,使用sudo nano /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/java.security命令注释掉并删除TLSv1和TLSv1.1。
# Comment the line below
#jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
#    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
#    include jdk.disabled.namedCurves

# your new line should read as beloew
jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
include jdk.disabled.namedCurves

0

虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。-【来自审查】 - Procrastinator

0
在这种情况下,对于我而言:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

我发现这与JDK/JRE(Java\jdk1.8.0_291\jre\lib\security)的配置有关,为了解决这个问题,您需要禁用TLS匿名和NULL密码套件

您可以在官方文档中找到如何操作: https://www.java.com/en/configure_crypto.html

在执行此操作之前,请考虑使用LEGACY算法所带来的影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接