Android SSL用法:从UI线程到后台线程的密钥输入流
我有一个关于android SSL套接字使用的设计问题。为了能够初始化安全套接字,我需要能够从后台线程访问密钥输入流来初始化会话上下文。我使用另一个key inputstream来初始化HttpsURLConnection,但我是在UI线程中这样做的(这可能不相关)。我有几个后台线程在各个安全套接字上运行,我希望它们共享一个SslContextFactory。见下文。这是正确的设计吗?SSLContext是线程安全的吗?如果SSLContext不是线程安全的,我想知道如何为每个线程创建一个SSLContext,而不必返回(即等待UI活动)UI线程为每个线程获取一个新的键输入流(原始资源上的流)Android SSL用法:从UI线程到后台线程的密钥输入流,android,ssl,android-activity,thread-safety,Android,Ssl,Android Activity,Thread Safety,我有一个关于android SSL套接字使用的设计问题。为了能够初始化安全套接字,我需要能够从后台线程访问密钥输入流来初始化会话上下文。我使用另一个key inputstream来初始化HttpsURLConnection,但我是在UI线程中这样做的(这可能不相关)。我有几个后台线程在各个安全套接字上运行,我希望它们共享一个SslContextFactory。见下文。这是正确的设计吗?SSLContext是线程安全的吗?如果SSLContext不是线程安全的,我想知道如何为每个线程创建一个SSL
class SslSessionContextFactory
{
SSLContext sslContext;
public SslSessionContextFactory(SslInfo info) throws Exception
{
KeyStore store = KeyStore.getInstance(info.getKeyStoreType());
// Obtain the input stream for the key <--- this is the code in question.
store.load(info.newKeyStream(), info.getPassphrase().toCharArray());
TrustManagerFactory factory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
factory.init(store);
// Initialize the SSL context.
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, getCertificates(), new SecureRandom());
}
public SSLSessionContext getSessionContext()
{
// Initialize the session context.
SSLSessionContext sessionContext = sslContext.getServerSessionContext();
sessionContext.setSessionCacheSize(SESSION_CACHE_SIZE);
sessionContext.setSessionTimeout(SESSION_TIMEOUT);
return sessionContext;
}
}
SslSessionContextFactory cxtFactory = ...;
SocketFactory sktFactory = cxtFactory.getSessionContext().getSocketFactory();
... sktFactory.createSocket(host, port);