使用RestTemplate访问https实现SSL请求操作,设置TLS版本

文章讲述了如何在Java中创建一个用于HTTPS请求的RestTemplate,通过自定义HttpsClientRequestFactory和TrustManager,允许所有证书。同时,处理了SSLHandshakeException异常,提供了升级JDK、全局设置TLS版本以及单独设置SSL信任策略的方法。

1. 添加HttpsClientRequestFactory工具类

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.X509Certificate;
 
/**
 * TLS的三个作用:
 * 	(1)身份认证
 * 		通过证书认证来确认对方的身份,防止中间人攻击
 * 	(2)数据私密性
 * 		使用对称性密钥加密传输的数据,由于密钥只有客户端/服务端有,其他人无法窥探。
 * 	(3)数据完整性
 * 		使用摘要算法对报文进行计算,收到消息后校验该值防止数据被篡改或丢失。
 *     
 *     使用RestTemplate进行HTTPS请求访问:
 * 	private static RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
 * 
 */
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
        try {
            if (!(connection instanceof HttpsURLConnection)) {
                throw new RuntimeException("An instance of HttpsURLConnection is expected");
            }
 
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; 
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        @Override
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        }
                        @Override
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        } 
                    }
            };
            // 这里修改TLS版本会被下面的覆盖
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            httpsConnection.setSSLSocketFactory(new MyCustomSSLSocketFactory(sslContext.getSocketFactory()));
 
            httpsConnection.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
 
            super.prepareConnection(httpsConnection, httpMethod);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static class MyCustomSSLSocketFactory extends SSLSocketFactory { 
        private final SSLSocketFactory delegate; 
        public MyCustomSSLSocketFactory(SSLSocketFactory delegate) {
            this.delegate = delegate;
        }
 
        // 返回默认启用的密码套件。除非一个列表启用,对SSL连接的握手会使用这些密码套件。
        // 这些默认的服务的最低质量要求保密保护和服务器身份验证
        @Override
        public String[] getDefaultCipherSuites() {
            return delegate.getDefaultCipherSuites();
        }
 
        // 返回的密码套件可用于SSL连接启用的名字
        @Override
        public String[] getSupportedCipherSuites() {
            return delegate.getSupportedCipherSuites();
        } 
 
        @Override
        public Socket createSocket(final Socket socket, final String host, final int port,
                                   final boolean autoClose) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(socket, host, port, autoClose);
            return overrideProtocol(underlyingSocket);
        } 
 
        @Override
        public Socket createSocket(final String host, final int port) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final String host, final int port, final InetAddress localAddress,
                                   final int localPort) throws
                IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final InetAddress host, final int port) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress,
                                   final int localPort) throws
                IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
            return overrideProtocol(underlyingSocket);
        }
 
        private Socket overrideProtocol(final Socket socket) {
            if (!(socket instanceof SSLSocket)) {
                throw new RuntimeException("An instance of SSLSocket is expected");
            }
            // 连接调用要升级TLS版本用这个,当支持的是列表时,能够与不同版本的客户端进行通信,在握手期间,TLS会选择两者都支持的最高的版本
            //((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.2"});
            ((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"});
            return socket;
        }
    }
}

注意:服务端TLS版本要和客户端工具类中定义的一致,

当支持的是列表时,能够与不同版本的客户端进行通信,在握手期间,TLS会选择两者都支持的最高的版本

2. 修改RestTemplate

    @Bean
    @Primary
    public RestTemplate getRestTemplate() {
        HttpsClientRequestFactory httpsClientRequestFactory = new HttpsClientRequestFactory();
        httpsClientRequestFactory.setConnectTimeout(3600);
        httpsClientRequestFactory.setReadTimeout(50 * 1000);
        RestTemplate restTemplate = new RestTemplate(httpsClientRequestFactory);
        restTemplate.setInterceptors(.....);
        return restTemplate;
    }

3、访问https,抛出的异常

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure解决方案

方法升级JDK版本

4. 全局设置TLS版本

全局设置优先级 > 代码里面的设置

-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2

5. 单独设置信任SSL的template

    @Bean(name = "sslRestTemplate")
    public RestTemplate sslRestTemplate() {
        RestTemplate restTemplate = null;
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        try {
            // 设置SSL
            TrustStrategy trustStrategy = (X509Certificate[] chain, String authType) -> true;
            SSLContext sslContexts =
                SSLContexts.custom().loadTrustMaterial(null, trustStrategy).build();
            SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContexts, new String[]{"TLSv1.2"}, null
                ,  NoopHostnameVerifier.INSTANCE);
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
            factory.setHttpClient(httpClient);
            restTemplate = new RestTemplate(factory);
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
            LOGGER.error("exception : ", e);
        }
        return restTemplate;
    }

