浏览代码

feat : token校验的时候新增sessionKey过期判断

bilingfeng 10 月之前
父节点
当前提交
ff88f417a1

+ 1 - 1
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/SDKApplication.java

@@ -23,7 +23,7 @@ public class SDKApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(SDKApplication.class, args);
-        System.out.println("赞象SDK服务启动成功 <消息推送策略01> ( ´・・)ノ(._.`) \n" +
+        System.out.println("赞象SDK服务启动成功 <token校验的时候新增sessionKey过期判断> ( ´・・)ノ(._.`) \n" +
                 " ___________ _   __\n" +
                 "/  ___|  _  \\ | / /\n" +
                 "\\ `--.| | | | |/ / \n" +

+ 39 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/api/WxApiService.java

@@ -3,7 +3,10 @@ package com.zanxiang.game.module.sdk.service.api;
 import com.zanxiang.game.module.base.pojo.enums.PayApplicationTypeEnum;
 import com.zanxiang.game.module.sdk.constant.RedisKeyConstant;
 import com.zanxiang.game.module.sdk.enums.ExpireTimeEnum;
+import com.zanxiang.game.module.sdk.pojo.dto.GameAppletDTO;
+import com.zanxiang.game.module.sdk.service.IGameAppletService;
 import com.zanxiang.game.module.sdk.util.RedisUtil;
+import com.zanxiang.game.module.sdk.util.SignUtil;
 import com.zanxiang.module.util.JsonUtil;
 import com.zanxiang.module.util.URIUtil;
 import com.zanxiang.module.util.exception.BaseException;
@@ -33,6 +36,9 @@ public class WxApiService {
     @Autowired
     private RedisUtil<String> redisUtil;
 
+    @Autowired
+    private IGameAppletService gameAppletService;
+
     /**
      * 根据应用类型获取openId
      *
@@ -132,4 +138,37 @@ public class WxApiService {
         redisUtil.setCache(key, resultMap.get("access_token"), ExpireTimeEnum.ONE_HOUR.getTime());
         return resultMap.get("access_token");
     }
+
+    /**
+     * 返回是否过期
+     *
+     * @param gameId :  应用id
+     * @param openId :  用户openId
+     * @return {@link String}
+     */
+    public boolean checkSessionKey(Long gameId, String openId, String sessionKey) {
+        String signature;
+        String accessToken;
+        try {
+            signature = SignUtil.HMACSHA256("", sessionKey);
+            GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(gameId);
+            accessToken = this.getAccessToken(gameAppletDTO.getAppId(), gameAppletDTO.getAppSecret());
+        } catch (Exception e) {
+            return Boolean.TRUE;
+        }
+        Map<String, String> paramMap = new HashMap<>(4);
+        paramMap.put("access_token", accessToken);
+        paramMap.put("signature", signature);
+        paramMap.put("openid", openId);
+        paramMap.put("sig_method", "hmac_sha256");
+        // 发送请求
+        String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/wxa/checksession", paramMap, Boolean.FALSE);
+        String sr = restTemplate.getForObject(url, String.class);
+        // 解析相应内容(转换成json对象)
+        Map<String, String> resultMap = JsonUtil.toMap(sr, Map.class, String.class);
+        if (resultMap == null || !Objects.equals("0", resultMap.get("errcode"))) {
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
 }

+ 20 - 0
game-module/game-module-sdk/src/main/java/com/zanxiang/game/module/sdk/service/impl/UserTokenServiceImpl.java

@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.zanxiang.game.module.base.pojo.enums.BanStatusEnum;
+import com.zanxiang.game.module.base.pojo.enums.GameCategoryEnum;
 import com.zanxiang.game.module.base.util.DateUtils;
+import com.zanxiang.game.module.mybatis.entity.Game;
 import com.zanxiang.game.module.mybatis.entity.GameExt;
 import com.zanxiang.game.module.mybatis.entity.User;
 import com.zanxiang.game.module.mybatis.entity.UserToken;
@@ -18,6 +20,7 @@ import com.zanxiang.game.module.sdk.pojo.dto.UserTokenDTO;
 import com.zanxiang.game.module.sdk.pojo.param.UserData;
 import com.zanxiang.game.module.sdk.pojo.vo.CpTokenCheckVO;
 import com.zanxiang.game.module.sdk.service.*;
+import com.zanxiang.game.module.sdk.service.api.WxApiService;
 import com.zanxiang.game.module.sdk.util.RedisUtil;
 import com.zanxiang.game.module.sdk.util.SignUtil;
 import com.zanxiang.module.redis.service.IDistributedLockComponent;
@@ -54,6 +57,15 @@ public class UserTokenServiceImpl extends ServiceImpl<UserTokenMapper, UserToken
     @Autowired
     private RedisUtil<UserToken> redisUtil;
 
+    @Autowired
+    private WxApiService wxApiService;
+
+    @Autowired
+    private IGameAppletService gameAppletService;
+
+    @Autowired
+    private IGameService gameService;
+
     @Autowired
     private IGameExtService gameExtService;
 
@@ -212,6 +224,14 @@ public class UserTokenServiceImpl extends ServiceImpl<UserTokenMapper, UserToken
         if (user == null || Objects.equals(BanStatusEnum.BAN_STATUS.getStatus(), user.getStatus())) {
             return Boolean.FALSE;
         }
+        //小程序监测sessionKey
+        if (Strings.isNotBlank(user.getSessionKey())) {
+            Game game = gameService.getById(userData.getGameId());
+            if (Objects.equals(game.getCategory(), GameCategoryEnum.CATEGORY_WX_APPLET.getId())
+                    && wxApiService.checkSessionKey(userData.getGameId(), user.getOpenId(), user.getSessionKey())) {
+                return Boolean.FALSE;
+            }
+        }
         //token检测
         Long expireTime = userData.getExpireTime();
         if (expireTime == null) {