Просмотр исходного кода

feat : 小游戏消息推送认证接口提交

bilingfeng 2 лет назад
Родитель
Сommit
1812d43c2d

+ 10 - 0
game-module/game-mybatis/src/main/java/com/zanxiang/mybatis/entity/GameApplet.java

@@ -46,6 +46,16 @@ public class GameApplet {
      */
     private String ghId;
 
+    /**
+     * 消息推送token
+     */
+    private String msgPushToken;
+
+    /**
+     * 消息推送加密密钥
+     */
+    private String msgPushEncodingAesKey;
+
     /**
      * 登录账号
      */

+ 36 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/controller/AppletController.java

@@ -0,0 +1,36 @@
+package com.zanxiang.sdk.controller;
+
+import com.zanxiang.sdk.annotation.UnSignCheck;
+import com.zanxiang.sdk.domain.vo.GameAppletVO;
+import com.zanxiang.sdk.service.GameAppletService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author : lingfeng
+ * @time : 2023-01-04
+ * @description : 客服消息
+ */
+@Api(tags = "sdk初始化接口")
+@RestController
+@RequestMapping(value = "/api/applet")
+public class AppletController {
+
+    @Autowired
+    private GameAppletService gameAppletService;
+
+    @UnSignCheck
+    @ApiOperation(value = "小程序客服消息认证")
+    @GetMapping("/custom/msg/{appId}")
+    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = GameAppletVO.class)})
+    public String appletMsgCheck(@PathVariable("appId") String appId, @RequestParam String signature,
+                                 @RequestParam String timestamp, @RequestParam String nonce,
+                                 @RequestParam String echostr) throws Exception {
+        return gameAppletService.appletMsgCheck(appId, signature, timestamp, nonce, echostr);
+    }
+
+}

+ 10 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/domain/dto/GameAppletDTO.java

@@ -34,4 +34,14 @@ public class GameAppletDTO {
      * 小程序原始id
      */
     private String ghId;
+
+    /**
+     * 消息推送token
+     */
+    private String msgPushToken;
+
+    /**
+     * 消息推送加密密钥
+     */
+    private String msgPushEncodingAesKey;
 }

+ 13 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/service/GameAppletService.java

@@ -13,6 +13,19 @@ import com.zanxiang.sdk.domain.vo.GameAppletVO;
  */
 public interface GameAppletService extends IService<GameApplet> {
 
+    /**
+     * applet味精检查
+     *
+     * @param appId     应用程序id
+     * @param signature 签名
+     * @param timestamp 时间戳
+     * @param nonce     现时标志
+     * @param echoStr   回声str
+     * @return {@link String}
+     * @throws Exception 异常
+     */
+    String appletMsgCheck(String appId, String signature, String timestamp, String nonce, String echoStr) throws Exception;
+
     /**
      * 通过游戏id和app id
      *

+ 31 - 1
game-module/game-sdk/src/main/java/com/zanxiang/sdk/service/Impl/GameAppletServiceImpl.java

@@ -10,9 +10,12 @@ import com.zanxiang.sdk.domain.dto.GameAppletDTO;
 import com.zanxiang.sdk.domain.params.UserData;
 import com.zanxiang.sdk.domain.vo.GameAppletVO;
 import com.zanxiang.sdk.service.GameAppletService;
+import com.zanxiang.sdk.util.SignUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.util.Objects;
+
 /**
  * @author : lingfeng
  * @time : 2022-07-08
@@ -23,7 +26,34 @@ import org.springframework.stereotype.Service;
 public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApplet> implements GameAppletService {
 
     /**
-     * 通过游戏id和app id
+     * 小游戏消息推送
+     *
+     * @param appId     应用程序id
+     * @param signature 签名
+     * @param timestamp 时间戳
+     * @param nonce     现时标志
+     * @param echoStr   回声str
+     * @return {@link String}
+     * @throws Exception 异常
+     */
+    @Override
+    public String appletMsgCheck(String appId, String signature, String timestamp, String nonce, String echoStr) throws Exception {
+        log.error("验证参数, appId : {}, signature : {}, timestamp : {}, nonce : {}, echoStr : {}", appId, signature, timestamp, nonce, echoStr);
+        GameApplet gameApplet = super.getOne(new LambdaQueryWrapper<GameApplet>()
+                .eq(GameApplet::getAppId, appId));
+        if (gameApplet == null) {
+            throw new BaseException("参数错误, 游戏小程序信息不存在");
+        }
+        String mySignature = SignUtil.SHA1(gameApplet.getMsgPushToken(), timestamp, nonce);
+        log.error("计算出来的签名, signature : {},  mySignature : {}", signature, mySignature);
+        if (!Objects.equals(mySignature, signature)) {
+            throw new BaseException("签名不匹配");
+        }
+        return echoStr;
+    }
+
+    /**
+     * 通过游戏id查询
      *
      * @param gameId 游戏id
      * @return {@link GameAppletDTO}

+ 40 - 0
game-module/game-sdk/src/main/java/com/zanxiang/sdk/util/SignUtil.java

@@ -4,6 +4,7 @@ import javax.crypto.Mac;
 import javax.crypto.spec.SecretKeySpec;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
+import java.util.Arrays;
 
 /**
  * @author : lingfeng
@@ -22,6 +23,11 @@ public class SignUtil {
      */
     private static final String SIGN_HMACSHA256 = "HMACSHA256";
 
+    /**
+     * MD5加密
+     */
+    private static final String SIGN_SHA1 = "SHA-1";
+
     /**
      * MD5加密
      *
@@ -57,4 +63,38 @@ public class SignUtil {
         }
         return sb.toString();
     }
+
+    /**
+     * 用SHA1算法生成安全签名
+     *
+     * @param token     票据
+     * @param timestamp 时间戳
+     * @param nonce     随机字符串
+     * @return 安全签名
+     * @throws Exception : 异常
+     */
+    public static String SHA1(String token, String timestamp, String nonce) throws Exception {
+        String[] array = new String[]{token, timestamp, nonce};
+        StringBuilder sb = new StringBuilder();
+        // 字符串排序
+        Arrays.sort(array);
+        for (int i = 0; i < 3; i++) {
+            sb.append(array[i]);
+        }
+        String str = sb.toString();
+        // SHA1签名生成
+        MessageDigest md = MessageDigest.getInstance(SIGN_SHA1);
+        md.update(str.getBytes());
+        byte[] digest = md.digest();
+        StringBuilder hexStr = new StringBuilder();
+        String shaHex;
+        for (byte b : digest) {
+            shaHex = Integer.toHexString(b & 0xFF);
+            if (shaHex.length() < 2) {
+                hexStr.append(0);
+            }
+            hexStr.append(shaHex);
+        }
+        return hexStr.toString();
+    }
 }