参考文章:  使用RestTemplate访问https实现SSL请求操作_java_脚本之家

<think>我们正在讨论RestTemplate处理HTTPS请求的方法。根据引用内容,主要涉及两种方式:忽略证书验证和信任特定证书。我们将按以下步骤展开: 1. 忽略证书验证(不推荐生产环境):通过自定义ClientHttpRequestFactory,在连接时设置不验证证书。 2. 信任特定证书(推荐):将目标服务器的证书导入到信任库中,并在RestTemplate中配置使用该信任库。 另外,引用中还提到需要将证书转换为Java可识别的格式(如.jks),但转换步骤并非必须,因为Java可以直接导入.crt或.cer格式的证书。 下面我们详细说明这两种实现方式。 </think>### RestTemplate处理HTTPS请求实现方式 RestTemplate处理HTTPS请求的核心在于**SSL证书验证**,主要采取两种策略实现安全连接: #### 1. **忽略证书验证(开发测试用)** 适用于开发环境,通过自定义`ClientHttpRequestFactory`绕过证书验证: ```java public class IgnoreCertificateHttpsRequestFactory extends SimpleClientHttpRequestFactory { @Override protected void prepareConnection(HttpURLConnection connection, String method) { if (connection instanceof HttpsURLConnection) { ((HttpsURLConnection) connection).setHostnameVerifier( (hostname, session) -> true // 忽略主机名验证 ); ((HttpsURLConnection) connection).setSSLSocketFactory( getTrustAllSocketFactory() // 忽略证书验证 ); } super.prepareConnection(connection, method); } private SSLSocketFactory getTrustAllSocketFactory() { try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{ new X509TrustManager() { public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} public X509Certificate[] getAcceptedIssuers() { return null; } } }, new SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception ex) { throw new IllegalStateException("SSL配置失败", ex); } } } ``` **配置示例**: ```java @Bean public RestTemplate restTemplate() { IgnoreCertificateHttpsRequestFactory factory = new IgnoreCertificateHttpsRequestFactory(); factory.setConnectTimeout(5000); return new RestTemplate(factory); // 注入自定义Factory[^2] } ``` > ️ **风险提示**:此方法禁用SSL验证,仅限测试环境使用。 --- #### 2. **信任特定证书(生产推荐)** 通过加载证书信任库实现安全验证: ```java public class TrustStoreHttpsRequestFactory extends SimpleClientHttpRequestFactory { private final String trustStorePath; private final String password; public TrustStoreHttpsRequestFactory(String path, String pwd) { this.trustStorePath = path; this.password = pwd; } @Override protected void prepareConnection(HttpURLConnection connection, String method) { if (connection instanceof HttpsURLConnection) { HttpsURLConnection httpsConn = (HttpsURLConnection) connection; httpsConn.setSSLSocketFactory(createSslSocketFactory()); } super.prepareConnection(connection, method); } private SSLSocketFactory createSslSocketFactory() { try (InputStream keyStore = Files.newInputStream(Paths.get(trustStorePath))) { KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(keyStore, password.toCharArray()); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{ new TrustStoreTrustManager(trustStore) }, null ); return sslContext.getSocketFactory(); } catch (Exception ex) { throw new IllegalStateException("证书加载失败", ex); } } } ``` **配置步骤**: 1. **导入证书到JKS信任库**: ```bash keytool -import -file server.crt -keystore truststore.jks -storepass changeit ``` 2. **Spring配置**: ```java @Bean public RestTemplate restTemplate() { TrustStoreHttpsRequestFactory factory = new TrustStoreHttpsRequestFactory( "classpath:truststore.jks", "changeit" ); return new RestTemplate(factory); // 使用证书信任库[^3][^4] } ``` --- #### 关键对比 | 方式 | 安全性 | 适用场景 | 实现复杂度 | |--------------------|--------|----------------|------------| | 忽略证书验证 | 低 | 开发/测试环境 | 简单 | | 信任特定证书 | 高 | 生产环境 | 中等 | | 系统信任库扩展 | 高 | 多服务调用场景 | 复杂 | > **最佳实践**: > - 生产环境**必须使用证书信任机制**(方法2) > - 证书需定期更新(`keytool -import`覆盖旧证书[^4]) > - 中文编码问题通过`StringHttpMessageConverter`解决[^3]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值