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
- configure: error: no acceptable C compiler found in $PATH 問題解決ErrorCompile
- 解決Vue中”This dependency was not found”的方法Vue
- mac xcrun: error: active developer path 解決方法MacErrorDeveloper
- bash: lspci: command not found解決方法
- symfony 2 No route found for “GET /” 的解決方法
- SSL - SSLHandshakeException: unable to find valid certification path to requested targetException
- Linux提示crontab command not found的解決方法Linux
- 'libxml/HTMLparser.h' file not found in ASIHTTPRequest 解決方法XMLHTMLHTTP
- ORA-01403:no data found 解決方法兩則
- 解決 command not found: expressExpress
- AIX dtterm not found的解決AI
- 小程式錯誤: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
- 解決direct path read 與 direct path write問題
- sqlplus: command not found解決SQL
- "javax.servlet.http.HttpServlet" was not found on the Java Build PathJavaServletHTTPUI
- ArchLinux長時間不更新永遠會有的unknown trust報錯解決方法彙總LinuxRust
- Invalid bound statement (not found)錯誤解決
- 解決警告“ld: warning: directory not found for option”
- 【使用tar解壓檔案到指定目錄時出現Not found in archive的解決方法 】Hive
- Spring 異常關鍵字 no matching editors or conversion strategy found 解決方法Spring
- mac執行git命令出現xcrun: error: invalid active developer path解決方法MacGitErrorDeveloper
- 解決bash: mysql: command not found的問題MySql
- android sqlite3 not found 解決總結AndroidSQLite
- Mac終端出現 brew command not found 解決Mac
- 解決ntp的錯誤 no server suitable for synchronization foundServerUI
- zabbix_get :command not found 解決辦法
- 安裝過程中出現PKG_CONFIG_PATH的問題解決方法
- Package libxml-2.0 was not found in the pkg-config search pathPackageXML
- 解決shell指令碼錯誤$’r’ command not found指令碼
- -bash: XXX: command not found解決辦法
- 解決nltk_data LookupError: Resource punkt not found.Error
- 404 not found是什麼意思?該怎麼解決?
- chrome谷歌瀏覽器視訊播放報錯:No compatible source was found for this media解決方法Chrome谷歌瀏覽器
- Do not trust anybody!Rust