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.net.ssl.SSLHandshakeException: The server selected protocol version TLS10 is not accepted by client preferences TLS13, TLS12

2. 解决

  • 修改java.security文件

    bash 复制代码
    # 以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代码

    java 复制代码
    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);
    }
相关推荐
梦帮科技1 分钟前
Web 漏洞攻防深度:WAF 规则逃逸技术(畸形分块传输、双重编码)与对抗加固
linux·前端·网络安全·ssl·web
\光辉岁月/2 分钟前
15.JavaSE-网络
java·开发语言·网络
const_qiu3 分钟前
第二章 2.4:搭建 Monorepo 骨架 + 共享包 @autix/contracts
java·人工智能·arcgis
wangchunyu11436 分钟前
JDK8新特性实战(Lambda、Stream、Optional)
java·开发语言
JosieBook1 小时前
【多线程】两台机器同时抢一个字典:一次并发 Bug 的排查
java·网络·bug
牛战士coding1 小时前
LLM 选错工具怎么办?提示词之外还需要确定性规则兜底
java·人工智能
今天的砖头有点烫手啊1 小时前
Java 序列化到底怎么选?从 Serializable 到 Protobuf 的演进与选型
java·json
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 登录发送验证码的测试验证与经验复盘
java·开发语言·交友
夕除1 小时前
redis--主从还是集群,内存
java·spring·mybatis
百数平台1 小时前
低代码安全技术实践:表单 / 流程 / 报表三级权限体系设计解析
java·大数据·数据库