欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android app去掉https签名校验

程序员文章站 2022-05-25 21:18:21
本文同步至http://javaexception.com/archives/30 问题: 之前的一个开源项目碰到了一个问题,Fix CertPathValidatorException: Trust anchor for certification path not found. 问题在于自建后台 ......

本文同步至

问题:

之前的一个开源项目碰到了一个问题,fix certpathvalidatorexception: trust anchor for certification path not found.

问题在于自建后台的站点用的是免费的ssl证书,okhttp默认会进行https签名校验,所以需要去掉这种校验。

 

解决办法:

okhttpclient.builder builder = new okhttpclient.builder();
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();

 

链接如下:

 

这个开源项目也值得学习下