Skip to content

Commit 169ff54

Browse files
committed
update version to 5.1.13, support SM2.
1 parent c980917 commit 169ff54

File tree

9 files changed

+125
-17
lines changed

9 files changed

+125
-17
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
## 1. 集成
1616
引入sdk包
1717
```xml
18-
<!--以5.1.12版本为例-->
18+
<!--以5.1.13版本为例-->
1919
<dependencies>
2020
<!-- jiguang-sdk -->
2121
<dependency>
2222
<groupId>io.github.jpush</groupId>
2323
<artifactId>jiguang-sdk</artifactId>
24-
<version>5.1.12</version>
24+
<version>5.1.13</version>
2525
</dependency>
2626
</dependencies>
2727
```

example-for-spring/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<groupId>io.github.jpush</groupId>
1515
<artifactId>example-for-spring</artifactId>
16-
<version>5.1.12</version>
16+
<version>5.1.13</version>
1717

1818
<properties>
1919
<maven.compiler.source>8</maven.compiler.source>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>io.github.jpush</groupId>
2828
<artifactId>jiguang-sdk</artifactId>
29-
<version>5.1.12</version>
29+
<version>5.1.13</version>
3030
</dependency>
3131
<!-- lombok -->
3232
<dependency>

jiguang-sdk/pom.xml

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<parent>
88
<groupId>io.github.jpush</groupId>
99
<artifactId>jiguang-sdk-java</artifactId>
10-
<version>5.1.12</version>
10+
<version>5.1.13</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

1414
<groupId>io.github.jpush</groupId>
1515
<artifactId>jiguang-sdk</artifactId>
16-
<version>5.1.12</version>
16+
<version>5.1.13</version>
1717
<packaging>jar</packaging>
1818

1919
<properties>
@@ -73,6 +73,12 @@
7373
<artifactId>jackson-datatype-jsr310</artifactId>
7474
<version>${jackson-datatype-jsr310.version}</version>
7575
</dependency>
76+
<!-- SM2 -->
77+
<dependency>
78+
<groupId>org.bouncycastle</groupId>
79+
<artifactId>bcpkix-jdk15on</artifactId>
80+
<version>1.68</version>
81+
</dependency>
7682
</dependencies>
7783

7884
</project>

jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public PushSendResult send(@NonNull PushSendParam param) {
3434
return pushClient.send(param);
3535
}
3636

