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);
    }
相关推荐
apocelipes1 小时前
golang unique包和字符串内部化
java·python·性能优化·golang
Full Stack Developme2 小时前
java.text 包详解
java·开发语言·python
刘梦凡呀3 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
best_virtuoso3 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian20143 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
楚韵天工4 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
helloworddm4 小时前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
失散134 小时前
分布式专题——43 ElasticSearch概述
java·分布式·elasticsearch·架构
ajsbxi4 小时前
【Java 基础】核心知识点梳理
java·开发语言·笔记
聪明的笨猪猪4 小时前
Java JVM “调优” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试