问题描述
是否可以提交一个 String
值来检查(如果它等于)目标证书的 CN
(String
值)?
我用
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(body))
.uri(URI.create("https://<ip-address>/..."))
.timeout(Duration.ofMillis(1000))
.build();
HttpResponse<String> response;
try {
response = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.authenticator(this.authenticator)
.build()
.send(request,HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException iex) {
} catch (IOException ioex) {
}
或者如果无法提交,也许可以定义或设置一个方法,从目标证书读取 CN 并根据另一个字符串值检查该值。
使用 HttpsURLConnection
的一种解决方案是:
HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
public boolean verify(String hostname,SSLSession session) {
// check what ever,example CN from certificat
Certificate[] peerCertificates = session.getPeerCertificates();
Certificate peerCertificate = peerCertificates[0];
...
return <true> or <false>;
}
}
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setHostnameVerifier(myHostnameVerifier);
使用 HttpsURLConnection
意味着使用 OutputStream
和 InputStream
,这是老派风格。
这也可以用 Java HttpClient
或 Apache HttpClient 来完成吗?
我必须并行运行许多这些连接。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)