37+
public PushSendResult sendWithSM2(@NonNull PushSendParam param) {
38+
return pushClient.sendWithSM2(param);
39+
}
40+
3741
public SchedulePushSendResult scheduleSend(@NonNull SchedulePushSendParam param) {
3842
return pushClient.scheduleSend(param);
3943
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.jiguang.sdk.bean.push.other;
2+
3+
import cn.jiguang.sdk.bean.push.PushSendParam;
4+
import cn.jiguang.sdk.bean.push.audience.Audience;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import lombok.Data;
7+
8+
@Data
9+
public class SM2Push {
10+
11+
/**
12+
* 两种格式
13+
* 字符串:"all"
14+
* {@link Audience}对象: {"tag":[],"tag_and":[],"tag_not":[],"alias":[],"registration_id":[],"segment":[],"abtest":[],"live_activity_id":"","file":{"file_id":""}}
15+
*/
16+
@JsonProperty("audience")
17+
private Object audience;
18+
19+
/**
20+
* 完整的消息体
21+
*/
22+
@JsonProperty("payload")
23+
private String payload;
24+
25+
}

jiguang-sdk/src/main/java/cn/jiguang/sdk/client/PushClient.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public interface PushClient {
2323
@Headers("Content-Type: application/json; charset=utf-8")
2424
PushSendResult send(PushSendParam param);
2525

26+
@RequestLine("POST /v3/push")
27+
@Headers({"Content-Type: application/json; charset=utf-8", "X-Encrypt-Type: SM2"})
28+
PushSendResult sendWithSM2(PushSendParam param);
29+
2630
@RequestLine("POST /v3/schedules")
2731
@Headers("Content-Type: application/json; charset=utf-8")
2832
SchedulePushSendResult scheduleSend(SchedulePushSendParam param);
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package cn.jiguang.sdk.codec;
22

3+
import cn.jiguang.sdk.bean.push.PushSendParam;
4+
import cn.jiguang.sdk.bean.push.other.SM2Push;
5+
import cn.jiguang.sdk.utils.SM2Util;
36
import com.fasterxml.jackson.databind.ObjectMapper;
47
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
58
import feign.RequestTemplate;
69
import feign.codec.EncodeException;
710
import feign.codec.Encoder;
811
import feign.form.FormEncoder;
912
import feign.jackson.JacksonEncoder;
13+
import lombok.SneakyThrows;
1014

1115
import java.lang.reflect.Type;
16+
import java.util.Base64;
17+
import java.util.Collection;
18+
import java.util.Map;
1219

1320
public class ApiEncoder implements Encoder {
1421

@@ -24,18 +31,33 @@ public ApiEncoder() {
2431

2532
@Override
2633
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
27-
String contentType = template.headers().get("Content-Type").stream().findFirst().orElse(null);
28-
if (contentType == null) {
29-
return;
34+
Map<String, Collection<String>> headers = template.headers();
35+
if (headers.containsKey("X-Encrypt-Type")) {
36+
String encryptType = headers.get("X-Encrypt-Type").stream().findFirst().orElse("");
37+
if (encryptType.startsWith("SM2") && object instanceof PushSendParam) {
38+
SM2Push sm2Push = convertToSM2Push((PushSendParam) object);
39+
jacksonEncoder.encode(sm2Push, SM2Push.class, template);
40+
return;
41+
}
3042
}
31-
if (contentType.startsWith("application/json")) {
32-
jacksonEncoder.encode(object, bodyType, template);
33-
return;
34-
}
35-
if (contentType.startsWith("multipart/form-data")) {
36-
formEncoder.encode(object, bodyType, template);
37-
return;
43+
if (headers.containsKey("Content-Type")) {
44+
String contentType = headers.get("Content-Type").stream().findFirst().orElse("");
45+
if (contentType.startsWith("application/json")) {
46+
jacksonEncoder.encode(object, bodyType, template);
47+
} else if (contentType.startsWith("multipart/form-data")) {
48+
formEncoder.encode(object, bodyType, template);
49+
}
3850
}
3951
}
4052

53+
@SneakyThrows
54+
private SM2Push convertToSM2Push(PushSendParam pushSendParam) {
55+
ObjectMapper objectMapper = new ObjectMapper();
56+
byte[] encrypt = SM2Util.encrypt(objectMapper.writeValueAsString(pushSendParam));
57+
SM2Push sm2Push = new SM2Push();
58+
sm2Push.setPayload(Base64.getEncoder().encodeToString(encrypt));
59+
sm2Push.setAudience(pushSendParam.getAudience());
60+
return sm2Push;
61+
}
62+
4163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.jiguang.sdk.utils;
2+
3+
import org.bouncycastle.asn1.gm.GMNamedCurves;
4+
import org.bouncycastle.asn1.x9.X9ECParameters;
5+
import org.bouncycastle.crypto.engines.SM2Engine;
6+
import org.bouncycastle.crypto.params.ECDomainParameters;
7+
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
8+
import org.bouncycastle.crypto.params.ParametersWithRandom;
9+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
10+
import org.bouncycastle.math.ec.ECPoint;
11+
import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve;
12+
13+
import java.security.SecureRandom;
14+
import java.security.Security;
15+
import java.util.Base64;
16+
17+
public class SM2Util {
18+
19+
static {
20+
Security.addProvider(new BouncyCastleProvider());
21+
}
22+
23+
private static final String BASE64_PUBLIC_KEY = "BPj6Mj/T444gxPaHc6CDCizMRp4pEl14WI2lvIbdEK2c+5XiSqmQt2TQc8hMMZqfxcDqUNQW95puAfQx1asv3rU=";
24+
25+
private static final ECPublicKeyParameters PUBLIC_KEY = initializePublicKey();
26+
27+
private static ECPublicKeyParameters initializePublicKey() {
28+
X9ECParameters sm2Params = GMNamedCurves.getByName("sm2p256v1");
29+
ECDomainParameters ecDomainParameters = new ECDomainParameters(
30+
sm2Params.getCurve(),
31+
sm2Params.getG(),
32+
sm2Params.getN(),
33+
sm2Params.getH()
34+
);
35+
SM2P256V1Curve CURVE = new SM2P256V1Curve();
36+
ECPoint ecPoint = CURVE.decodePoint(Base64.getDecoder().decode(BASE64_PUBLIC_KEY));
37+
return new ECPublicKeyParameters(ecPoint, ecDomainParameters);
38+
}
39+
40+
public static byte[] encrypt(String plainText) throws Exception {
41+
SM2Engine engine = new SM2Engine();
42+
engine.init(true, new ParametersWithRandom(PUBLIC_KEY, new SecureRandom()));
43+
byte[] plainBytes = plainText.getBytes();
44+
return engine.processBlock(plainBytes, 0, plainBytes.length);
45+
}
46+
47+
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.jpush</groupId>
88
<artifactId>jiguang-sdk-java</artifactId>
9-
<version>5.1.12</version>
9+
<version>5.1.13</version>
1010
<packaging>pom</packaging>
1111

1212
<name>Jiguang SDK For Rest Api</name>

0 commit comments

Comments
 (0)