我有一个ssl连接(双向握手(,我无法理解为什么以下代码过程400(openJdk 11,p12文件和密码由服务器提供,cer文件由服务器提供(,
我已经通过以下命令从cer文件创建了jks文件:
keytool -importcert -file example-api.cer -keystore example-api.jks
代码
File keyFile = new File(Objects.requireNonNull(exampleController.class.getClassLoader().
getResource("example-client-api1.p12")).getFile());
File trustFile = new File(Objects.requireNonNull(exampleController.class.getClassLoader().
getResource("example-api.jks")).getFile());
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try(FileInputStream inStream = new FileInputStream(keyFile)) {
keyStore.load(inStream, "password".toCharArray());
}
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustFile, "password".toCharArray() ,new TrustAllStrategy()).
loadKeyMaterial(keyStore , "password".toCharArray()).build();
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
SSLConnectionSocketFactory socketFactory =
new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLHostnameVerifier(hostnameVerifier)
.setSSLSocketFactory(socketFactory)
.useSystemProperties()
.build();
HttpGet httpget = new HttpGet("https://example-api/link?token=@Secret_Token@");
System.out.println("executing request" + httpget.getRequestLine());
return httpclient.execute(httpget);
上面的代码总是返回400(未发送所需的SSL证书(。
但以下卷曲(在IOS上(有效:
curl https://example-api/link?token=@secret_token@ --cacert ./example-api-ca.crt --cert ./example-client-api1.p12:password
如有任何帮助,将不胜感激
好吧,在经历了一些令人沮丧的日子之后:我不知道原因,但问题是URL的hostName中的连字符字符('-'(,删除连字符可以解决问题,不确定原因,但无论如何都可以发布,也许有人可以解释这种现象。示例(使用上面的代码(:
example-api -> not working
example.api -> Works OK