在项目中使用到了tlsv1.2来请求三方支付渠道,在拿到证书xxx.crt 和 密钥 xxx.key后,通过java代码配置keyStore和trustStore后一直报错,说加载证书失败,经查询发现java不能直接加载crt和key文件,需要转换成.p12或者.jks格式的文件
以下是转换方式:
.crt 转 .cer
openssl x509 -in xxx.crt -out xxx.cer -outform der
.cer 转 truststore
keytool -import -alias "merchant-support" -file xxx.cer -keystore xxx.truststore
.crt 转 .p12
openssl pkcs12 -export -in xxx.crt -inkey 20181030.key -out ms.p12 -name xxx
转换后如果直接用java加载keyStore和trustStore总是提示没有找到信任库,可以在项目发布的jdk中导入信任库,然后加载的时候不是用信任库,就会默认的使用jdk中的信任库。
如下,创建SSLContext
public static SSLContext getSSLContext(String password, String keyStorePath, String trustKeyStorePath)
throws Exception {
//KeyStore用于存放证书,创建对象时 指定交换数字证书的加密标准
File keyFile = new File(keyStorePath);
InputStream keyStoreStream =