我目前正在编写一个从PHP服务器接收AJAX请求的JAVA应用程序。 我使用套接字,它适用于HTTP请求。 但现在我想对HTTPs请求做同样的事情。
所以我使用网站的证书和RSA密钥创建了一个keyStore:
openssl pkcs12 -export -in <path_to_website_cert> -inkey <path_to_website_rsa_key> -out keystore.p12 -name website -CAfile <path_to_ca_cert> -caname root
然后我用jks转换它:
keytool -importkeystore -deststorepass changeit -destkeypass changeit -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass password -alias website
我还将证书添加到trustStore:
keytool -import -file <path_to_website_cert> -alias website -keystore <path_to_my_jdk>\jre\lib\security\cacerts
以下是我用于在JAVA应用上启动服务器的代码:
public void lancer_serveur_https() {
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStore", "C:/Users/f.guibert/Documents/Cert/incafu.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
System.setProperty("javax.net.ssl.trustStore", "C:/Users/f.guibert/Documents/Cert/trustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
try {
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(this.port);
while(true) {
try {
SSLSocket s = (SSLSocket) ss.accept();
s.setEnabledCipherSuites(s.getSupportedCipherSuites());
OutputStream out = s.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while (((line = in.readLine()) != null) && (!("".equals(line)))) {
System.out.println(line);
}
out.close();
in.close();
s.close();
} catch(Exception e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
完成所有这些步骤后,当我的AJAX请求被发送时,我收到了JAVA的错误:
java.net.SocketException: Software caused connection abort: recv failed
我的网络控制台(网络标签)中出现错误,指出该证书仅对域有效:
*.mydomain.com, with error code SSL_ERROR_BAD_CERT_DOMAIN.
我真的被它困住了因为我不知道我做错了什么。我认为有些东西我用keyStore和trustStore误解了但是找不到什么。