我有一个客户端证书、 key 和 cacert 可以使用。我想要的代码的 curl 是
curl https:<ip>/query --cert client_cert.pem --key client_key.pem --cacert ca_cert.pem "-d <post data>"
我曾尝试将这些文件组合成 PFX(和 p12),因为这似乎是必需的格式,但我不确定我是否正确地完成了:
openssl pkcs12 -export -out client.pfx -inkey client_key.pem -in client_cert.pem -certfile ca_cert.pem
我最初收到关于我的证书中没有定义 SAN 的错误,此后我添加了一些应该忽略此错误的代码,但我认为这不是我的问题的原因。当 curl 给我实际预期的内容时,我从 POST 收到 404 结果
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream("client.pfx"), keyPassphrase.toCharArray());
SSLContext sslContext = SSLContexts.custom()
.loadKeyMaterial(keyStore, keyPassphrase.toCharArray())
.loadTrustMaterial(keyStore, TrustSelfSignedStrategy.INSTANCE)
.build();
HttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).setSSLContext(sslContext).build();
HttpPost httpPost = new HttpPost("https://" + ip + "/query");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(postKey, postVal));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.toString());
给予
HttpResponseProxy{HTTP/1.1 404 Not Found [Date: Sun, 28 Oct 2018 02:11:55 GMT, Server: Apache/2.4.29 (Ubuntu), Content-Length: 280, Keep-Alive: timeout=5, max=100, Connection: Keep-Alive, Content-Type: text/html; charset=iso-8859-1] ResponseEntityProxy{[Content-Type: text/html; charset=iso-8859-1,Content-Length: 280,Chunked: false]}}
如果我的代码是错误的或者它是证书还是什么,有什么想法吗?据我所知,我已经正确地制作了 pfx,如果 curl 有效,证书显然也有效。我已经研究了一段时间,但无法弄清楚。
编辑:由于我的调试,原始代码(“https//”与“https://”)是堆栈溢出的拼写错误。那不是问题所在。
最佳答案
在 https 之后添加一个冒号 (:
)。
HttpPost httpPost = new HttpPost("https//" + ip + "/query"); // old
HttpPost httpPost = new HttpPost("https://" + ip + "/query"); // new
关于Java 发送带客户端身份验证的 HTTP Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027837/