2011-06-16 24 views
2

我來自'C'背景。我正在嘗試分析如何編寫Java SSL服務器。是否有任何方法以編程方式直接將證書和私鑰加載到SSLServerSocket(Java)?

因此,我發現的例子講的是將證書加載到密鑰存儲區並將其私有化,然後以編程方式將KeyStore關聯到SSLContext,然後從相同的位置創建SSLServerSocket。

但是,有沒有任何方法可以直接提到SSLServerSocket應該使用哪個證書?此外,即使我使用密鑰庫,並且如果有多個證書和密鑰,我如何指定SSLServerSocket應該使用哪一個?

任何示例鏈接都會有所幫助。

+1

請參閱:http://stackoverflow.com/questions/1788031 – Thor 2011-06-16 16:06:05

回答

2

正如我在評論中所說的,在answer to one of your questions earlier中,您需要使用KeyManager來執行此操作。

這樣做有很多種方法。 jSSLutils是一個庫,可以使它更方便一些(但你也可以更手動地做)。有一個FixedServerAliasKeyManager的例子。最簡單的方法之一是使用這樣的東西:

X509SSLContextFactory sslContextFactory = new X509SSLContextFactory(); 
// By default, this would use the keystore passed with the usual system properties. 

sslContextFactory.setKeyManagerWrapper(
    new FixedServerAliasKeyManager.Wrapper("the-alias-you-want")); 
// You could read the alias name from a custom system property, for example. 

SSLContext sslContext = sslContextFactory.buildSSLContext("TLS"); 
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory(); 
相關問題