[英]Using Self Signed certificate in java
我想連接到短信網關。 我發現以下代碼。
public void smsSender(String username, String password, String to,
String text) throws IOException {
try {
String data = "username=" + username + "&password=" + password
+ "&to=" + to + "&text=" + text;
URL url = new URL("https://sendsms.abc.com:1010/sms.php");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(
urlc.getOutputStream()));
br.write(data);
br.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlc.getInputStream()));
String line;
while (null != ((line = rd.readLine()))) {
output = line;
System.out.println(output);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
當我嘗試使用此方法進行連接時,Eclipse發送一條錯誤消息。
無法找到到請求目標的有效認證路徑
我嘗試訪問的服務器正在使用自簽名證書。 我是這個領域的新手。 我怎么解決這個問題。 提前致謝 :)
要通過SSL進行遠程方法調用,客戶端需要信任服務器的證書。 正如您所說的服務器具有自簽名證書一樣,您的客戶端需要明確配置為信任證書,否則連接將失敗。 要在客戶端和服務器的自簽名證書之間創建信任關系,請執行以下步驟,
首先,您應該在客戶端獲得服務器證書。
為此,我知道的方法是,即在瀏覽器中命中服務器URL並獲取服務器的證書,然后將其導入瀏覽器中。 可能還有其他獲取服務器證書的方法,但是您必須進行探索。
現在,將公共密鑰作為證書從瀏覽器導出到客戶端。 讓它成為server.cer。
現在,創建客戶端密鑰庫
keytool -genkey -alias clientkeys -keyalg RSA -keystore client.keystore -storepass 123456 -keypass 123456 -dname“ CN = localhost,OU = MYOU,O = MYORG,L = MYCITY,S = MYSTATE,C = MY”
創建客戶證書
keytool -export -alias clientkeys -keystore client.keystore -storepass 123456 -file client.cer
現在,將服務器證書導入到客戶端信任存儲中。
keytool-導入-alias serverCert -keystore client.truststore -storepass clientcert -file server.cer
現在,按照Werner提供的鏈接中erickson的評論中所述加載客戶端密鑰庫。
讓我知道是否仍然不清楚。 但我建議您閱讀Google上與客戶端和服務器之間的SSL握手相關的一些文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.