Okhttp 发送https请求,忽略ssl认证

工具类

java 复制代码
import lombok.extern.slf4j.Slf4j;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;

@Slf4j
public class SSLSocketClient {

    // 获取SSLSocketFactory
    public static SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, getTrustManager(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception exception) {
            log.error("get SSLSocketFactory error", exception);
        }
        return null;
    }

    // 获取TrustManager
    private static TrustManager[] getTrustManager() {
        return new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
    }

    // 获取HostnameVerifier
    public static HostnameVerifier getHostnameVerifier() {
        return (s, sslSession) -> true;
    }

    public static X509TrustManager getX509TrustManager() {
        X509TrustManager trustManager = null;
        try {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init((KeyStore) null);
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
            if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
                throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
            }
            trustManager = (X509TrustManager) trustManagers[0];
        } catch (Exception exception) {
            log.error("get X509TrustManager error", exception);
        }
        return trustManager;
    }
}

使用okhttp

java 复制代码
private final OkHttpClient client = new OkHttpClient().newBuilder()
            .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
            .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
            .connectTimeout(60, TimeUnit.SECONDS).build();
相关推荐
初级代码游戏2 小时前
openssl 正确生成v3带SAN的证书
https·证书·ssl·openssl·tls·v3
Gworg2 小时前
网站HTTP改成HTTPS
网络协议·http·https
果果开发ggdoc.cn4 小时前
WordPress免费证书插件
服务器·https·ssl
sunnyday04267 小时前
feign调用跳过HTTPS的SSL证书校验配置详解
java·网络·https·ssl
m0_748239477 小时前
springBoot发布https服务及调用
spring boot·后端·https
钟离墨笺16 小时前
【网络协议】【http】【https】TLS1.3
网络协议·http·https
尘世壹俗人2 天前
Java如何向http/https接口发出请求
java·http·https
铁头乔2 天前
Java 中如何使用 SSL 连接 IoTDB
java·数据库·开源·ssl·时序数据库·iotdb
这样の我2 天前
springboot使用ssl连接elasticsearch
spring boot·elasticsearch·ssl
constCpp3 天前
什么是HTTP3?
网络·c++·http·https