如何控制 SSLContext 范围
例如,如果我使用以下代码,因为我想更改证书的验证方式。
trm = some trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { trm }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
然后,这将为将来建立的所有 https 连接设置 SSLContext,无论哪个线程。控制范围以便我只为我想要的那些调用设置它的最干净的方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在想要使用此信任存储的实际连接对象上设置套接字工厂:
执行此操作而不是调用
setDefaultSSLSocketFactory
。You can set the socket factory on the actual connection object that you want to have use this trust store:
Do that instead of invoking
setDefaultSSLSocketFactory
.