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();
相关推荐
ZachOn1y14 小时前
计算机网络:应用层 —— 应用层概述
计算机网络·http·https·应用层·dns
网安墨雨21 小时前
iOS应用网络安全之HTTPS
web安全·ios·https
低调之人1 天前
Fiddler勾选https后google浏览器网页访问不可用
前端·测试工具·https·fiddler·hsts
x66ccff1 天前
HTTPS如何通过CA证书实现安全通信,以及HTTPS的局限性
网络协议·安全·https
Graceful_scenery1 天前
https双向认证
服务器·网络·网络协议·http·https
TroubleMaker1 天前
OkHttp源码学习之retryOnConnectionFailure属性
android·java·okhttp
幽弥千月1 天前
【ELK】ES单节点升级为集群并开启https【亲测可用】
elk·elasticsearch·https
TroubleMaker2 天前
OkHttp源码学习之Interceptor
android·okhttp
cdcdhj2 天前
在window环境下安装openssl生成钥私、证书和签名,nodejs利用express实现ssl的https访问和测试
https·ssl·express