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);
    }
相关推荐
wangbing11252 分钟前
开发指南142-类和字符串转换
java·开发语言
A懿轩A15 分钟前
【Java 基础编程】Java 集合框架详解:List/Set/Map 选型 + ArrayList/HashMap 原理与使用
java·windows·list
毕设源码-赖学姐17 分钟前
【开题答辩全过程】以 基于SpringBoot 的个人健康分析指导系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
范什么特西35 分钟前
配置Maven准备工作
java·maven
海兰1 小时前
Elasticsearch Java 客户端(9.x)
java·elasticsearch·jenkins
重生之后端学习1 小时前
17. 电话号码的字母组合
java·开发语言·数据结构·算法·深度优先
蜜獾云1 小时前
JAVA面试题速记-redis知识点
java·开发语言·redis
Drifter_yh2 小时前
「JVM」 深入理解 StringTable:从底层编译优化到 intern 核心解密
java·jvm
JavaLearnerZGQ2 小时前
Spring Boot 流式响应接口核心组件解析
java·spring boot·后端
cur1es2 小时前
【TCP 协议的相关特性】
java·网络·网络协议·tcp/ip·tcp·滑动窗口·连接管理