【问题标题】:Is it possible to disable ssl for https?是否可以为 https 禁用 ssl?
【发布时间】:2023-03-18 13:25:01
【问题描述】:

Java 上的应用程序。使用 OkHttp 版本 2.7.5。向另一个服务发出请求并发生错误。

SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target

我没有证书。 okHttp3的版本好像有解决办法。但不能更改版本。如何解决问题?

【问题讨论】:

标签: java ssl https okhttp


【解决方案1】:

是否可以为 https 禁用 ssl?

从字面上看,没有。

使用 SSL 是 HTTPS 协议的基础。如果您根本不想使用 SSL,请使用 HTTP 端点配置您的服务器并使用它而不是 HTTPS。

进一步使用 SSL 需要(至少)语法格式良好的证书。这也是 HTTPS 协议的基础。

现在,如果问题是您的服务器证书已过期,那么可能的解决方案是使用以下描述的方法:

如果问题是您无法为服务器获得适当的证书(例如,您买不起),那么另一种解决方案是:

  1. 生成self-signed certificate;见How to generate a self-signed certificate using Java Keytool,
  2. 安装在服务器端,
  3. 如上配置客户端忽略证书有效性。

但请注意,做这些事情中的任何一个都有安全问题。

还有第三种更安全的解决方案。

  1. 生成自签名证书(如上)
  2. 安装在服务器端,
  3. 使用 Keytool 将证书作为可信证书添加到客户端应用的密钥库中。

【讨论】:

  • 还有免费获取证书的方法,比如letsencrypt。但这似乎完全是关于建立客户端连接,而不是管理服务器证书。需要连接到外部服务,连接尝试说 boo。
【解决方案2】:

为什么要使用 HTTPS 但没有证书,您应该按照上面提到的 Stephen 进行操作。但是,如果您想从字面上忘记 https 的含义,您可以考虑覆盖该行为

 private static OkHttpClient getUnprotectedClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }
        };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();


        return new okhttp3.OkHttpClient.Builder()
                .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                }).build();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2021-04-17
    • 2014-10-08
    相关资源
    最近更新 更多