[英]How to create SSL contexts for client authentication
我正在對遠程服務器執行客戶端身份驗證。 我發現以下用於設置密鑰庫位置和密碼的代碼。
System.setProperty("Djavax.net.ssl.keyStorePassword=password");
System.setProperty("Djavax.net.ssl.keyStore=location");
此代碼為在該環境中運行的所有線程設置密鑰庫。 但是我想在不同的線程上設置多個密鑰庫位置/密碼。 經過研究后,我發現可以創建SSLContexts
來實現此目的,並且獲得了以下代碼來實現此目的。 但這不起作用。
try{
char[] password = "changeit".toCharArray();
String keyFileLocation = "location to keystore";
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyFileLocation), password);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
kmf.init(ks, password);
KeyManager[] kms = kmf.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory.getInstance("");
tmf.init(ks);
TrustManager[] tms = tmf.getTrustManagers();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kms, tms, null);
SSLContext.setDefault(ctx);
} catch(Exception e){
System.out.println(e);
}
請引導我朝正確的方向實現。 這段代碼有什么問題?
代碼失敗,因為我沒有為TrustManagerFactory提供算法。 下面的代碼工作。
try{
char[] password = "changeit".toCharArray();
String keyFileLocation = "location to keystore";
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream(keyFileLocation), password);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
kmf.init(ks, password);
KeyManager[] kms = kmf.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
TrustManager[] tms = tmf.getTrustManagers();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kms, tms, null);
SSLContext.setDefault(ctx);
} catch(Exception e){
System.out.println(e);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.