TRX 1 ano atrás
pai
commit
9cc7ac4d7c

+ 2 - 2
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/free/GateWayFreeController.java

@@ -48,7 +48,7 @@ public class GateWayFreeController {
     @RequestMapping(value = "registerGateWay", method = {RequestMethod.POST})
     public ResultContent<MqttInfoReturnModel> registerGateWay(@RequestBody GateWayInfoAddParam param) {
         Assert.hasText(param.getGateWayId(), "网关ID不能为空");
-        ResultContent resultContent = akSignService.verifyAk(param.getAk(), param.getTime(), param.getSign());
+        ResultContent resultContent = akSignService.verifyAk(param.getAk(), param.getTime(), param.getSign(), param.getGateWayId());
         if (resultContent.isFailed()) {
             return resultContent;
         }
@@ -60,7 +60,7 @@ public class GateWayFreeController {
     @RequestMapping(value = "gateWayBindDevice", method = {RequestMethod.POST})
     public ResultContent gateWayBindDevice(@RequestBody GateWayBindDeviceParam param) {
         Assert.hasText(param.getGateWayId(), "网关ID不能为空");
-        ResultContent resultContent = akSignService.verifyAk(param.getAk(), param.getTime(), param.getSign());
+        ResultContent resultContent = akSignService.verifyAk(param.getAk(), param.getTime(), param.getSign(), param.getGateWayId());
         if (resultContent.isFailed()) {
             return resultContent;
         }

+ 6 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/docker/AkSkConfig.java

@@ -35,4 +35,10 @@ public class AkSkConfig extends SuperEntity {
     @DBRef(lazy = true)
     private GateWayInfo gateWayInfo;
 
+    @Schema(description = "关联使用的数据ID")
+    private String aboutDataId;
+
+    private Long useTime;
+
+    private String useTimeStr;
 }

+ 16 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/base/AkSignService.java

@@ -5,6 +5,7 @@ import com.zhongshu.iot.client.type.DataState;
 import com.zhongshu.iot.server.core.dao.docker.AkSkConfigDao;
 import com.zhongshu.iot.server.core.domain.docker.AkSkConfig;
 import com.zhongshu.iot.server.core.util.CommonUtil;
+import com.zhongshu.iot.server.core.util.DateUtils;
 import com.zhongshu.iot.server.core.util.SecurityUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
@@ -33,7 +34,7 @@ public class AkSignService {
      * @param sign
      * @return
      */
-    public ResultContent verifyAk(String ak, String time, String sign) {
+    public ResultContent verifyAk(String ak, String time, String sign, String gateWayId) {
         if (StringUtils.isNotEmpty(ak)) {
             AkSkConfig akSkConfig = akSkConfigDao.findTopByAk(ak);
             if (ObjectUtils.isEmpty(akSkConfig)) {
@@ -42,6 +43,12 @@ public class AkSignService {
             if (akSkConfig.getState() != DataState.Enable) {
                 return ResultContent.buildFail("秘钥不可用");
             }
+            // 这个网关使用了,其他就不能使用
+            if (StringUtils.isNotEmpty(akSkConfig.getAboutDataId()) &&
+                    !akSkConfig.getAboutDataId().equals(gateWayId)) {
+                return ResultContent.buildFail("");
+            }
+
             String sk = akSkConfig.getSk();
             if (StringUtils.isNotEmpty(sk)) {
                 String input = String.format("%s%s%s", ak, time, sk);
@@ -50,6 +57,14 @@ public class AkSignService {
                     return ResultContent.buildFail("签名验证错误");
                 }
             }
+            if (StringUtils.isEmpty(akSkConfig.getAboutDataId())) {
+                akSkConfig.setAboutDataId(gateWayId);
+            }
+            if (StringUtils.isEmpty(akSkConfig.getUseTimeStr())) {
+                akSkConfig.setUseTime(System.currentTimeMillis());
+                akSkConfig.setUseTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+            }
+            akSkConfigDao.save(akSkConfig);
         }
         return ResultContent.buildSuccess();
     }