Skip to content

Commit

Permalink
update version to 5.1.13, support SM2.
Browse files Browse the repository at this point in the history
  • Loading branch information
wicked-tc130 committed Jan 12, 2025
1 parent c980917 commit 169ff54
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
## 1. 集成
引入sdk包
```xml
<!--以5.1.12版本为例-->
<!--以5.1.13版本为例-->
<dependencies>
<!-- jiguang-sdk -->
<dependency>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions example-for-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>io.github.jpush</groupId>
<artifactId>example-for-spring</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>
</dependency>
<!-- lombok -->
<dependency>
Expand Down
10 changes: 8 additions & 2 deletions jiguang-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk-java</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -73,6 +73,12 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson-datatype-jsr310.version}</version>
</dependency>
<!-- SM2 -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.68</version>
</dependency>
</dependencies>

</project>
4 changes: 4 additions & 0 deletions jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public PushSendResult send(@NonNull PushSendParam param) {
return pushClient.send(param);
}

public PushSendResult sendWithSM2(@NonNull PushSendParam param) {
return pushClient.sendWithSM2(param);
}

public SchedulePushSendResult scheduleSend(@NonNull SchedulePushSendParam param) {
return pushClient.scheduleSend(param);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.jiguang.sdk.bean.push.other;

import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.audience.Audience;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class SM2Push {

/**
* 两种格式
* 字符串:"all"
* {@link Audience}对象: {"tag":[],"tag_and":[],"tag_not":[],"alias":[],"registration_id":[],"segment":[],"abtest":[],"live_activity_id":"","file":{"file_id":""}}
*/
@JsonProperty("audience")
private Object audience;

/**
* 完整的消息体
*/
@JsonProperty("payload")
private String payload;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface PushClient {
@Headers("Content-Type: application/json; charset=utf-8")
PushSendResult send(PushSendParam param);

@RequestLine("POST /v3/push")
@Headers({"Content-Type: application/json; charset=utf-8", "X-Encrypt-Type: SM2"})
PushSendResult sendWithSM2(PushSendParam param);

@RequestLine("POST /v3/schedules")
@Headers("Content-Type: application/json; charset=utf-8")
SchedulePushSendResult scheduleSend(SchedulePushSendParam param);
Expand Down
42 changes: 32 additions & 10 deletions jiguang-sdk/src/main/java/cn/jiguang/sdk/codec/ApiEncoder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package cn.jiguang.sdk.codec;

import cn.jiguang.sdk.bean.push.PushSendParam;
import cn.jiguang.sdk.bean.push.other.SM2Push;
import cn.jiguang.sdk.utils.SM2Util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.FormEncoder;
import feign.jackson.JacksonEncoder;
import lombok.SneakyThrows;

import java.lang.reflect.Type;
import java.util.Base64;
import java.util.Collection;
import java.util.Map;

public class ApiEncoder implements Encoder {

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

@Override
public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
String contentType = template.headers().get("Content-Type").stream().findFirst().orElse(null);
if (contentType == null) {
return;
Map<String, Collection<String>> headers = template.headers();
if (headers.containsKey("X-Encrypt-Type")) {
String encryptType = headers.get("X-Encrypt-Type").stream().findFirst().orElse("");
if (encryptType.startsWith("SM2") && object instanceof PushSendParam) {
SM2Push sm2Push = convertToSM2Push((PushSendParam) object);
jacksonEncoder.encode(sm2Push, SM2Push.class, template);
return;
}
}
if (contentType.startsWith("application/json")) {
jacksonEncoder.encode(object, bodyType, template);
return;
}
if (contentType.startsWith("multipart/form-data")) {
formEncoder.encode(object, bodyType, template);
return;
if (headers.containsKey("Content-Type")) {
String contentType = headers.get("Content-Type").stream().findFirst().orElse("");
if (contentType.startsWith("application/json")) {
jacksonEncoder.encode(object, bodyType, template);
} else if (contentType.startsWith("multipart/form-data")) {
formEncoder.encode(object, bodyType, template);
}
}
}

@SneakyThrows
private SM2Push convertToSM2Push(PushSendParam pushSendParam) {
ObjectMapper objectMapper = new ObjectMapper();
byte[] encrypt = SM2Util.encrypt(objectMapper.writeValueAsString(pushSendParam));
SM2Push sm2Push = new SM2Push();
sm2Push.setPayload(Base64.getEncoder().encodeToString(encrypt));
sm2Push.setAudience(pushSendParam.getAudience());
return sm2Push;
}

}
47 changes: 47 additions & 0 deletions jiguang-sdk/src/main/java/cn/jiguang/sdk/utils/SM2Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.jiguang.sdk.utils;

import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve;

import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;

public class SM2Util {

static {
Security.addProvider(new BouncyCastleProvider());
}

private static final String BASE64_PUBLIC_KEY = "BPj6Mj/T444gxPaHc6CDCizMRp4pEl14WI2lvIbdEK2c+5XiSqmQt2TQc8hMMZqfxcDqUNQW95puAfQx1asv3rU=";

private static final ECPublicKeyParameters PUBLIC_KEY = initializePublicKey();

private static ECPublicKeyParameters initializePublicKey() {
X9ECParameters sm2Params = GMNamedCurves.getByName("sm2p256v1");
ECDomainParameters ecDomainParameters = new ECDomainParameters(
sm2Params.getCurve(),
sm2Params.getG(),
sm2Params.getN(),
sm2Params.getH()
);
SM2P256V1Curve CURVE = new SM2P256V1Curve();
ECPoint ecPoint = CURVE.decodePoint(Base64.getDecoder().decode(BASE64_PUBLIC_KEY));
return new ECPublicKeyParameters(ecPoint, ecDomainParameters);
}

public static byte[] encrypt(String plainText) throws Exception {
SM2Engine engine = new SM2Engine();
engine.init(true, new ParametersWithRandom(PUBLIC_KEY, new SecureRandom()));
byte[] plainBytes = plainText.getBytes();
return engine.processBlock(plainBytes, 0, plainBytes.length);
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.jpush</groupId>
<artifactId>jiguang-sdk-java</artifactId>
<version>5.1.12</version>
<version>5.1.13</version>
<packaging>pom</packaging>

<name>Jiguang SDK For Rest Api</name>
Expand Down

0 comments on commit 169ff54

Please sign in to comment.