Closed hughdev closed 7 years ago
hello。: 还有ssl,https;如何进行配置呢? 谢谢开源,感谢开源哦
可以详细查看博客http://blog.csdn.net/wzgiceman/article/details/52981059
api 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(basePar.getConnectionTime(), TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.readTimeout(basePar.getConnectionTime(), TimeUnit.SECONDS)
.writeTimeout(basePar.getConnectionTime(), TimeUnit.SECONDS)
.followRedirects(false)
.followSslRedirects(true)
.cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(RxRetrofitApp.getApplication())))
.sslSocketFactory(UnsafeUtils.obtainSSLSocketFactory(), UnsafeUtils.TRUST_ALL_CERTS)
.hostnameVerifier(UnsafeUtils.ACCEPT_ALL_HOSTNAME)
/**
* 不安全的服务器证书管理及主机名校验
* <p>
* Create by liyujiang on 2019/3/16 10:17
*/
public class UnsafeUtils {
public static final UnSafeTrustManager TRUST_ALL_CERTS = new UnSafeTrustManager();
public static final UnSafeHostnameVerifier ACCEPT_ALL_HOSTNAME = new UnSafeHostnameVerifier();
public static SSLSocketFactory obtainSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
// 第一个参数是授权的密钥管理器,用来授权验证,比如授权自签名的证书验证。第二个是被授权的证书管理器,用来验证服务器端的证书
sslContext.init(null, new TrustManager[]{TRUST_ALL_CERTS}, new SecureRandom());
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return (SSLSocketFactory) SSLSocketFactory.getDefault();
}
/**
* 为了解决客户端不信任服务器数字证书的问题,网络上大部分的解决方案都是让客户端不对证书做任何检查,
* 这是一种有很大安全漏洞的办法
*/
@SuppressLint("TrustAllX509TrustManager")
private static class UnSafeTrustManager implements 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[]{};
}
}
/**
* 此类是用于主机名验证的基接口。 在握手期间,如果 URL 的主机名和服务器的标识主机名不匹配,
* 则验证机制可以回调此接口的实现程序来确定是否应该允许此连接。策略可以是基于证书的或依赖于其他验证方案。
* 当验证 URL 主机名使用的默认规则失败时使用这些回调。如果主机名是可接受的,则返回 true
*/
@SuppressLint("BadHostnameVerifier")
private static class UnSafeHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}
rt!保存、添加、删除?怎么做呢