Skip to content

Commit 389af21

Browse files
author
Helperhaps
authored
Merge pull request #25 from boxingyewei/master
添加国密加密
2 parents 0ae1e9d + c06a803 commit 389af21

File tree

11 files changed

+730
-20
lines changed

11 files changed

+730
-20
lines changed

libs/bcpkix-jdk15on-1.60.jar

778 KB
Binary file not shown.

libs/bcprov-jdk15on-1.60.jar

4 MB
Binary file not shown.

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jiguang-common</artifactId>
6-
<version>1.1.6-SNAPSHOT</version>
6+
<version>1.1.6</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jiguang-java-client-common</url>
99
<name>Jiguang Client Common Dependencies</name>
@@ -71,6 +71,17 @@
7171
<scope>provided</scope>
7272
</dependency>
7373

74+
<dependency>
75+
<groupId>org.bouncycastle</groupId>
76+
<artifactId>bcprov-jdk15on</artifactId>
77+
<version>1.60</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.bouncycastle</groupId>
81+
<artifactId>bcpkix-jdk15on</artifactId>
82+
<version>1.60</version>
83+
</dependency>
84+
7485
<!-- Test Dependencies -->
7586

7687
<dependency>

src/main/java/cn/jiguang/common/ClientConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public class ClientConfig extends HashMap<String, Object> {
9595
public static final Object TIME_TO_LIVE_SCHEMA = Long.class;
9696
public static final long DEFAULT_TIME_TO_LIVE = -1;
9797

98+
/**
99+
* The way to encrypt
100+
* Default value is empty
101+
* It won't encrypt any data
102+
*/
103+
public static final String ENCRYPT_TYPE = "encrypt.type";
104+
public static final Object ENCRYPT_TYPE_SCHEMA = String.class;
105+
public static final String DEFAULT_ENCRYPT_TYPE = "";
106+
98107
private static ClientConfig instance = new ClientConfig();
99108

100109
private ClientConfig() {
@@ -128,6 +137,7 @@ private ClientConfig() {
128137
this.put(APNS_PRODUCTION, DEFAULT_APNS_PRODUCTION);
129138
this.put(TIME_TO_LIVE, DEFAULT_TIME_TO_LIVE);
130139

140+
this.put(ENCRYPT_TYPE, DEFAULT_ENCRYPT_TYPE);
131141
}
132142

133143
public static ClientConfig getInstance() {
@@ -178,6 +188,10 @@ public void setConnectionRequestTimeout(int timeout) {
178188
this.put(CONNECTION_REQUEST_TIMEOUT, timeout);
179189
}
180190

191+
public void setEncryptType(String encryptType) {
192+
this.put(ENCRYPT_TYPE, encryptType);
193+
}
194+
181195
public void setConnectionTimeout(int connectionTimeout) {
182196
this.put(CONNECTION_TIMEOUT, connectionTimeout);
183197
}
@@ -210,6 +224,10 @@ public Integer getSocketTimeout() {
210224
return (Integer) this.get(SOCKET_TIMEOUT);
211225
}
212226

227+
public String getEncryptType() {
228+
return (String) this.get(ENCRYPT_TYPE);
229+
}
230+
213231
public void setApnsProduction(boolean production) {
214232
if(production) {
215233
this.put(APNS_PRODUCTION, 1);

src/main/java/cn/jiguang/common/connection/ApacheHttpClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ public class ApacheHttpClient implements IHttpClient {
6161
// 目标主机的最大连接数
6262
private int _maxRoute = 100;
6363

64+
private final String _encryptType;
65+
6466
public ApacheHttpClient(String authCode, HttpProxy proxy, ClientConfig config) {
6567
_maxRetryTimes = config.getMaxRetryTimes();
6668
_connectionTimeout = config.getConnectionTimeout();
6769
_connectionRequestTimeout = config.getConnectionRequestTimeout();
6870
_socketTimeout = config.getSocketTimeout();
6971
_authCode = authCode;
72+
_encryptType = config.getEncryptType();
7073
if (proxy != null) {
7174
_proxy = new HttpHost(proxy.getHost(), proxy.getPort());
7275
}
@@ -207,6 +210,9 @@ public ResponseWrapper sendGet(String url) throws APIConnectionException, APIReq
207210
HttpGet httpGet = new HttpGet(url);
208211
try {
209212
httpGet.setHeader(HttpHeaders.AUTHORIZATION, _authCode);
213+
if (!StringUtils.isEmpty(_encryptType)) {
214+
httpGet.setHeader("X-Encrypt-Type", _encryptType);
215+
}
210216
configHttpRequest(httpGet);
211217
response = getHttpClient(url).execute(httpGet, HttpClientContext.create());
212218
processResponse(response, wrapper);
@@ -234,6 +240,9 @@ public ResponseWrapper sendGet(String url, String content)
234240
HttpGet httpGet = new HttpGet(url);
235241
try {
236242
httpGet.setHeader(HttpHeaders.AUTHORIZATION, _authCode);
243+
if (!StringUtils.isEmpty(_encryptType)) {
244+
httpGet.setHeader("X-Encrypt-Type", _encryptType);
245+
}
237246
httpGet.setHeader("Content-Type", NativeHttpClient.CONTENT_TYPE_JSON);
238247
configHttpRequest(httpGet);
239248
response = getHttpClient(url).execute(httpGet, HttpClientContext.create());
@@ -316,6 +325,9 @@ public ResponseWrapper sendPost(String url, String content) throws APIConnection
316325
HttpPost httpPost = new HttpPost(url);
317326
try {
318327
httpPost.setHeader(HttpHeaders.AUTHORIZATION, _authCode);
328+
if (!StringUtils.isEmpty(_encryptType)) {
329+
httpPost.setHeader("X-Encrypt-Type", _encryptType);
330+
}
319331
httpPost.setHeader("Content-Type", "application/json");
320332
configHttpRequest(httpPost);
321333
StringEntity params = new StringEntity(StringUtils.notNull(content), CHARSET);
@@ -346,6 +358,9 @@ public ResponseWrapper sendPut(String url, String content) throws APIConnectionE
346358
HttpPut httpPut = new HttpPut(url);
347359
try {
348360
httpPut.setHeader(HttpHeaders.AUTHORIZATION, _authCode);
361+
if (!StringUtils.isEmpty(_encryptType)) {
362+
httpPut.setHeader("X-Encrypt-Type", _encryptType);
363+
}
349364
httpPut.setHeader("Content-Type", "application/json");
350365
configHttpRequest(httpPut);
351366
StringEntity params = new StringEntity(StringUtils.notNull(content), CHARSET);

src/main/java/cn/jiguang/common/connection/Http2Client.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import cn.jiguang.common.resp.APIConnectionException;
55
import cn.jiguang.common.resp.APIRequestException;
66
import cn.jiguang.common.resp.ResponseWrapper;
7-
import okhttp3.*;
7+
import cn.jiguang.common.utils.StringUtils;
8+
import okhttp3.MediaType;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.RequestBody;
812
import org.slf4j.Logger;
913
import org.slf4j.LoggerFactory;
1014

@@ -30,6 +34,8 @@ public class Http2Client implements IHttpClient {
3034
private String _authCode;
3135
private HttpProxy _proxy;
3236

37+
private final String _encryptType;
38+
3339
public Http2Client(String authCode, HttpProxy proxy, ClientConfig config) {
3440
_maxRetryTimes = config.getMaxRetryTimes();
3541
_connectionTimeout = config.getConnectionTimeout();
@@ -38,7 +44,7 @@ public Http2Client(String authCode, HttpProxy proxy, ClientConfig config) {
3844

3945
_authCode = authCode;
4046
_proxy = proxy;
41-
47+
_encryptType = config.getEncryptType();
4248
String message = MessageFormat.format("Created instance with "
4349
+ "connectionTimeout {0}, readTimeout {1}, maxRetryTimes {2}, SSL Version {3}",
4450
_connectionTimeout, _readTimeout, _maxRetryTimes, _sslVer);
@@ -63,14 +69,17 @@ public ResponseWrapper sendGet(String url, String content) throws APIConnectionE
6369
}
6470

6571
try {
66-
Request request = new Request.Builder().url(url)
72+
Request.Builder requestBuilder = new Request.Builder().url(url)
6773
.header("User-Agent", JPUSH_USER_AGENT)
6874
.addHeader("Accept-Charset", CHARSET)
6975
.addHeader("Charset", CHARSET)
7076
.addHeader("Connection", "Keep-Alive")
7177
.addHeader("Authorization", _authCode)
72-
.addHeader("Content-Type", CONTENT_TYPE_JSON)
73-
.build();
78+
.addHeader("Content-Type", CONTENT_TYPE_JSON);
79+
if (!StringUtils.isEmpty(_encryptType)) {
80+
requestBuilder.addHeader("X-Encrypt-Type", _encryptType);
81+
}
82+
Request request = requestBuilder.build();
7483
if (null != content) {
7584
byte[] data = content.getBytes(CHARSET);
7685
request.newBuilder().header("Content-Length", String.valueOf(data.length));
@@ -159,14 +168,18 @@ public ResponseWrapper sendDelete(String url) throws APIConnectionException, API
159168
LOG.debug("Send request - Delete url:" + " " + url);
160169
Request request;
161170
try {
162-
request = new Request.Builder().url(url)
171+
Request.Builder requestBuilder = new Request.Builder().url(url)
163172
.header("User-Agent", JPUSH_USER_AGENT)
164173
.addHeader("Accept-Charset", CHARSET)
165174
.addHeader("Charset", CHARSET)
166175
.addHeader("Connection", "Keep-Alive")
167176
.addHeader("Authorization", _authCode)
168177
.addHeader("Content-Type", CONTENT_TYPE_JSON)
169-
.delete().build();
178+
.delete();
179+
if (!StringUtils.isEmpty(_encryptType)) {
180+
requestBuilder.addHeader("X-Encrypt-Type", _encryptType);
181+
}
182+
request = requestBuilder.build();
170183
handleResponse(wrapper, request);
171184
} catch (UnsupportedEncodingException e) {
172185
e.printStackTrace();
@@ -183,14 +196,18 @@ public ResponseWrapper sendDelete(String url, String content) throws APIConnecti
183196
Request request;
184197
try {
185198
RequestBody body = RequestBody.create(JSON, content);
186-
request = new Request.Builder().url(url)
199+
Request.Builder requestBuilder = new Request.Builder().url(url)
187200
.header("User-Agent", JPUSH_USER_AGENT)
188201
.addHeader("Accept-Charset", CHARSET)
189202
.addHeader("Charset", CHARSET)
190203
.addHeader("Connection", "Keep-Alive")
191204
.addHeader("Authorization", _authCode)
192205
.addHeader("Content-Type", CONTENT_TYPE_JSON)
193-
.delete(body).build();
206+
.delete(body);
207+
if (!StringUtils.isEmpty(_encryptType)) {
208+
requestBuilder.addHeader("X-Encrypt-Type", _encryptType);
209+
}
210+
request = requestBuilder.build();
194211
handleResponse(wrapper, request);
195212
} catch (UnsupportedEncodingException e) {
196213
e.printStackTrace();
@@ -206,14 +223,18 @@ public ResponseWrapper sendPost(String url, String content) throws APIConnection
206223
ResponseWrapper wrapper = new ResponseWrapper();
207224
try {
208225
RequestBody body = RequestBody.create(JSON, content);
209-
Request request = new Request.Builder().url(url)
226+
Request.Builder requestBuilder = new Request.Builder().url(url)
210227
.header("User-Agent", JPUSH_USER_AGENT)
211228
.addHeader("Accept-Charset", CHARSET)
212229
.addHeader("Charset", CHARSET)
213230
.addHeader("Connection", "Keep-Alive")
214231
.addHeader("Authorization", _authCode)
215232
.addHeader("Content-Type", CONTENT_TYPE_JSON)
216-
.post(body).build();
233+
.post(body);
234+
if (!StringUtils.isEmpty(_encryptType)) {
235+
requestBuilder.addHeader("X-Encrypt-Type", _encryptType);
236+
}
237+
Request request = requestBuilder.build();
217238
handleResponse(wrapper, request);
218239
} catch (UnsupportedEncodingException e) {
219240
e.printStackTrace();
@@ -229,14 +250,18 @@ public ResponseWrapper sendPut(String url, String content) throws APIConnectionE
229250
ResponseWrapper wrapper = new ResponseWrapper();
230251
try {
231252
RequestBody body = RequestBody.create(JSON, content);
232-
Request request = new Request.Builder().url(url)
253+
Request.Builder requestBuilder = new Request.Builder().url(url)
233254
.header("User-Agent", JPUSH_USER_AGENT)
234255
.addHeader("Accept-Charset", CHARSET)
235256
.addHeader("Charset", CHARSET)
236257
.addHeader("Connection", "Keep-Alive")
237258
.addHeader("Authorization", _authCode)
238259
.addHeader("Content-Type", CONTENT_TYPE_JSON)
239-
.put(body).build();
260+
.put(body);
261+
if (!StringUtils.isEmpty(_encryptType)) {
262+
requestBuilder.addHeader("X-Encrypt-Type", _encryptType);
263+
}
264+
Request request = requestBuilder.build();
240265
handleResponse(wrapper, request);
241266
} catch (UnsupportedEncodingException e) {
242267
e.printStackTrace();

src/main/java/cn/jiguang/common/connection/NativeHttpClient.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package cn.jiguang.common.connection;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
5-
63
import cn.jiguang.common.ClientConfig;
74
import cn.jiguang.common.resp.APIConnectionException;
85
import cn.jiguang.common.resp.APIRequestException;
96
import cn.jiguang.common.resp.ResponseWrapper;
7+
import cn.jiguang.common.utils.StringUtils;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
1010

1111
import javax.net.ssl.*;
1212
import java.io.IOException;
@@ -36,6 +36,7 @@ public class NativeHttpClient implements IHttpClient {
3636
private final int _readTimeout;
3737
private final int _maxRetryTimes;
3838
private final String _sslVer;
39+
private final String _encryptType;
3940

4041
private String _authCode;
4142
private HttpProxy _proxy;
@@ -45,7 +46,7 @@ public NativeHttpClient(String authCode, HttpProxy proxy, ClientConfig config )
4546
_connectionTimeout = config.getConnectionTimeout();
4647
_readTimeout = config.getReadTimeout();
4748
_sslVer = config.getSSLVersion();
48-
49+
_encryptType = config.getEncryptType();
4950
_authCode = authCode;
5051
_proxy = proxy;
5152

@@ -145,6 +146,9 @@ private ResponseWrapper _doRequest(String url, String content,
145146
conn.setReadTimeout(_readTimeout);
146147
conn.setUseCaches(false);
147148
conn.setRequestMethod(method.name());
149+
if (!StringUtils.isEmpty(_encryptType)) {
150+
conn.setRequestProperty("X-Encrypt-Type", _encryptType);
151+
}
148152
conn.setRequestProperty("User-Agent", JPUSH_USER_AGENT);
149153
conn.setRequestProperty("Connection", "Keep-Alive");
150154
conn.setRequestProperty("Accept-Charset", CHARSET);

src/main/java/cn/jiguang/common/connection/NettyHttpClient.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import cn.jiguang.common.resp.APIConnectionException;
55
import cn.jiguang.common.resp.APIRequestException;
66
import cn.jiguang.common.resp.ResponseWrapper;
7+
import cn.jiguang.common.utils.StringUtils;
78
import io.netty.bootstrap.Bootstrap;
89
import io.netty.buffer.ByteBuf;
910
import io.netty.buffer.Unpooled;
10-
import io.netty.channel.*;
11+
import io.netty.channel.Channel;
12+
import io.netty.channel.ChannelFuture;
13+
import io.netty.channel.ChannelOption;
14+
import io.netty.channel.EventLoopGroup;
1115
import io.netty.channel.nio.NioEventLoopGroup;
1216
import io.netty.channel.socket.nio.NioSocketChannel;
1317
import io.netty.handler.codec.http.*;
@@ -39,6 +43,8 @@ public class NettyHttpClient implements IHttpClient {
3943
private EventLoopGroup _workerGroup;
4044
private SslContext _sslCtx;
4145

46+
private final String _encryptType;
47+
4248

4349
public NettyHttpClient(String authCode, HttpProxy proxy, ClientConfig config) {
4450
_maxRetryTimes = config.getMaxRetryTimes();
@@ -48,7 +54,7 @@ public NettyHttpClient(String authCode, HttpProxy proxy, ClientConfig config) {
4854
config.getConnectionTimeout(), _readTimeout, _maxRetryTimes, config.getSSLVersion());
4955
LOG.debug(message);
5056
_authCode = authCode;
51-
57+
_encryptType = config.getEncryptType();
5258
try {
5359
_sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
5460
_workerGroup = new NioEventLoopGroup();
@@ -87,6 +93,9 @@ public void sendRequest(HttpMethod method, String content, URI uri, BaseCallback
8793
} else {
8894
request = new DefaultFullHttpRequest(HTTP_1_1, method, uri.getRawPath());
8995
}
96+
if (!StringUtils.isEmpty(_encryptType)) {
97+
request.headers().set("X-Encrypt-Type", _encryptType);
98+
}
9099
request.headers().set(HttpHeaderNames.HOST, uri.getHost());
91100
request.headers().set(HttpHeaderNames.AUTHORIZATION, _authCode);
92101
request.headers().set("Content-Type", "application/json;charset=utf-8");
@@ -169,6 +178,9 @@ private ResponseWrapper sendHttpRequest(HttpMethod method, String url, String bo
169178
} else {
170179
request = new DefaultFullHttpRequest(HTTP_1_1, method, uri.getRawPath());
171180
}
181+
if (!StringUtils.isEmpty(_encryptType)) {
182+
request.headers().set("X-Encrypt-Type", _encryptType);
183+
}
172184
request.headers().set(HttpHeaderNames.HOST, uri.getHost());
173185
request.headers().set(HttpHeaderNames.AUTHORIZATION, _authCode);
174186
request.headers().set("Content-Type", "application/json;charset=utf-8");

0 commit comments

Comments
 (0)