Skip to content

Commit

Permalink
Fix CertPathValidatorException: Trust anchor for certification path n…
Browse files Browse the repository at this point in the history
…ot found.
  • Loading branch information
xingstarx committed Jun 3, 2018
1 parent e7cc74e commit 52ff2e8
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions app/src/main/java/org/houxg/leamonax/network/ApiProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
import org.houxg.leamonax.network.api.UserApi;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
Expand Down Expand Up @@ -70,6 +78,37 @@ public void log(String message) {
builder.addNetworkInterceptor(interceptor);
builder.addNetworkInterceptor(new StethoInterceptor());
}
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
try {
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
builder.connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS);
OkHttpClient client = builder.build();
mApiRetrofit = new Retrofit.Builder()
.baseUrl(host + "/api/")
Expand Down

1 comment on commit 52ff2e8

@spacemeowx2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个commit是搞笑吗? 为了安全上了https, 然后你这再把校验干掉, 不加选项?

Please sign in to comment.