java - Spring Boot 2.5、Java 15 - Java HttpClient 或 Apache HttpClient,检查目标证书的 CN
问题描述
是否可以提交一个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,并根据另一个 String 值检查该值。
一种解决方案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
and InputStream
,这是老式的风格。
这也可以用 JavaHttpClient
或 Apache HttpClient 完成吗?我必须并行运行许多这些连接。
解决方案
推荐阅读
- javascript - Javascript setInterval,如何返回一个值?
- javascript - 使用 .equals 的 Dexie.js 查询不适用于 localStorage
- amazon-s3 - 使用和不使用 --dryrun 选项运行 s3 同步之间的时间差
- java - 带引用链的 JPA 复合主键
- css - 在使用滚动捕捉时实现页脚
- visual-studio-code - VScode自定义集成终端不显示输出中的所有路径
- javascript - 如何在 vuetify 的 v-combobox 中使用 no-data-text 道具?
- javascript - 麦克风在应用程序中被禁用/不工作(从网站转换)
- jquery - DataTables - 我不想一次加载所有数据?
- c# - C# 填充列表
使用来自另一个类的字符串和默认构造函数