[英]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.