需要访问带私有证书的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
二, 问题解决:
-
在res里创建raw目录,放入你的证书文件
-
在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>
- 在AndroidManifest.xml中引用network-security-config.xml配置文件
<application
... 省略 ...
android:networkSecurityConfig="@xml/network_security_config"
... 省略 ...
>
- 现在问题已经解决了,Rebuild Project项目之后,访问正常