项目安全升级,使用https协议,老板不出钱,只能使用自签名证书的方式,发现Filedownload 无法下载文件,报错:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 大致意思:信任锚认证路径找不到。
自签名证书没有经过CA认证,肯定会抛此异常,没办法,谁让用的是自签名的证书呢, 只能先忽略SSL认证,开始找FileDownload忽略SSL验证的相关接口,百度一圈无果,难道没人遇到这样的问题吗?
经过求助作者得到以下答案:
有思路了, 哐哐哐就是一顿干,实现了FileDownloadConnection的接口,写了一个自己的FileDownloadUrlConnection
满心欢喜的验证结果,结果不出意外, 没有生效。是我写的不对? 仔细查看lssues,发现了新大陆
作者自己有实现过FileDownloadUrlConnection, 使用的是okhttp(默认是httpurlconnection),赶紧拿来试试,先下载一个http的链接,叮!!!成功了。赶紧添加忽略SSL的代码,换成https 的链接试试,好的,成功。。
方案总结:
1. 使用作者实现的OkHttp3Connection替换FileDownload的默认连接方式(httpurlconnection)
2. 在创建okhttp实例的时候添加忽略SSL验证的代码
3. 使用FileDownloader.setupOnApplicationOnCreate(this).connectionCreator(....)的方式注册FileDownload
贴上全部源码:
OkHttp3Connection.java(作者实现的)
import com.liulishuo.filedownloader.connection.FileDownloadConnection;
import com.liulishuo.filedownloader.util.FileDownloadHelper;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.util.List;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* The FileDownloadConnection implemented with the okhttp3.
*/
public class OkHttp3Connection implements FileDownloadConnection {
final OkHttpClient mClient;
private final Request.Builder mRequestBuilder;
private Request mRequest;
private Response mResponse;
OkHttp3Connection(Request.Builder builder, OkHttpClient client) {
mRequestBuilder = builder;
mClient = client;
}
public OkHttp3Connection(String url, OkHttpClient client) {
this(new Request.Builder().url(url), client);
}
@Override
public void addHeader(String name, String value) {
mRequestBuilder.addHeader(name, value);
}
@Override
public boolean dispatchAddResumeOffset(String etag, long offset) {
return false;
}
@Override
public InputStream getInputStream() throws IOException {
if (mResponse == null) throw new IOException("Please invoke #execute first!");
final ResponseBody body = mResponse.body();
if (body == null) throw new IOException("No body found on response!");
return body.byteStream();
}
@Override
public Map> getRequestHeaderFields() {
if (mRequest == null) {
mRequest = mRequestBuilder.build();
}
return mRequest.headers().toMultimap();
}
@Override
public Map> getResponseHeaderFields() {
return mResponse == null ? null : mResponse.headers().toMultimap();
}
@Override
public String getResponseHeaderField(String name) {
return mResponse == null ? null : mResponse.header(name);
}
@Override public boolean setRequestMethod(String method) throws ProtocolException {
mRequestBuilder.method(method, null);
return true;
}
@Override
public void execute() throws IOException {
if (mRequest == null) {
mRequest = mRequestBuilder.build();
}
mResponse = mClient.newCall(mRequest).execute();
}
@Override
public int getResponseCode() throws IOException {
if (mResponse == null) throw new IllegalStateException("Please invoke #execute first!");
return mResponse.code();
}
@Override
public void ending() {
mRequest = null;
mResponse = null;
}
/**
* The creator for the connection implemented with the okhttp3.
*/
public static class Creator implements FileDownloadHelper.ConnectionCreator {
private OkHttpClient mClient;
private OkHttpClient.Builder mBuilder;
public Creator() {
}
/**
* Create the Creator with the customized {@code client}.
*
* @param builder the builder for customizing the okHttp client.
*/
public Creator(OkHttpClient.Builder builder) {
mBuilder = builder;
}
/**
* Get a non-null builder used for customizing the okHttpClient.
*
* If you have already set a builder through the construct method, we will return it directly.
*
* @return the non-null builder.
*/
public OkHttpClient.Builder customize() {
if (mBuilder == null) {
mBuilder = new OkHttpClient.Builder();
}
return mBuilder;
}
@Override
public FileDownloadConnection create(String url) throws IOException {
if (mClient == null) {
synchronized (Creator.class) {
if (mClient == null) {
mClient = mBuilder != null ? mBuilder.build() : new OkHttpClient();
mBuilder = null;
}
}
}
return new OkHttp3Connection(url, mClient);
}
}
}
在Application中注册
//下载器初始化, 使用okhttp下载引擎
FileDownloader.setupOnApplicationOnCreate(this).connectionCreator(new OkHttp3Connection.Creator(SSLUtils.createOkHttp()));
创建Okhttp实例并忽略SSL验证
/**
* filedownload 使用okhttp作为下载引擎忽略掉SSL验证, 添加证书不好使
* @return
*/
public static OkHttpClient.Builder createOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20_000, TimeUnit.SECONDS);
builder.sslSocketFactory(SSLUtils.createSSLSocketFactory(), new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
});
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder;
}
记录自己解决问题的点滴,感谢FileDownload的作者的答复,如果有遇到此问题的同胞,希望可以帮到你。
祝:工作顺利!