From 8ea10bb813ec36c66964e769e0229803ef806747 Mon Sep 17 00:00:00 2001 From: kerwincui <164770707@qq.com> Date: Thu, 19 May 2022 10:56:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E8=AE=A4=E8=AF=81=E7=BB=9F?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/iot/controller/ToolController.java | 286 ++++++++++++------ .../ruoyi/iot/domain/ProductAuthorize.java | 6 +- .../com/ruoyi/iot/mapper/DeviceMapper.java | 4 +- .../iot/mapper/ProductAuthorizeMapper.java | 9 +- .../iot/model/MqttAuthenticationModel.java | 89 ++++++ ...{MqttAuthModel.java => MqttInfoModel.java} | 2 +- ...del.java => ProductAuthenticateModel.java} | 15 +- .../com/ruoyi/iot/service/IDeviceService.java | 2 +- .../iot/service/IProductAuthorizeService.java | 8 - .../iot/service/impl/DeviceServiceImpl.java | 4 +- .../impl/ProductAuthorizeServiceImpl.java | 67 ++-- .../java/com/ruoyi/iot/util/AESUtils.java | 6 +- .../resources/mapper/iot/DeviceMapper.xml | 7 +- .../mapper/iot/ProductAuthorizeMapper.xml | 14 +- 14 files changed, 325 insertions(+), 194 deletions(-) create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthenticationModel.java rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/{MqttAuthModel.java => MqttInfoModel.java} (96%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/{DeviceAuthenticateModel.java => ProductAuthenticateModel.java} (87%) diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/controller/ToolController.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/controller/ToolController.java index 13cc123f..82ad4f00 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/controller/ToolController.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/controller/ToolController.java @@ -1,6 +1,5 @@ package com.ruoyi.iot.controller; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.config.RuoYiConfig; @@ -10,17 +9,14 @@ import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException; +import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.file.FileUploadUtils; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.iot.domain.Device; import com.ruoyi.iot.domain.ProductAuthorize; -import com.ruoyi.iot.model.AuthenticateInputModel; -import com.ruoyi.iot.model.DeviceAuthenticateModel; -import com.ruoyi.iot.model.MqttClientConnectModel; -import com.ruoyi.iot.model.RegisterUserInput; -import com.ruoyi.iot.model.ThingsModels.IdentityAndName; -import com.ruoyi.iot.model.ThingsModels.ThingsModelValueItem; +import com.ruoyi.iot.mapper.ProductAuthorizeMapper; +import com.ruoyi.iot.model.*; import com.ruoyi.iot.model.ThingsModels.ThingsModelShadow; import com.ruoyi.iot.mqtt.EmqxService; import com.ruoyi.iot.mqtt.MqttConfig; @@ -56,7 +52,6 @@ import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; @@ -94,6 +89,9 @@ public class ToolController extends BaseController { @Autowired private IToolService toolService; + @Autowired + private ProductAuthorizeMapper productAuthorizeMapper; + // 令牌秘钥 @Value("${token.secret}") private String secret; @@ -114,108 +112,198 @@ public class ToolController extends BaseController { @ApiOperation("mqtt认证") @PostMapping("/mqtt/auth") public ResponseEntity mqttAuth(@RequestParam String clientid, @RequestParam String username, @RequestParam String password) throws Exception { - try { - if (clientid.startsWith("server")) { - // 服务端配置账号认证 - if (mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) { - System.out.println("-----------认证成功,clientId:" + clientid + "---------------"); - return ResponseEntity.ok().body("ok"); - } - } else if (clientid.startsWith("web") || clientid.startsWith("phone")) { - // web端和手机端token认证 - String token = password; - if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) { - token = token.replace(Constants.TOKEN_PREFIX, ""); - } - try { - Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); - System.out.println("-----------认证成功,clientId:" + clientid + "---------------"); - return ResponseEntity.ok().body("ok"); - } catch (Exception ex) { - return returnUnauthorized(clientid, username, password, ex.getMessage()); - } + if (clientid.startsWith("server")) { + // 服务端认证:配置的账号密码认证 + if (mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) { + log.info("-----------服务端mqtt认证成功,clientId:" + clientid + "---------------"); + return ResponseEntity.ok().body("ok"); } else { - // 设备端 - String[] clientInfo = clientid.split("&"); - if (clientInfo.length != 2) { - // 设备简单认证 - String deviceNum = clientInfo[0]; - Device device = deviceService.selectShortDeviceBySerialNumber(deviceNum); - if (device !=null && mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) { - System.out.println("-----------认证成功,clientId:" + clientid + "---------------"); -// ProductAuthorize authorize = new ProductAuthorize(null, device.getProductId(), device.getDeviceId(), device.getSerialNumber(), 1L, "admin"); -// authorizeService.boundProductAuthorize(authorize); - return ResponseEntity.ok().body("ok"); - } - return returnUnauthorized(clientid, username, password, "认证信息有误"); - } - // 设备加密认证 - String deviceNum = clientInfo[0]; - Long productId = Long.valueOf(clientInfo[1]); - AuthenticateInputModel authenticateInputModel = new AuthenticateInputModel(deviceNum, productId); - DeviceAuthenticateModel model = deviceService.selectDeviceAuthenticate(authenticateInputModel); - if (model == null) { - return returnUnauthorized(clientid, username, password, "认证信息有误"); - } - // 密码解密,密码加密格式 password & productId & userId & expireTime - String decryptPassword = AESUtils.decrypt(password, model.getMqttSecret()); - if (decryptPassword == null || decryptPassword == "") { - return returnUnauthorized(clientid, username, password, "认证信息有误"); - } - String[] infos = decryptPassword.split("&"); - if (infos.length != 3) { - return returnUnauthorized(clientid, username, password, "认证信息有误"); - } - String mqttPassword = infos[0]; - Long userId = Long.valueOf(infos[1]); - Long expireTime = Long.valueOf(infos[2]); - // 账号密码验证,产品必须为发布状态:1-未发布,2-已发布 - if (mqttPassword.equals(model.getMqttPassword()) - && username.equals(model.getMqttAccount()) - && expireTime > System.currentTimeMillis() - && model.getProductStatus() == 2) { - - // 设备状态验证 (1-未激活,2-禁用,3-在线,4-离线) - if (model.getDeviceId() != null && model.getDeviceId() != 0 && model.getStatus() != 2) { - System.out.println("-----------认证成功,clientId:" + clientid + "---------------"); -// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin"); -// authorizeService.boundProductAuthorize(authorize); - return ResponseEntity.ok().body("ok"); - } else { - // 自动添加设备 - int result = deviceService.insertDeviceAuto(deviceNum, userId, productId); - if (result == 1) { - System.out.println("-----------认证成功,clientId:" + clientid + "---------------"); -// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin"); -// authorizeService.boundProductAuthorize(authorize); - return ResponseEntity.ok().body("ok"); - } - } - } + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "mqtt账号和密码与认证服务器配置不匹配"); + } + } else if (clientid.startsWith("web") || clientid.startsWith("phone")) { + // web端和移动端认证:token认证 + String token = password; + if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) { + token = token.replace(Constants.TOKEN_PREFIX, ""); + } + try { + Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); + log.info("-----------移动端/Web端mqtt认证成功,clientId:" + clientid + "---------------"); + return ResponseEntity.ok().body("ok"); + } catch (Exception ex) { + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), ex.getMessage()); + } + } else { + // 设备端认证:加密认证(E)和简单认证(S,配置的账号密码认证) + String[] clientArray = clientid.split("&"); + if(clientArray.length != 4 || clientArray[0].equals("") || clientArray[1].equals("") || clientArray[2].equals("") || clientArray[3].equals("")){ + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备mqtt客户端Id格式为:认证类型 & 设备编号 & 产品ID & 用户ID"); + } + String authType = clientArray[0]; + String deviceNumber = clientArray[1]; + Long productId = Long.valueOf(clientArray[2]); + Long userId = Long.valueOf(clientArray[3]); + // 产品认证信息 + ProductAuthenticateModel model = deviceService.selectProductAuthenticate(new AuthenticateInputModel(deviceNumber, productId)); + if (model == null) { + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备认证,通过产品ID查询不到信息"); + } + if (model.getProductStatus() != 2) { + // 产品必须为发布状态:1-未发布,2-已发布 + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备认证,设备对应产品还未发布"); + } + + if (authType.equals("S")) { + // 设备简单认证 + return simpleMqttAuthentication(new MqttAuthenticationModel(clientid, username, password, deviceNumber, productId, userId), model); + + } else if (authType.equals("E")) { + // 设备加密认证 + return encryptAuthentication(new MqttAuthenticationModel(clientid, username, password, deviceNumber, productId, userId), model); + } else { + return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备认证,认证类型有误"); } - } catch (Exception ex) { - // ex.printStackTrace(); - return returnUnauthorized(clientid, username, password, ex.getMessage()); } - return returnUnauthorized(clientid, username, password, "认证信息有误"); + } + + /** + * 设备简单认证 + */ + private ResponseEntity simpleMqttAuthentication(MqttAuthenticationModel mqttModel, ProductAuthenticateModel productModel) { + String[] passwordArray = mqttModel.getPassword().split("&"); + if (productModel.getIsAuthorize() == 1 && passwordArray.length != 2) { + return returnUnauthorized(mqttModel, "设备简单认证,产品启用授权码后,密码格式为:密码 & 授权码"); + } + String mqttPassword = passwordArray[0]; + String authCode = passwordArray.length == 2 ? passwordArray[1] : ""; + if ((!mqttConfig.getusername().equals(mqttModel.getUserName())) || (!mqttConfig.getpassword().equals(mqttPassword))) { + return returnUnauthorized(mqttModel, "设备简单认证,mqtt账号和密码与认证服务器配置不匹配"); + } + // 授权码处理 + if (productModel.getIsAuthorize() == 1) { + // 授权码验证和处理 + String resultMessage = authCodeProcess(authCode, mqttModel, productModel); + if (resultMessage != "") { + return returnUnauthorized(mqttModel, resultMessage); + } + } + if (productModel.getDeviceId() != null && productModel.getDeviceId() != 0) { + if (productModel.getStatus() == 2) { + return returnUnauthorized(mqttModel, "设备简单认证,设备处于禁用状态"); + } + log.info("-----------设备简单认证成功,clientId:" + mqttModel.getClientId() + "---------------"); + return ResponseEntity.ok().body("ok"); + } else { + // 自动添加设备 + int result = deviceService.insertDeviceAuto(mqttModel.getDeviceNumber(), mqttModel.getUserId(), mqttModel.getProductId()); + if (result == 1) { + log.info("-----------设备简单认证成功,并自动添加设备到系统,clientId:" + mqttModel.getClientId() + "---------------"); + return ResponseEntity.ok().body("ok"); + } + return returnUnauthorized(mqttModel, "设备简单认证,自动添加设备失败"); + } + } + + /** + * 设备加密认证 + * + * @return + */ + private ResponseEntity encryptAuthentication(MqttAuthenticationModel mqttModel, ProductAuthenticateModel productModel) throws Exception { + String decryptPassword = AESUtils.decrypt(mqttModel.getPassword(), productModel.getMqttSecret()); + if (decryptPassword == null || decryptPassword == "") { + return returnUnauthorized(mqttModel, "设备加密认证,mqtt密码解密失败"); + } + String[] passwordArray = decryptPassword.split("&"); + if (passwordArray.length != 2 && passwordArray.length != 3) { + // 密码加密格式 password & expireTime (& authCode 可选) + return returnUnauthorized(mqttModel, "设备加密认证,mqtt密码加密格式为:密码 & 过期时间 & 授权码,其中授权码为可选"); + } + String mqttPassword = passwordArray[0]; + Long expireTime = Long.valueOf(passwordArray[1]); + String authCode = passwordArray.length == 3 ? passwordArray[2] : ""; + if (productModel.getIsAuthorize() == 1) { + // 授权码验证和处理 + String resultMessage = authCodeProcess(authCode, mqttModel, productModel); + if (resultMessage != "") { + return returnUnauthorized(mqttModel, resultMessage); + } + } + + if (!mqttPassword.equals(productModel.getMqttPassword())) { + return returnUnauthorized(mqttModel, "设备加密认证,设备mqtt密码错误"); + } + if (!mqttModel.getUserName().equals(productModel.getMqttAccount())) { + return returnUnauthorized(mqttModel, "设备加密认证,设备mqtt用户名错误"); + } + if (expireTime < System.currentTimeMillis()) { + return returnUnauthorized(mqttModel, "设备加密认证,设备mqtt密码已过期"); + } + // 设备状态验证 (1-未激活,2-禁用,3-在线,4-离线) + if (productModel.getDeviceId() != null && productModel.getDeviceId() != 0) { + if (productModel.getStatus() == 2) { + return returnUnauthorized(mqttModel, "设备加密认证,设备处于禁用状态"); + } + log.info("-----------设备加密认证成功,clientId:" + mqttModel.getClientId() + "---------------"); + return ResponseEntity.ok().body("ok"); + } else { + // 自动添加设备 + int result = deviceService.insertDeviceAuto(mqttModel.getDeviceNumber(), mqttModel.getUserId(), mqttModel.getProductId()); + if (result == 1) { + log.info("-----------设备加密认证成功,并自动添加设备到系统,clientId:" + mqttModel.getClientId() + "---------------"); + return ResponseEntity.ok().body("ok"); + } + return returnUnauthorized(mqttModel, "设备加密认证,自动添加设备失败"); + } + } + + /** + * 授权码认证和处理 + */ + private String authCodeProcess(String authCode, MqttAuthenticationModel mqttModel, ProductAuthenticateModel productModel) { + String message = ""; + if (authCode == "") { + message = "设备认证,设备授权码不能为空"; + } + // 查询授权码是否存在 + ProductAuthorize authorize = productAuthorizeMapper.selectFirstAuthorizeByAuthorizeCode(new ProductAuthorize(authCode, productModel.getProductId())); + if (authorize == null) { + message = "设备认证,设备授权码错误"; + return message; + } + if (authorize.getSerialNumber() != null && !authorize.getSerialNumber().equals("")) { + // 授权码已关联设备 + if (authorize.getSerialNumber() != productModel.getSerialNumber()) { + message = "设备认证,设备授权码已经分配给其他设备"; + return message; + } + } else { + // 授权码未关联设备 + authorize.setSerialNumber(productModel.getSerialNumber()); + authorize.setDeviceId(productModel.getDeviceId()); + authorize.setUserId(mqttModel.getUserId()); + authorize.setUserName(""); + authorize.setUpdateTime(DateUtils.getNowDate()); + int result = productAuthorizeMapper.updateProductAuthorize(authorize); + if (result != 1) { + message = "设备认证,设备授权码关联失败"; + return message; + } + } + return message; } /** * 返回认证信息 */ - private ResponseEntity returnUnauthorized(String clientid, String username, String password, String message) { - System.out.println("认证失败:" + message - + "\nclientid:" + clientid - + "\nusername:" + username - + "\npassword:" + password); - log.error("认证失败:" + message - + "\nclientid:" + clientid - + "\nusername:" + username - + "\npassword:" + password); + private ResponseEntity returnUnauthorized(MqttAuthenticationModel mqttModel, String message) { + log.warn("认证失败:" + message + + "\nclientid:" + mqttModel.getClientId() + + "\nusername:" + mqttModel.getUserName() + + "\npassword:" + mqttModel.getPassword()); return ResponseEntity.status(401).body("Unauthorized"); } - @ApiOperation("mqtt钩子处理") @PostMapping("/mqtt/webhook") public void webHookProcess(@RequestBody MqttClientConnectModel model) { diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/ProductAuthorize.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/ProductAuthorize.java index cacd59cd..f2b23a8e 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/ProductAuthorize.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/ProductAuthorize.java @@ -48,13 +48,9 @@ public class ProductAuthorize extends BaseEntity public ProductAuthorize() { } - public ProductAuthorize(String authorizeCode, Long productId, Long deviceId, String serialNumber, Long userId, String userName) { + public ProductAuthorize(String authorizeCode, Long productId) { this.authorizeCode = authorizeCode; this.productId = productId; - this.deviceId = deviceId; - this.serialNumber = serialNumber; - this.userId = userId; - this.userName = userName; } public void setAuthorizeId(Long authorizeId) diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceMapper.java index 91ad3caa..2a9b7c45 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceMapper.java @@ -3,7 +3,7 @@ package com.ruoyi.iot.mapper; import com.ruoyi.iot.domain.Device; import com.ruoyi.iot.model.AuthenticateInputModel; import com.ruoyi.iot.model.DeviceAllShortOutput; -import com.ruoyi.iot.model.DeviceAuthenticateModel; +import com.ruoyi.iot.model.ProductAuthenticateModel; import com.ruoyi.iot.model.DeviceShortOutput; import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesInput; import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesOutput; @@ -50,7 +50,7 @@ public interface DeviceMapper * @param model 设备编号和产品ID * @return 设备 */ - public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model); + public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model); /** * 查询设备和运行状态 diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/ProductAuthorizeMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/ProductAuthorizeMapper.java index b1ac0cbf..8f29e197 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/ProductAuthorizeMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/ProductAuthorizeMapper.java @@ -66,17 +66,10 @@ public interface ProductAuthorizeMapper */ public int insertBatchAuthorize(List list); - /** - * 根据产品id查询一条未绑定的授权码 - * @param authorize - * @return - */ - ProductAuthorize selectOneUnboundAuthorizeByProductId(ProductAuthorize authorize); - /** * 根据授权码查询一条未绑定的授权码 * @param authorize * @return */ - ProductAuthorize selectOneUnboundAuthorizeByAuthorizeCode(ProductAuthorize authorize); + ProductAuthorize selectFirstAuthorizeByAuthorizeCode(ProductAuthorize authorize); } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthenticationModel.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthenticationModel.java new file mode 100644 index 00000000..1cf2dd63 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthenticationModel.java @@ -0,0 +1,89 @@ +package com.ruoyi.iot.model; + +/** + * 动作 + * @author kerwincui + * @date 2021-12-16 + */ +public class MqttAuthenticationModel +{ + /** Mqtt客户端ID */ + String clientId; + + /** Mqtt用户名 */ + String userName; + + /** Mqtt密码 */ + String password; + + /** 设备编号 */ + String deviceNumber; + + /** 产品ID */ + Long productId; + + /** 设备关联的用户ID */ + Long userId; + + public MqttAuthenticationModel(String clientid,String username,String password,String deviceNumber ,Long productId,Long userId){ + this.clientId=clientid; + this.userName=username; + this.password=password; + this.deviceNumber=deviceNumber; + this.productId=productId; + this.userId=userId; + } + public MqttAuthenticationModel(String clientid,String username,String password){ + this.clientId=clientid; + this.userName=username; + this.password=password; + } + + public String getClientId() { + return clientId; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDeviceNumber() { + return deviceNumber; + } + + public void setDeviceNumber(String deviceNumber) { + this.deviceNumber = deviceNumber; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthModel.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttInfoModel.java similarity index 96% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthModel.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttInfoModel.java index 9597fa89..553929b5 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttAuthModel.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/MqttInfoModel.java @@ -9,7 +9,7 @@ import org.apache.commons.lang3.builder.ToStringStyle; * @author kerwincui * @date 2021-12-16 */ -public class MqttAuthModel +public class MqttInfoModel { private String clientid; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/DeviceAuthenticateModel.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/ProductAuthenticateModel.java similarity index 87% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/DeviceAuthenticateModel.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/ProductAuthenticateModel.java index 188ef6f3..feb01688 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/DeviceAuthenticateModel.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/model/ProductAuthenticateModel.java @@ -1,7 +1,9 @@ package com.ruoyi.iot.model; -public class DeviceAuthenticateModel { +import com.ruoyi.common.annotation.Excel; + +public class ProductAuthenticateModel { /** 产品分类ID */ private Long deviceId; @@ -20,6 +22,9 @@ public class DeviceAuthenticateModel { /** 产品状态 1-未发布,2-已发布 */ private int productStatus; + /** 是否启用授权码(0-否,1-是) */ + private Integer isAuthorize; + /** 设备编号 */ private String serialNumber; @@ -32,6 +37,14 @@ public class DeviceAuthenticateModel { /** 产品秘钥 */ private String mqttSecret; + public Integer getIsAuthorize() { + return isAuthorize; + } + + public void setIsAuthorize(Integer isAuthorize) { + this.isAuthorize = isAuthorize; + } + public int getProductStatus() { return productStatus; } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IDeviceService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IDeviceService.java index 4825318f..d9552ff5 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IDeviceService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IDeviceService.java @@ -46,7 +46,7 @@ public interface IDeviceService * @param model 设备编号和产品ID * @return 设备 */ - public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model); + public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model); /** * 查询设备和运行状态 diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IProductAuthorizeService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IProductAuthorizeService.java index 38d35469..f4fe9f29 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IProductAuthorizeService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/IProductAuthorizeService.java @@ -67,12 +67,4 @@ public interface IProductAuthorizeService */ public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO); - - /** - * 根据产品id和设备序列号绑定授权码 - * @param productAuthorize - * @return - */ - public int boundProductAuthorize(ProductAuthorize productAuthorize); - } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java index c32a0408..b487506a 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java @@ -116,8 +116,8 @@ public class DeviceServiceImpl implements IDeviceService { * @return 设备 */ @Override - public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model) { - return deviceMapper.selectDeviceAuthenticate(model); + public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model) { + return deviceMapper.selectProductAuthenticate(model); } /** diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/ProductAuthorizeServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/ProductAuthorizeServiceImpl.java index b270ec35..c7777887 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/ProductAuthorizeServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/ProductAuthorizeServiceImpl.java @@ -21,98 +21,92 @@ import static com.ruoyi.common.utils.SecurityUtils.getLoginUser; /** * 产品授权码Service业务层处理 - * + * * @author kami * @date 2022-04-11 */ @Service -public class ProductAuthorizeServiceImpl implements IProductAuthorizeService -{ +public class ProductAuthorizeServiceImpl implements IProductAuthorizeService { @Autowired private ProductAuthorizeMapper productAuthorizeMapper; /** * 查询产品授权码 - * + * * @param authorizeId 产品授权码主键 * @return 产品授权码 */ @Override - public ProductAuthorize selectProductAuthorizeByAuthorizeId(Long authorizeId) - { + public ProductAuthorize selectProductAuthorizeByAuthorizeId(Long authorizeId) { return productAuthorizeMapper.selectProductAuthorizeByAuthorizeId(authorizeId); } /** * 查询产品授权码列表 - * + * * @param productAuthorize 产品授权码 * @return 产品授权码 */ @Override - public List selectProductAuthorizeList(ProductAuthorize productAuthorize) - { + public List selectProductAuthorizeList(ProductAuthorize productAuthorize) { return productAuthorizeMapper.selectProductAuthorizeList(productAuthorize); } /** * 新增产品授权码 - * + * * @param productAuthorize 产品授权码 * @return 结果 */ @Override - public int insertProductAuthorize(ProductAuthorize productAuthorize) - { + public int insertProductAuthorize(ProductAuthorize productAuthorize) { productAuthorize.setCreateTime(DateUtils.getNowDate()); return productAuthorizeMapper.insertProductAuthorize(productAuthorize); } /** * 修改产品授权码 - * + * * @param productAuthorize 产品授权码 * @return 结果 */ @Override - public int updateProductAuthorize(ProductAuthorize productAuthorize) - { + public int updateProductAuthorize(ProductAuthorize productAuthorize) { productAuthorize.setUpdateTime(DateUtils.getNowDate()); return productAuthorizeMapper.updateProductAuthorize(productAuthorize); } /** * 批量删除产品授权码 - * + * * @param authorizeIds 需要删除的产品授权码主键 * @return 结果 */ @Override - public int deleteProductAuthorizeByAuthorizeIds(Long[] authorizeIds) - { + public int deleteProductAuthorizeByAuthorizeIds(Long[] authorizeIds) { return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeIds(authorizeIds); } /** * 删除产品授权码信息 - * + * * @param authorizeId 产品授权码主键 * @return 结果 */ @Override - public int deleteProductAuthorizeByAuthorizeId(Long authorizeId) - { + public int deleteProductAuthorizeByAuthorizeId(Long authorizeId) { return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeId(authorizeId); } /** * 根据数量批量新增产品授权码 + * * @param productAuthorizeVO * @return */ - @Override - @Transactional - public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO) { + @Override + @Transactional(rollbackFor = Exception.class) + public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO) { Long productId = productAuthorizeVO.getProductId(); int createNum = productAuthorizeVO.getCreateNum(); List list = new ArrayList<>(createNum); @@ -126,31 +120,6 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService list.add(authorize); } return productAuthorizeMapper.insertBatchAuthorize(list); - } - - /** - * 根据产品id和设备序列号绑定授权码 - * - * @param productAuthorize - * @return - */ - @Override - @Transactional - public int boundProductAuthorize(ProductAuthorize productAuthorize){ - ProductAuthorize authorize = null; - if(StringUtils.isEmpty(productAuthorize.getAuthorizeCode())){ - //TODO-kami: 2022/4/11 13:34 后期无需查询,硬件调用直接传入参数,可以删除 - authorize = productAuthorizeMapper.selectOneUnboundAuthorizeByProductId(productAuthorize); - productAuthorize.setAuthorizeCode(authorize.getAuthorizeCode()); - }else { - authorize = productAuthorizeMapper.selectOneUnboundAuthorizeByAuthorizeCode(productAuthorize); - } - if (authorize == null){ - throw new ServiceException("授权码数据异常", HttpStatus.BAD_REQUEST); - } - productAuthorize.setAuthorizeId(authorize.getAuthorizeId()); - productAuthorize.setUpdateTime(DateUtils.getNowDate()); - return productAuthorizeMapper.updateProductAuthorize(productAuthorize); } } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/AESUtils.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/AESUtils.java index de627797..a1a6d327 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/AESUtils.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/AESUtils.java @@ -59,10 +59,10 @@ public class AESUtils { IvParameterSpec iv = new IvParameterSpec(ivString.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); //先用base64解密 - byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc); + byte[] encrypted = new BASE64Decoder().decodeBuffer(sSrc); try { - byte[] original = cipher.doFinal(encrypted1); - String originalString = new String(original); + byte[] original = cipher.doFinal(encrypted); + String originalString = new String(original,"UTF-8"); return originalString; } catch (Exception e) { System.out.println(e.toString()); diff --git a/springboot/wumei-iot/src/main/resources/mapper/iot/DeviceMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/iot/DeviceMapper.xml index 9e6a7ff3..b250f71b 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/iot/DeviceMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/iot/DeviceMapper.xml @@ -68,13 +68,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + + @@ -197,8 +198,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where device_id = #{deviceId} - + SELECT p.mqtt_password,p.mqtt_account, p.mqtt_secret,p.is_authorize,p.product_id,p.product_name,p.STATUS as product_status,d.device_id,d.device_name,d.STATUS,d.serial_number FROM iot_product p LEFT JOIN ( SELECT device_id, device_name, STATUS, product_id, product_name, serial_number FROM iot_device diff --git a/springboot/wumei-iot/src/main/resources/mapper/iot/ProductAuthorizeMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/iot/ProductAuthorizeMapper.xml index 15f424fe..7624e39e 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/iot/ProductAuthorizeMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/iot/ProductAuthorizeMapper.xml @@ -42,23 +42,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where authorize_id = #{authorizeId} - - where del_flag = 0 and authorize_code = #{authorizeCode} - and serial_number is null - and device_id is null + and product_id = #{productId} limit 1