Android okHttp:Https之SSL - Hostname was not verified

欢迎各位访问的博客,博客地址

踩坑篇

项目服务端传输协议换成了https,客户端打接口一直报:Hostname 'url' was not verified
原因:服务端https的证书没有过审
解决方案(直接上代码):

private static void allowAllSSL() {
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[]{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 null;
                }
            }}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        mOkHttpClient.setSslSocketFactory(sc.getSocketFactory());
        mOkHttpClient.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }

主要是这里:

mOkHttpClient.setSslSocketFactory(sc.getSocketFactory());
        mOkHttpClient.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }

具体分析可以去了解下SSL安全协议,这样写之后实际是降低了https的安全性了,不过证书不过审只能这样了。

已标记关键词 清除标记
<div><p>I'm getting an error connecting to my Plex server:</p> <pre><code> 2019-02-04 14:34:32.955 ERROR 16 --- [ main] com.jasonhhouse.Gaps.GapsApplication : Error connecting to Plex to get Movie list javax.net.ssl.SSLPeerUnverifiedException: Hostname <ip address> not verified: certificate: sha256/OLw8G/2bRkH+g5KpGgWsw9Jy2SVx38Y5GUz8jO/9a9w= DN: CN=*.<domain>.plex.direct, O="Plex, Inc.", L=Los Gatos, ST=California, C=US subjectAltNames: [*.<domain>.plex.direct] at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:330) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:283) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:168) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) ~[okhttp-3.12.1.jar:na] at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) ~[okhttp-3.12.1.jar:na] at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254) ~[okhttp-3.12.1.jar:na] at okhttp3.RealCall.execute(RealCall.java:92) ~[okhttp-3.12.1.jar:na] at com.jasonhhouse.Gaps.GapsApplication.findAllPlexMovies(GapsApplication.java:101) ~[classes/:na] at com.jasonhhouse.Gaps.GapsApplication.run(GapsApplication.java:79) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE] at com.jasonhhouse.Gaps.GapsApplication.main(GapsApplication.java:74) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:558) ~[na:na] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na] </domain></domain></ip></code></pre><p>该提问来源于开源项目:JasonHHouse/gaps</p></div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页