简体   繁体   中英

Java use SSLContext for HttpsURLConnection

Looking for an example to open HttpsURLConnection with SSLContext and restricted to TLSv1.2. The context is built using trust store and trust key and after I added the custom() call - the TLS setting seem to be changed to just "TLS" vs. "TLSv1.2"

my code is:

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext = SSLContexts.custom()
                .loadTrustMaterial(getKeyStore(trustStoreURL, trustStorePassword), new TrustSelfSignedStrategy())
                .loadKeyMaterial(getKeyStore(keyStoreUrl, keyStorePassword), keyStorePassword.toCharArray()).build();

So after the custom() I see "TLS" in sslContext properties.

Why do you want to use only a single version, is there any restriction on your server host? Most modern servers use TLSv1.2 which is backward compatible to one or two versions. When you use TLSv1.2 while creating socket factory like below,

SSLSocketFactory.getInstance("TLSv1.2")

the default allowed protocols would be SSL, TLS, TLSv1.1, TLSv1.2.

With that being said, to answer your question, You can set your SSLSocket to enable just a few protocols using the setEnabledProtocols method. Please check this doc for more on this. Once done, your SSL connection will allow only the specified protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM