Java RestTemplate使用TLS1.0(关闭SSL验证)

编程入门 行业动态 更新时间:2024-10-27 04:35:55

Java <a href=https://www.elefans.com/category/jswz/34/1744999.html style=RestTemplate使用TLS1.0(关闭SSL验证)"/>

Java RestTemplate使用TLS1.0(关闭SSL验证)

1. 问题

使用RestTemplate调用Http API时,服务器是TLS1.0,但是客户端Java默认禁止TLS1.0,会报错:org.springframework.web.client.ResourceAccessException: I/O error on POST request for “https://10.255.200.114/health”: The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]; nested exception is javax.ssl.SSLHandshakeException: The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]

2. 解决

  • 修改java.security文件

    # 以MacOS为例
    vi /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/conf/security/java.security
    # 将TLSv1.0 从jdk.tls.disabledAlgorithms中删除
    jdk.tls.disabledAlgorithms=SSLv3, TLSv1.1, RC4, DES, MD5withRSA, \
    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
    include jdk.disabled.namedCurves
    
  • RestTemplate代码

    public RestTemplate restTemplate() {SSLConnectionSocketFactory scsf = null;try {scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),NoopHostnameVerifier.INSTANCE);} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (KeyManagementException e) {throw new RuntimeException(e);} catch (KeyStoreException e) {throw new RuntimeException(e);}Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", scsf).build();PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);connectionManager.setMaxTotal(200);connectionManager.setDefaultMaxPerRoute(100);connectionManager.setValidateAfterInactivity(1000);RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(45000).setConnectTimeout(45000)// Timeout waiting for connection from pool.setConnectionRequestTimeout(45000).build();ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build());return new RestTemplate(factory);
    }
    

更多推荐

Java RestTemplate使用TLS1.0(关闭SSL验证)

本文发布于:2023-12-06 05:33:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RestTemplate   Java   SSL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!