Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control SSLContext scope

Tags:

java

http

ssl

If I use the following code because I want to, for example, to change the way certificates are validated.

trm = some trust manager

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trm }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

Then this sets the SSLContext for all https connections that will be made in the future regardless of what thread. What is the cleanest way to control the scope so that I set it only for those calls I want?

like image 915
che javara Avatar asked Nov 02 '25 16:11

che javara


1 Answers

You can set the socket factory on the actual connection object that you want to have use this trust store:

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(sc.getSocketFactory());

Do that instead of invoking setDefaultSSLSocketFactory.

like image 108
laz Avatar answered Nov 05 '25 09:11

laz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!