[英]Using spring Web Client hit https rest service
I have got two .cer
file from client now my goal is to hit client api using https rather than http using spring webclient. I have got two
.cer
file from client now my goal is to hit client api using https rather than http using spring webclient. As I am new to spring webclient not getting clue what to do因为我是 spring webclient 的新手,所以不知道该怎么做
I have imported both.cer in keystore using below command我已经使用以下命令在密钥库中导入了both.cer
keytool -import -file "C:\Users\ankur\Download\Entrust_Root_Certification_Authority-G2.cer" -keystore "C:\Program Files\Java\jre1.8.0_40\lib\security\cacerts" -storepass "changeit"
keytool -import -file "C:\Users\ankur\Download\certificate\Entrust_Certification_Authority-L1K.cer" -keystore "C:\Program Files\Java\jre1.8.0_40\lib\security\cacerts" -storepass "changeit
I have written below to code in order to hit the api programatically from webclient我在下面编写了代码,以便从 webclient 以编程方式访问 api
@Bean
public WebClient createWebClient() throws SSLException {
SslContext sslContext = SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
HttpClient httpClient = HttpClient
.create()
.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
return WebClient.builder()
.clientConnector(connector).build();
}
application.yml
rest:
endpoint:https://someexample.com/xyz
I have no idea what to do with.cer file where to store these.cer file do we need to include in resource folder of spring boot any example or link would be helpful thanks我不知道如何处理.cer 文件在哪里存储这些.cer 文件我们需要包含在 spring 引导的资源文件夹中吗?任何示例或链接都会有所帮助,谢谢
below is the one of the approach with that you can configure ssl certs to webclient
下面是一种方法,您可以将 ssl 证书配置为
webclient
Step 1: Copy your required certs to your
resources
folder第 1 步:将所需的证书复制到
resources
文件夹
let's take an example as config/Entrust_Root_Certification_Authority-G2.cer
我们以
config/Entrust_Root_Certification_Authority-G2.cer
Step 2: Create a property to configure the location of your certs as below in one of your configuration class
第 2 步:创建一个属性以在您的配置之一 class 中配置证书的位置,如下所示
@Value("${entrust.ssl.cert.path:config/Entrust_Root_Certification_Authority-G2.cer}")
private String entrustSslCertFilePath;
Step 3: Load these certs while creating
SslContext
第 3 步:在创建
SslContext
时加载这些证书
SslContext sslContext = SslContextBuilder.forClient()
.trustManager(new ClassPathResource(entrustSslCertFilePath).getInputStream())
.build();
Note: Above metioned i have used with pem
file which worked properly, give a try and if required feel free to convert cer
to pem
file and sslshopper will be very helpful for validating and converting ssl certs注意:上面提到的我已经使用了正常工作的
pem
文件,请尝试一下,如果需要,请随时将cer
转换为pem
文件, sslshopper将非常有助于验证和转换 ssl 证书
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.