IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 【OKHttp】javax.net.ssl. SSLHandshakeException:PKIX path building failed -> 正文阅读

[网络协议]【OKHttp】javax.net.ssl. SSLHandshakeException:PKIX path building failed

Android异常:java.lang.IllegalStateException: Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl

web、java项目异常:javax.net.ssl. SSLHandshakeException:PKIX path building failed

以下解决,第一步创建NullHostNameVerifier

package com.malx.signature.network;

import android.annotation.SuppressLint;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

/**
 * @author by maliang on 2021/12/13 17:25
 * #First Created Time:
 * #包名:com.malx.signature
 * class description:用于主机名验证,此不校验允许所有。
 */
public class NullHostNameVerifier implements HostnameVerifier {
    /**
     * Verify that the host name is an acceptable match with
     * the server's authentication scheme.
     *
     * @param hostname the host name
     * @param session  SSLSession used on the connection to host
     * @return true if the host name is acceptable
     */
    @SuppressLint("BadHostnameVerifier")
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

第二步 :

创建OkHttpClient示例(中间其他配置代码省略...):

import java.net.Proxy;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

...

OkHttpClient.Builder builder = new OkHttpClient.Builder();
...
try {
    X509TrustManager trustManager = new X509TrustManager() {

                /**
                 * Given the partial or complete certificate chain provided by the
                 * peer, build a certificate path to a trusted root and return if
                 * it can be validated and is trusted for client SSL
                 * authentication based on the authentication type.
                 * <p>
                 * The authentication type is determined by the actual certificate
                 * used. For instance, if RSAPublicKey is used, the authType
                 * should be "RSA". Checking is case-sensitive.
                 *
                 * @param chain    the peer certificate chain
                 * @param authType the authentication type based on the client certificate
                 * @throws IllegalArgumentException if null or zero-length chain
                 *                                  is passed in for the chain parameter or if null or zero-length
                 *                                  string is passed in for the  authType parameter
                 * @throws CertificateException     if the certificate chain is not trusted
                 *                                  by this TrustManager.
                 */
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                /**
                 * Given the partial or complete certificate chain provided by the
                 * peer, build a certificate path to a trusted root and return if
                 * it can be validated and is trusted for server SSL
                 * authentication based on the authentication type.
                 * <p>
                 * The authentication type is the key exchange algorithm portion
                 * of the cipher suites represented as a String, such as "RSA",
                 * "DHE_DSS". Note: for some exportable cipher suites, the key
                 * exchange algorithm is determined at run time during the
                 * handshake. For instance, for TLS_RSA_EXPORT_WITH_RC4_40_MD5,
                 * the authType should be RSA_EXPORT when an ephemeral RSA key is
                 * used for the key exchange, and RSA when the key from the server
                 * certificate is used. Checking is case-sensitive.
                 *
                 * @param chain    the peer certificate chain
                 * @param authType the key exchange algorithm used
                 * @throws IllegalArgumentException if null or zero-length chain
                 *                                  is passed in for the chain parameter or if null or zero-length
                 *                                  string is passed in for the  authType parameter
                 * @throws CertificateException     if the certificate chain is not trusted
                 *                                  by this TrustManager.
                 */
                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                }

                /**
                 * Return an array of certificate authority certificates
                 * which are trusted for authenticating peers.
                 *
                 * @return a non-null (possibly empty) array of acceptable
                 * CA issuer certificates.
                 */
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new X509TrustManager[]{trustManager}, new SecureRandom());
            builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
            builder.hostnameVerifier(new NullHostNameVerifier());
        } catch (IllegalStateException | NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }
// 不使用代理,防止抓包
builder.proxy(Proxy.NO_PROXY);
// 解决网络java.net.SocketException: Socket closed问题
builder.addNetworkInterceptor(new Interceptor() {
            @NonNull
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                // ("Connection", "keep-alive") close
                Request.Builder newBuilder = chain.request().newBuilder();
                // 这个是默认情况下,如果服务器自定义需要使用自定义方式解决
                newBuilder.addHeader("Connection", "keep-alive")
                        .addHeader("Accept-Charset", "UTF-8");
                Request request = newBuilder.build();
                return chain.proceed(request);
            }
        });
OkHttpClient okHttpClient = builder.build();
...

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-08 22:56:03  更:2022-03-08 22:56:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 7:16:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码