Viraj
Viraj

Reputation: 1420

sslcontextbuilder and SSLContexts deprecated

I am using JDK1.8 with JDK Compilance JavaSE-1.7, Eclipse Luna, and Apache httpclient 4.4.1.

I am getting warning in Eclipse that sslcontextbuilder and SSLContexts are deprecated. What are alternatives for these classes?

Upvotes: 26

Views: 33816

Answers (3)

Soumya Sahoo
Soumya Sahoo

Reputation: 1

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);
    }
}

Upvotes: 0

Rob Baily
Rob Baily

Reputation: 3019

I have actually just been looking at this and it appears that the HttpCLient SSLContexts class is in the process of being moved from org.apache.http.conn.ssl.SSLContexts to org.apache.http.ssl.SSLContexts. I changed my imports to these new packages and it appears to be good now. Not sure what your reference is for sslcontextbuilder but I am pretty sure it has an alternate implementation as well. Let me know more details and I can check.

Upvotes: 46

bluetata
bluetata

Reputation: 654

you can change it as below code

// 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

Upvotes: 1

Related Questions