[英]Java SSL connection with self-signed certificate without copying complete keystore to client
我正在使用Java與客戶端應用程序一起設置許可servlet,該客戶端應用程序將發布對新許可的請求並在該服務器上驗證現有許可。 該servlet在Tomcat中運行。 我已經配置了Tomcat,以便它僅允許通過https連接到servlet,這很好用。
我已經使用'keytool -genkey -alias www.mysite.com -keyalg RSA -keystore license.store'
創建了一個自簽名證書,該證書創建了一個文件license.store
並指向asdf1234
指向此keystoreFile,其密碼為asdf1234
。
當我嘗試通過Java中的https從客戶端連接到servlet時,由於證書不在信任庫中,我收到了熟悉的PKIX path building failed
。 我嘗試使用此建議來解決此問題,導致以下代碼:
private SSLSocketFactory getSSLFactory() throws Exception {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream is = this.getClass().getResourceAsStream("license.store");
if(is ==null) {
return null;
}
keyStore.load(is, "asdf1234".toCharArray());
is.close();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
return ctx.getSocketFactory();
}
之后,我打電話給:
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setSSLSocketFactory(getSSLFactory());
這將導致成功的連接。
現在的問題是,只有將license.store
復制到客戶端並將其加載到KeyStore.load()
時,我才能license.store
工作。 將服務器使用的私鑰及其密碼復制到客戶端並不十分安全。 有什么方法可以從license.store中僅提取公鑰並使用它? 我已經在這個論壇和其他論壇上搜索了一天,但似乎無法獲取。
您不應該生成一個公鑰-私鑰對,而是將服務器的證書導入到您(客戶端)的Java信任庫中。 該證書不是機密信息,因此不會在客戶端提供安全風險。 請參閱-import
選項以獲取keytool。 這是一個例子 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.