2
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.NETWORK
嗨而我打電話從改造一個API服務,我得到這個錯誤,我搜索了很多,發現答案像java.security.cert.CertPathValidatorException:信任錨認證路徑不found.NETWORK
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(ROOT)
//.setClient(new OkClient(new com.squareup.okhttp.OkHttpClient()))
//.setClient(getOkClient())
.setClient(setSSLFactoryForClient(new com.squareup.okhttp.OkHttpClient()))
.setRequestInterceptor(new SessionRequestInterceptor())
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(new AndroidLog(NetworkUtil.APP_TAG))
.build();
REST_CLIENT = restAdapter.create(Restapi.class);
}
// SET SSL
public static OkClient setSSLFactoryForClient(OkHttpClient client) {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
client.setSslSocketFactory(sslSocketFactory);
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
return new OkClient(client);
}
使用setSSLFactoryForClient方法後,它做工精細,但我不明白怎麼回事錯的,什麼這個方法做,我知道這個問題是關係到SSL證書認證,但任何一個可以解釋我這短暫請
非常感謝尤里!當我更改URL(所有方法相同只是更改url)到其他服務器,它給了我完美的迴應,是否意味着服務器URL沒有給予迴應使用一些安全屏障,並且此代碼繞過 –
「這是禁用安全的SSL。這對本地測試是正確的,但不適合真實用戶使用。「正如你所說的那樣,而不是什麼正確的方法來解決這個問題 –
如果其他服務器生成一個有效的證書,並被其他客戶端接受,例如捲曲在桌面上。然後,修復程序可能會在運行時或通過keytool將其頒發的證書添加到信任庫。 例如https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/CustomTrust.java 如果其他服務器只使用測試證書,那麼您的代碼適用於測試。 –