Android avax.net.ssl.SSLPeerUnverifiedException: No peer certificate 解决方法(httpClient支持HTTPS的访问方式)
深山老妖浏览:28382019-03-12 07:42:17本文累计收益:0我也要赚钱

项目中Android https请求地址遇到了这个异常(无终端认证),javax.net.ssl.SSLPeerUnverifiedException: No peer certificate,是SSL协议中没有终端认证。

解决方案如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
* Post请求连接Https服务
     * @param serverURL  请求地址
     * @param jsonStr    请求报文
     * @return
     * @throws Exception
     */
    public static synchronized String doHttpsPost(String serverURL, String jsonStr)throws Exception {
        <span style="color:#009900;">// 参数 </span>
        HttpParams httpParameters = new BasicHttpParams();
       <span style="color:#33cc00;"> </span><span style="color:#009900;">// 设置连接超时 </span>
        HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
        <span style="color:#009900;">// 设置socket超时 </span>
        HttpConnectionParams.setSoTimeout(httpParameters, 3000);
        <span style="color:#009900;">// 获取HttpClient对象 (认证) </span>
        HttpClient hc = initHttpClient(httpParameters);
        HttpPost post = new HttpPost(serverURL);
       <span style="color:#006600;"> </span><span style="color:#009900;">// 发送数据类型 </span>
        post.addHeader("Content-Type", "application/json;charset=utf-8");
       <span style="color:#009900;"> // 接受数据类型 </span>
        post.addHeader("Accept", "application/json");
      <span style="background-color: rgb(255, 255, 255);"><span style="color:#006600;"> </span><span style="color:#009900;"> // 请求报文 </span></span>
        StringEntity entity = new StringEntity(jsonStr, "UTF-8");
        post.setEntity(entity);
        post.setParams(httpParameters);
        HttpResponse response = null;
        try {
            response = hc.execute(post);
        } catch (UnknownHostException e) {
            throw new Exception("Unable to access " + e.getLocalizedMessage());
        } catch (SocketException e) {
            e.printStackTrace();
        }
        int sCode = response.getStatusLine().getStatusCode();
        if (sCode == HttpStatus.SC_OK) {
            return EntityUtils.toString(response.getEntity());
        } else
            throw new Exception("StatusCode is " + sCode);
    }
     
    private static HttpClient client = null;
    /**
     * 初始化HttpClient对象
     * @param params
     * @return
     */
    public static synchronized HttpClient initHttpClient(HttpParams params) {
        if(client == null){
            try {
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                trustStore.load(null, null);
                   
                SSLSocketFactory sf = new SSLSocketFactoryImp(trustStore);
                //允许所有主机的验证
                sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                   
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
                HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
                // 设置http和https支持
                SchemeRegistry registry = new SchemeRegistry();
                registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                registry.register(new Scheme("https", sf, 443));
                   
                ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
                   
                return new DefaultHttpClient(ccm, params);
            } catch (Exception e) {
                e.printStackTrace();
                return new DefaultHttpClient(params);
            }
        }
        return client;
    }
     
   public static class SSLSocketFactoryImp extends SSLSocketFactory {
        final SSLContext sslContext = SSLContext.getInstance("TLS");
     
        public SSLSocketFactoryImp(KeyStore truststore)
                throws NoSuchAlgorithmException, KeyManagementException,
                KeyStoreException, UnrecoverableKeyException {
            super(truststore);
     
            TrustManager tm = new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
     
                @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 {
                }
            };
            sslContext.init(null, new TrustManager[] { tm }, null);
        }
     
        @Override
        public Socket createSocket(Socket socket, String host, int port,
                boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host,
                    port, autoClose);
        }
     
        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }


开启工作线程start(),再次运行, javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 的异常消失。服务端的数据正常返回了。

分析问题:

HTTPS:超文本安全传输协议,和HTTP相比,多了一个SSL/TSL的认证过程,端口为443。

1.peer终端发送一个request,https服务端把支持的加密算法等以证书的形式返回一个身份信息(包含ca颁发机构和加密公钥等)。

2.获取证书之后,验证证书合法性。

3.随机产生一个密钥,并以证书当中的公钥加密。

4.request https服务端,把用公钥加密过的密钥传送给https服务端。

5.https服务端用自己的密钥解密,获取随机值。

6.之后双方传送数据都用此密钥加密后通信。

HTTPS流程清楚后,问题也就明显了,验证证书时,无法验证。

上面提供的解决方案就是添加默认信任全部证书。以此来通过接下来的通信。

但是,这样问题是解决了。但是觉得还是不带靠谱(信任全部证书有点危险)。继续噼噼啪啪的网上搜索一番。又找到了一种解决方案,其过程大致这样的:

1.浏览器访问https地址,保存提示的证书到本地,放到android项目中的assets目录。

2.导入证书,代码如下。

3.把证书添加为信任。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public static String requestHTTPSPage(Context context, String mUrl) {
            InputStream ins = null;
            String result = "";
            try {
                ins = context.getAssets().open("my.key"); // 下载的证书放到项目中的assets目录中
                CertificateFactory cerFactory = CertificateFactory.getInstance("X.509");
                Certificate cer = cerFactory.generateCertificate(ins);
                KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
                keyStore.load(null, null);
                keyStore.setCertificateEntry("trust", cer);
       
                SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
                Scheme sch = new Scheme("https", socketFactory, 443);
                HttpClient mHttpClient = new DefaultHttpClient();
                mHttpClient.getConnectionManager().getSchemeRegistry().register(sch);
       
                BufferedReader reader = null;
                try {
                    HttpGet request = new HttpGet();
                    request.setURI(new URI(mUrl));
                    HttpResponse response = mHttpClient.execute(request);
                    if (response.getStatusLine().getStatusCode() != 200) {
                        request.abort();
                        return result;
                    }
       
                    reader = new BufferedReader(new InputStreamReader(response
                            .getEntity().getContent()));
                    StringBuffer buffer = new StringBuffer();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                    }
                    result = buffer.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ins != null)
                        ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return result;

 

评论列表
发表评论
+ 关注