在retrofit2中使用ssl,刚刚接触,很可能会出现如下错误。

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

究其原因就是没有找到本地的证书。非常简单的错误。只要将证书放在本地就可以了。

可是有时(比如说开发时、或者访问别人的https站点时),我们需要将其忽略。

这时,我们就需要将其忽略。

在iOS开发中,一句代码就可以解决。

[operation.securityPolicy setValidatesDomainName:NO];

但是在Android中使用了retrofit2后,却怎么也没有找到设置取消安全验证的方法。

网上查了很久,都是重新设置一个OKHttpClient,在OKHttpClient中进行配置。

但是我在使用中又出现了如下的问题:

compile 'com.squareup.retrofit2:retrofit:2.0.2'

我引用的retrofit包是2.0.2的,这个版本默认引用的OKHttp中,hostnameVerifier和sslSocketFactory是不可修改的。

好吧,没办法,只能用反射解决此问题了···

         OkHttpClient sClient = new OkHttpClient();
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } @Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } @Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}}, new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
} HostnameVerifier hv1 = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}; String workerClassName="okhttp3.OkHttpClient";
try {
Class workerClass = Class.forName(workerClassName);
Field hostnameVerifier = workerClass.getDeclaredField("hostnameVerifier");
hostnameVerifier.setAccessible(true);
hostnameVerifier.set(sClient, hv1); Field sslSocketFactory = workerClass.getDeclaredField("sslSocketFactory");
sslSocketFactory.setAccessible(true);
sslSocketFactory.set(sClient, sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
} Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.client(sClient)
.build();

如上,就可以屏蔽掉ssl的证书验证了~

如果有更好的办法,也请您告诉我~

retrofit2中ssl的Trust anchor for certification path not found问题的更多相关文章

  1. andorid HTTPS 不需要证书 VolleyEror: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not fou

    1.加证书(这里不说) 2.修改代码 import java.security.KeyManagementException;import java.security.NoSuchAlgorithmE ...

  2. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  3. java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...

  4. java中 SSL认证和keystore使用

    java中 SSL认证和keystore使用 2013-10-12 11:08 10488人阅读 评论(0) 收藏 举报   目录(?)[+]     好久没用过SSL认证了,东西久不用,就有点生疏. ...

  5. JAVA中SSL证书认证通讯

    JAVA中SSL证书认证通讯 SSL通讯服务端 /******************************************************************** * 项目名称 ...

  6. 目标检测中的anchor-based 和anchor free

    目标检测中的anchor-based 和anchor free 1.  anchor-free 和 anchor-based 区别 深度学习目标检测通常都被建模成对一些候选区域进行分类和回归的问题.在 ...

  7. 解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target

    最近在写java的一个服务,需要给远程服务器发送post请求,认证方式为Basic Authentication,在请求过程中出现了 PKIX path building failed: sun.se ...

  8. maven配置下载包 解决SunCertPathBuilderException:unable to find valid certification path to requested target

    ​ 解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题 ★ ...

  9. azure iothub create-device-identity样例报错: unable to find valid certification path ,及iothub-explorer Error: CERT_UNTRUSTED

    https://docs.microsoft.com/zh-cn/azure/iot-hub/iot-hub-java-java-getstarted 在IDEA中执行上述的代码,会出现下面的报错信息 ...

随机推荐

  1. 【转载】MySQL启多个实例

    很多朋友都想在一台服务器上运行多个MySQL Instance,究竟怎么做呢?首先要明晰几个原理, 简称为mysqld读取my.cnf的顺序:第一搜,首先读取/etc/my.cnf,多实例这个配置文件 ...

  2. lnmp之php5.3.27 编译信息

    ./configure \--prefix=/application/php5.3.27 --with-mysql=application/mysql \--with-iconv-dir=/usr/l ...

  3. maven-shade-plugin

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  4. Let's Encrypt 正式出發(免费HTTPS证书即将到来)

    转自:https://blog.gslin.org/archives/2015/10/20/6073/lets-encrypt-%E6%AD%A3%E5%BC%8F%E5%87%BA%E7%99%BC ...

  5. 高性能MySQL笔记:第1章 MySQL架构

    MySQL 最重要.最与众不同的特性是他的存储引擎架构,这种架构的设计将查询处理(Query Precessing)及其系统任务(Server Task)和数据的存储/提取相分离.   1.1 MyS ...

  6. php反射机制

    PHP5添加了一项新的功能:Reflection.这个功能使得phper可以reverse-engineer class, interface,function,method and extensio ...

  7. Configure Ocserv on CentOS 6

    Configure Ocserv on CentOS 6 Table of Contents 1. Install ocserv 2. Configure ocserv 3. How to host ...

  8. Spring学习之第一个Spring MVC程序(IDEA开发环境)

    回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的时代.一般接触到一个新的Web框架,都会想问这个框 ...

  9. app微信支付(一) - 微信支付基本业务流程解析

    想必微信支付是现在很多朋友离不开的一个功能,不论是扫描支付还是公众号支付或者app端支付,基本的思路都是一样的,之前做过支付宝支付以及中国移动支付接口,这几天花时间讲讲微信支付吧 先看一下微信支付的时 ...

  10. POJ3321Apple Tree[树转序列 BIT]

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26995   Accepted: 8007 Descr ...