[英]Java sending HTTP Post with client authentication
我有一個客戶端證書,密鑰和證書可以使用。 我想要的代碼的卷曲將是
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的錯誤,此后添加了一些應忽略此代碼的代碼,但我認為這不是造成問題的原因。 當卷曲給我實際的預期內容時,我從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 //” vs“ https://”)是由於我調試而導致堆棧溢出的錯字。 那不是問題。
添加一個冒號( :
https的后)。
HttpPost httpPost = new HttpPost("https//" + ip + "/query"); // old
HttpPost httpPost = new HttpPost("https://" + ip + "/query"); // new
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.