java.net
有一个简单的getServerCertificates
在其 API 中(示例如下)。我正在reactor-netty中寻找类似的操作,如果没有,则在spring-boot/webflux/HttpClient的任何其他反应式API中寻找类似的操作。
这个操作(客户端读取证书)在reactor-netty中似乎不可能。是吗?如果不是,另一个 spring-boot 组件中是否有替代方法来执行此操作?
package com.example.readCertificate.service;
import java.net.URL;
import java.securiiity.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ShowCert {
private Logger logger = LogManager.getLogger();
public void showCert(String url) {
try {
URL destinationURL = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) destinationURL.openConnection();
connection.connect();
Certificate[] certificates = connection.getServerCertificates();
for (Certificate certificate : certificates) {
logger.info("certificate is:" + certificate);
}
} catch (Exception e) {
logger.error(e);
}
}
}
在 Spring WebFlux 的 WebClient 中,我们通常使用 netty 作为后端。我们提供一个豆子ReactorClientHttpConnector
我们在其中创建 netty http-client。
为了处理 SSL,netty 使用通道管道中的处理程序。
在这里我对事件进行回调doOnConnected()
并访问 SSL 处理程序和SSLSession
.
SSLSession
提供方法getPeerCertificates(), getLocalCertificates()
,这样我们就可以在这里访问证书。
@Bean
public ReactorClientHttpConnector reactorClientHttpConnector() {
return new ReactorClientHttpConnector(
HttpClient.create()
.doOnConnected(connection -> {
ChannelPipeline pipeline = connection.channel().pipeline();
Optional.ofNullable(pipeline)
.map(p -> p.get(SslHandler.class))
.map(SslHandler::engine)
.map(SSLEngine::getSession)
.ifPresent(sslSession -> {
try {
Certificate[] peerCertificates = sslSession.getPeerCertificates();
if (Objects.nonNull(peerCertificates)) {
Stream.of(peerCertificates)
.forEach(System.out::println);
}
} catch (Exception e) {
e.printStackTrace();
}
});
})
);
}
并创建您的 WebClient:
@Bean
public WebClient httpsClient() {
return WebClient.builder()
.clientConnector(reactorClientHttpConnector())
.baseUrl("https://secured-resource.com)
.build();
}
然后,当使用此 httpsClient bean 进行 http 调用时,您应该在控制台中看到结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)