【发布时间】:2018-05-22 05:31:50
【问题描述】:
我将密钥和证书(组合)到一个 cert.pem 文件中,
我得到了,
“异常”:“javax.net.ssl.SSLHandshakeException”,
"message": "收到致命警报:bad_certificate",
pem file is right, but i think problem is how i generating jks keystore file.
.pem 证书格式
开始证书
...
结束证书
开始证书
...
结束证书
开始 RSA 私钥
...
结束 RSA 私钥###`
将它与 keytool comand comand is 结合起来
keytool -import -trustcacerts -alias yourdomain -file combined.pem -keystore yourkeystore.jks
java代码是
public class HttpsTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
请求是
FileInputStream instream = new FileInputStream(
new File(this.resourcePath()+"/path_to.jks")
);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(instream, "password".toCharArray());
SSLContext sslContext = SSLContexts.custom()
.loadKeyMaterial(keyStore, "password".toCharArray()) // use null as second param if you don't have a separate key password
.build();
sslContext.init(null,new X509TrustManager[]{new HttpsTrustManager()}, new SecureRandom());
HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
HttpResponse response = httpClient.execute(
new HttpPost("https://url")
);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
【问题讨论】: