HTTPS接口对接

生成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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值