生成keystore文件
1.找到/jre/lib/security/cacerts目录
keytool -genkey -alias test -keystore rootCA.keystore
将证书导入服务器jdk中
1.找到/jre/lib/security/cacerts目录
2.将证书上传到/jre/lib/security目录
3.执行keytool -import -noprompt -trustcacerts -alias ca -keystore /目录前缀/jre/lib/security/cacerts -file rootCA.crt
ca是别名,rootCA.crt是证书
restTemplat改造
@Bean("keystoreRestTemplate")
RestTemplate keystoreRestTemplate() throws Exception {
HttpComponentsClientHttpRequestFactory factory = new
HttpComponentsClientHttpRequestFactory();
factory.setConnectionRequestTimeout(5 * 60 * 1000);
factory.setConnectTimeout(5 * 60 * 1000);
factory.setReadTimeout(5 * 60 * 1000);
// https
SSLContextBuilder builder = new SSLContextBuilder();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
ClassPathResource resource = new ClassPathResource("keystore/rootCA.keystore");
InputStream inputStream = resource.getInputStream();
keyStore.load(inputStream, null);
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", new PlainConnectionSocketFactory())
.register("https", socketFactory).build();
PoolingHttpClientConnectionManager phccm = new PoolingHttpClientConnectionManager(registry);
phccm.setMaxTotal(200);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).setConnectionManager(phccm).setConnectionManagerShared(true).build();
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
inputStream.close();
return restTemplate;
}