CertPathValidatorException: Trust anchor for certification path not found解決方法
1. 概述
如果我們的專案新增了 OkHttpUtils 的證照,如果報這個錯,解決方案如下,直接 複製 trustAllHosts() 方法到自己專案中即可,或者根據 這個方法修改自己對應程式碼即可;
錯誤log:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path
not found.
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:413)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:210)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:477)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
at com.etnet.global.ConnectionTool.post(ConnectionTool.java:462)
at com.etnet.android.Welcome$3.run(Welcome.java:177)
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for
certification path not found.
at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:184)
at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:593)
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
... 10 more
解決方法:忽略證照驗證:
public static StringBuffer post(String urlString, String param) {
StringBuffer sb = new StringBuffer();
URL url = null;
HttpURLConnection connection = null;
DataOutputStream out = null;
BufferedReader in = null;
try {
url = new URL(urlString);
//關鍵程式碼
//ignore https certificate validation |忽略 https 證照驗證
if (url.getProtocol().toUpperCase().equals("HTTPS")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url
.openConnection();
https.setHostnameVerifier(HttpIgnoreSSL.DO_NOT_VERIFY);
connection = https;
} else {
connection = (HttpURLConnection) url.openConnection();
}
connection.setReadTimeout(10000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(param);
out.flush();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()), 512);
String line;
while ((line = in.readLine()) != null) {
sb.append(line.trim());
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (connection != null) {
connection.disconnect();
}
}
return sb;
}
public static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
// Android use X509 cert
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
public final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
相關文章
- react-native install 報錯 No git binary found in $PATH解決方法ReactGit
- 專案出現 The superclass “javax.servlet.http.HttpServlet“ was not found on the Java Build Path 解決方法JavaServletHTTPUI
- SSL - SSLHandshakeException: unable to find valid certification path to requested targetException
- bash: lspci: command not found解決方法
- 解決Vue中”This dependency was not found”的方法Vue
- mac xcrun: error: active developer path 解決方法MacErrorDeveloper
- Linux提示crontab command not found的解決方法Linux
- 小程式錯誤:Component is not found in path
- More than one file was found with OS independent path
- mysql中出現Unit mysql.service could not be found 的解決方法MySql
- ArchLinux長時間不更新永遠會有的unknown trust報錯解決方法彙總LinuxRust
- Spring 異常關鍵字 no matching editors or conversion strategy found 解決方法Spring
- VS2022推送程式碼 到github錯誤: CertGetCertificateChain trust error CERT_TRUST_IS_PARTIAL_CHAIN的解決辦法GithubAIRustError
- Package libxml-2.0 was not found in the pkg-config search pathPackageXML
- -bash: XXX: command not found解決辦法
- 解決vue3 頁面重新整理控制檯報[Vue Router warn]: No match found for location with path“xxxxx” 警告問題Vue
- 編譯ffmpeg遇到ERROR: x265 not found using pkg-config解決方法編譯Error
- 解決bash: mysql: command not found的問題MySql
- android sqlite3 not found 解決總結AndroidSQLite
- Mac終端出現 brew command not found 解決Mac
- zabbix_get :command not found 解決辦法
- chrome谷歌瀏覽器視訊播放報錯:No compatible source was found for this media解決方法Chrome谷歌瀏覽器
- jsp錯誤: "javax.servlet.http.HttpServlet" was not found on the Java Build PathJSJavaServletHTTPUI
- “command-not-found has crashed” 解決辦法 (*unsolved)
- 解決nltk_data LookupError: Resource punkt not found.Error
- 解決shell指令碼錯誤$’r’ command not found指令碼
- 解決 The ‘supervisor==3.2.0‘ distribution was not found and is required by the application錯誤UIAPP
- #Schpringbot#_bug解決_jsp檔案not_foundJS
- “getfacl: Removing leading '/' from absolute path names”解決辦法REM
- springboot專案解決 No beans of 'UserDao' type found 問題Spring BootBean
- 解決 biolinux-conda command not found 2018-05-04Linux
- IDEA+Maven:invalid bound statement (not found)解決辦法IdeaMaven
- /system/bin/sh: screenrecord: inaccessible or not found 報錯解決方案
- CentOS 7 命令 lsb_release: command not found 解決方案CentOS
- Taglist: Exuberant ctags (http://ctags.sf.net) not found in PATH. Plugin is not loaded.HTTPPlugin
- Trust Region Policy OptimizationRust
- IDEA+Maven+Springboot:invalid bound statement (not found) 解決辦法IdeaMavenSpring Boot
- This dependency was not found: * !!vue-style-loader!css-loader? 解決方案VueCSS