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);
    }
相关推荐
雨落在了我的手上37 分钟前
初始java(十):类和对象(⼆)
java·开发语言
莫雪歌1 小时前
Java AI 应用开发实践:基于 Spring Boot 实现 Chat、Memory、RAG 与 Tool Calling
java·aigc
SmartBrain2 小时前
AI全栈开发(SDD):慢病管理系统工程级设计
java·大数据·开发语言·人工智能·架构·aigc
梦想CAD控件2 小时前
网页端对DWG图纸进行预览与批注(CAD轻量化)
java·前端·javascript
老毛肚2 小时前
Spring boot 特性和自写Reids组件
java·spring boot·后端
极光代码工作室2 小时前
基于SpringBoot的课程管理系统
java·springboot·web开发·后端开发
JustNow_Man2 小时前
【opencode】安装使用daytona沙箱插件
android·java·javascript
武子康3 小时前
Java-05 深入浅出 MyBatis动态SQL与参数拼接完全指南
java·spring boot·后端
过期动态3 小时前
【LeetCode 热题 100】字母异位分组
java·算法·leetcode·职场和发展·哈希算法
辰海Coding3 小时前
MiniSpring框架学习-为什么一个请求访问 /helloworld,最后能调用到某个 Controller 方法?原始 MVC实现
java·学习·程序人生·spring·mvc