uni-app离线打包android设置https私有证书,解决Trust anchor for certification path not found报错,sslVerify: false无效

需要访问带私有证书的https接口,在离线打包成安卓app后,在页面接口中设置跳过证书是无效的(sslVerify: false),目前的解决方案是把私有证书放在安卓app中

一,这是后台报错信息

com.android.simple                   W  javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor certification path not found.
com.android.simple                   W  	at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:219)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.RealConnection.connectTls(SourceFile:22)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.RealConnection.establishProtocol(SourceFile:15)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.RealConnection.connect(SourceFile:41)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.StreamAllocation.findConnection(SourceFile:92)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.StreamAllocation.findHealthyConnection(SourceFile:
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.StreamAllocation.newStream(SourceFile:8)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.connection.ConnectInterceptor.intercept(SourceFile:7)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:23)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:1)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.cache.CacheInterceptor.intercept(SourceFile:41)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:23)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:1)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.BridgeInterceptor.intercept(SourceFile:65)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:23)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(SourceFile:21)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:23)
com.android.simple                   W  	at dc.squareup.okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:1)
com.android.simple                   W  	at dc.squareup.okhttp3.RealCall.getResponseWithInterceptorChain(SourceFile:16)
com.android.simple                   W  	at dc.squareup.okhttp3.RealCall.execute(SourceFile:10)
com.android.simple                   W  	at io.dcloud.feature.weex.adapter.DCWXHttpAdapter$1.run(SourceFile:19)
com.android.simple                   W  	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
com.android.simple                   W  	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
com.android.simple                   W  	at java.lang.Thread.run(Thread.java:764)
com.android.simple                   W  Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException:Trust anchor for certification path not found.
com.android.simple                   W  	at com.android.org.conscrypt.TrustManagerImpl.checkTrustedRecursive(TrustManagerImpl.java:646
com.android.simple                   W  	at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:495)
com.android.simple                   W  	at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:418)
com.android.simple                   W  	at com.android.org.conscrypt.TrustManagerImpl.getTrustedChainForServer(TrustManagerImpl.java:339)
com.android.simple                   W  	at android.security.net.config.NetworkSecurityTrustManager.checkServerTrusted(NetworkSecurityTrustManager.java:94)
com.android.simple                   W  	at android.security.net.config.RootTrustManager.checkServerTrusted(RootTrustManager.java:88)
com.android.simple                   W  	at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:197)
com.android.simple                   W  	at com.android.org.conscrypt.ConscryptFileDescriptorSocket.verifyCertificateChain(ConscryptFileDescriptorSocket.java:399)
com.android.simple                   W  	at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
com.android.simple                   W  	at com.android.org.conscrypt.SslWrapper.doHandshake(SslWrapper.java:374)
com.android.simple                   W  	at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:217)
com.android.simple                   W  	... 23 more
com.android.simple                   W  Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
com.android.simple                   W  	... 34 more

二, 问题解决:

  1. 在res里创建raw目录,放入你的证书文件
    在这里插入图片描述

  2. 在res里创建xml目录,创建network-security-config.xml文件,内容如下,其中src就配置了引用第1步里的证书,证书名不用写后缀

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config>
        <trust-anchors>
            <certificates src="@raw/zs"/>
            <certificates src="system"/>
        </trust-anchors>
    </base-config>
</network-security-config>

在这里插入图片描述

  1. 在AndroidManifest.xml中引用network-security-config.xml配置文件
    <application
        ... 省略 ...
        android:networkSecurityConfig="@xml/network_security_config"
        ... 省略 ...
    >

在这里插入图片描述

  1. 现在问题已经解决了,Rebuild Project项目之后,访问正常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值