【问题标题】:Accommodate HTTPS Connections in HttpURLConnection在 HttpURLConnection 中容纳 HTTPS 连接
【发布时间】:2018-09-09 17:30:04
【问题描述】:

我正在使用 HttpURLConnection 执行 POST 请求。

我读到 HttpsURLConnection 扩展了 HttpURLConnection 以支持 https 特定的功能,例如 SSL

如果HttpsURLConnection在HttpURLConnection(父类)中使用相同的方法,它会超级调用来自HttpURLConnection的方法。

        URL url = new URL(callBackUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setConnectTimeout(setConnectTimeout);
        connection.setReadTimeout(setConnectTimeout);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
        os.write(data.toString());

        os.flush();
        os.close();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

            while ((output = br.readLine()) != null) {
                stringBuffer.append(output);

            }
        }

但是,当我使用 HTTPS 网址时,我不断收到以下错误;

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.8.0_181]
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964) ~[na:1.8.0_181]
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328) ~[na:1.8.0_181]
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322) ~[na:1.8.0_181]

问题,我如何处理 SSL 和非 SSL 连接,就像 PHP 中使用 CURL 时设置忽略 SSL 检查的情况一样

curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, true);

我在 Linux、Spring 框架和 TomCat Web 服务器上。

有人吗?

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    为了让HTTPS 工作首先生效,您需要将 SSL 证书添加到 Java 的信任库中...为此,您必须按照以下步骤操作:

    通常,您使用的 JVM 的信任库文件位于 %JAVA_HOME%\lib\security\cacerts

    您可以先通过运行以下命令查看您的证书是否已添加到信任库:

    keytool -list -keystore "%JAVA_HOME%/jre/lib/security/cacerts"
    

    一旦您确认它没有添加到您的 Java 信任库中,您可以使用以下命令添加它:

    keytool -import -noprompt -trustcacerts -alias <AliasName> -file   <certificate> -keystore <KeystoreFile> -storepass <Password>
    

    完成以下步骤后,您应该能够运行程序而不会遇到与 HTTPS 相关的问题。

    希望有帮助!

    EDIT-1: 上述方法有助于在 Windows 环境中添加 SSL 证书。请检查以下article 以获得 Linux 变体...

    【讨论】:

    • 我尝试运行 keytool -list -keystore "%JAVA_HOME%/jre/lib/security/cacerts" 响应 keytool error: java.lang.Exception: Keystore file does not exist: %JAVA_HOME%/jre/lib/security/cacerts 在运行第二个命令时,keytool -import -noprompt -trustcacerts -alias &lt;AliasName&gt; -file &lt;certificate&gt; -keystore &lt;KeystoreFile&gt; -storepass &lt;Password&gt; 我不断收到 bash: syntax error near unexpected token newline'`
    • 更新了我的答案以在不同的操作系统变体上显示相同的过程,请检查!
    • 修改 JVM cacerts 文件是一个非常非常糟糕的主意,因为它首先会影响该系统上的所有 Java 应用程序(可能导致其他应用程序中的漏洞),其次您必须每次都一次又一次地执行修改你更新JRE。而是创建一个特定于应用程序的信任库并在您的应用程序中使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    相关资源
    最近更新 更多