Skip to content

Commit 9c309bb

Browse files
author
bulezeng(曾卫进)
committed
git commit -m "xingePush#19 增加缺省信任证书管理器。缺省信任证书管理器 信任任何服务器证书, 用户也可以自定义信任证书管理器,并通过setTrustManager 接口进行设置"
1 parent 88bba3f commit 9c309bb

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.tencent.xinge;
2+
3+
4+
5+
import javax.net.ssl.X509TrustManager;
6+
import java.security.cert.CertificateException;
7+
import java.security.cert.X509Certificate;
8+
9+
/**
10+
* ȱʡÐÅÈιÜÀíÆ÷
11+
*/
12+
13+
class DefaultX509TrustManager implements X509TrustManager {
14+
15+
16+
@Override
17+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
18+
19+
}
20+
21+
@Override
22+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
23+
24+
}
25+
26+
public X509Certificate[] getAcceptedIssuers() {
27+
return null;
28+
}
29+
30+
31+
32+
}

src/main/java/com/tencent/xinge/XingeApp.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
import java.net.HttpURLConnection;
66
import java.net.MalformedURLException;
77
import java.net.URL;
8+
import java.security.KeyManagementException;
9+
import java.security.NoSuchAlgorithmException;
810

911
import com.tencent.xinge.push.app.PushAppRequest;
1012
import org.apache.commons.codec.binary.Base64;
1113
import org.json.JSONObject;
1214

1315
import com.tencent.xinge.api.RESTAPI_V3;
1416

15-
import javax.net.ssl.HostnameVerifier;
16-
import javax.net.ssl.HttpsURLConnection;
17-
import javax.net.ssl.SSLSession;
17+
import javax.net.ssl.*;
1818

1919
/**
2020
* 提供V3接口<br>
@@ -29,6 +29,7 @@ public class XingeApp {
2929

3030
private String authString = null;
3131
private String authStringEnc = null;
32+
X509TrustManager trustManager = new DefaultX509TrustManager();
3233

3334
/**
3435
* HTTP Header Authorization 的值:Basic base64_auth_string<br>
@@ -104,8 +105,9 @@ private synchronized JSONObject callRestful(String apiAddress, String jsonReques
104105
https.setReadTimeout(10000);
105106
https.setRequestProperty("Content-Type", "application/json");
106107
https.setRequestProperty("Authorization", "Basic " + authStringEnc);
107-
https.setRequestProperty("Connection", "Keep-Alive ");
108+
https.setRequestProperty("Connection", "Keep-Alive");
108109

110+
https.setSSLSocketFactory(this.getSSLSocketFactory());
109111

110112
byte[] out = jsonRequestString.getBytes(Charsets.UTF_8);
111113
int length = out.length;
@@ -152,6 +154,14 @@ private synchronized JSONObject callRestful(String apiAddress, String jsonReques
152154
jsonRet.put("ret_code", 10101);
153155
jsonRet.put("err_msg", stringifyError(e));
154156

157+
} catch (NoSuchAlgorithmException e) {
158+
jsonRet = new JSONObject();
159+
jsonRet.put("ret_code", 10102);
160+
jsonRet.put("err_msg", stringifyError(e));
161+
} catch (KeyManagementException e) {
162+
jsonRet = new JSONObject();
163+
jsonRet.put("ret_code", 10103);
164+
jsonRet.put("err_msg", stringifyError(e));
155165
} finally {
156166
if (br != null) {
157167
try {
@@ -183,6 +193,22 @@ public static String stringifyError(Throwable error) {
183193
return result.toString();
184194
}
185195

196+
private SSLSocketFactory getSSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
197+
TrustManager[] tm = {this.trustManager};
198+
SSLContext sslContext = SSLContext.getInstance("TLSv1");
199+
sslContext.init(null, tm, new java.security.SecureRandom());
200+
SSLSocketFactory ssf = sslContext.getSocketFactory();
201+
return ssf;
202+
}
203+
204+
/**
205+
* 设置证书信任管理器
206+
* @param trustManager
207+
*/
208+
public void setTrustManager(X509TrustManager trustManager) {
209+
this.trustManager = trustManager;
210+
}
211+
186212
public class TrustAnyHostnameVerifier implements HostnameVerifier {
187213
public boolean verify(String hostname, SSLSession session) {
188214
// 直接返回true

0 commit comments

Comments
 (0)