sslcontextbuilder 和 SSLContexts 已弃用

问题描述 投票:0回答:3

我使用的是 JDK1.8 以及 JDK Compilance JavaSE-1.7、Eclipse Luna 和 Apache httpclient 4.4.1。

我在 Eclipse 中收到警告,指出

sslcontextbuilder
SSLContexts
已弃用。这些课程有哪些替代方案?

java apache ssl
3个回答
44
投票

我实际上刚刚查看了这个,看来 HttpCLient SSLContexts 类正在从

org.apache.http.conn.ssl.SSLContexts
移动到
org.apache.http.ssl.SSLContexts
。我将导入更改为这些新软件包,现在看起来很好。 不确定您的参考是什么
sslcontextbuilder
但我很确定它也有替代实现。 让我知道更多细节,我可以检查。


1
投票

您可以按以下代码更改它

// Fixing deprecated code to use current HttpClient implementations      Sekito.Lv 01/30/2019 11:29     Start
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.SSLContextBuilder;

//import org.apache.http.conn.ssl.SSLContexts;
//import org.apache.http.conn.ssl.SSLContextBuilder;
// Fixing deprecated code to use current HttpClient implementations      Sekito.Lv 01/30/2019 11:29     End

0
投票
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;

public class SSLContextExample {
    public static SSLConnectionSocketFactory createSSLConnectionSocketFactory() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }

                    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                    }

                    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        };

        // Create a trust manager that does not validate certificate chains
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, null);

        return new SSLConnectionSocketFactory(sslContext);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.