将客户端证书添加到HTTP标头

时间:2018-01-08 15:41:31

标签: ssl spring-boot http-headers httpclient resttemplate

我有证书的位置。有人可以指导我如何将证书添加到RestTemplate中的HTTP标头。下面是我如何构建RestTemplate。证书需要名称为ssl-client-cert。这应该在代码中完成还是在Web服务器上配置?

public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {

SSLContext sslContext;

KeyStore keystore = KeyStore.getInstance("JKS");
FileInputStream ks = new FileInputStream("<path>");
FileInputStream ts = new FileInputStream("<path>");
keystore.load(ks,"changeit".toCharArray());
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(ts,"changeit".toCharArray());

sslContext = SSLContextBuilder.create()
.loadKeyMaterial(keystore, global.getstorePass().toCharArray())
.loadTrustMaterial(truststore, new TrustSelfSignedStrategy())
.build();

HttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .build();

return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(client))
.build();

}

0 个答案:

没有答案