如何控制SSLContext范围 [英] How to control SSLContext scope

查看:142
本文介绍了如何控制SSLContext范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下代码,因为我想改变验证证书的方式。

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

trm =某个信托经理

trm = some trust manager

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

然后,这将为将来进行的所有https连接设置SSLContext,而不管是什么线程。控制范围的最简洁方法是什么,以便我只为我想要的那些调用设置它?

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?

推荐答案

你可以设置您希望使用此信任库的实际连接对象上的套接字工厂:

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

这样做而不是调用 setDefaultSSLSocketFactory

这篇关于如何控制SSLContext范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