mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-17 16:36:03 +08:00
设备认证统一
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package com.ruoyi.iot.controller;
|
package com.ruoyi.iot.controller;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.annotation.Log;
|
||||||
import com.ruoyi.common.config.RuoYiConfig;
|
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.core.redis.RedisCache;
|
||||||
import com.ruoyi.common.enums.BusinessType;
|
import com.ruoyi.common.enums.BusinessType;
|
||||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||||
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||||
import com.ruoyi.common.utils.file.FileUtils;
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
import com.ruoyi.iot.domain.Device;
|
import com.ruoyi.iot.domain.Device;
|
||||||
import com.ruoyi.iot.domain.ProductAuthorize;
|
import com.ruoyi.iot.domain.ProductAuthorize;
|
||||||
import com.ruoyi.iot.model.AuthenticateInputModel;
|
import com.ruoyi.iot.mapper.ProductAuthorizeMapper;
|
||||||
import com.ruoyi.iot.model.DeviceAuthenticateModel;
|
import com.ruoyi.iot.model.*;
|
||||||
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.model.ThingsModels.ThingsModelShadow;
|
import com.ruoyi.iot.model.ThingsModels.ThingsModelShadow;
|
||||||
import com.ruoyi.iot.mqtt.EmqxService;
|
import com.ruoyi.iot.mqtt.EmqxService;
|
||||||
import com.ruoyi.iot.mqtt.MqttConfig;
|
import com.ruoyi.iot.mqtt.MqttConfig;
|
||||||
@@ -56,7 +52,6 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
@@ -94,6 +89,9 @@ public class ToolController extends BaseController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IToolService toolService;
|
private IToolService toolService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProductAuthorizeMapper productAuthorizeMapper;
|
||||||
|
|
||||||
// 令牌秘钥
|
// 令牌秘钥
|
||||||
@Value("${token.secret}")
|
@Value("${token.secret}")
|
||||||
private String secret;
|
private String secret;
|
||||||
@@ -114,108 +112,198 @@ public class ToolController extends BaseController {
|
|||||||
@ApiOperation("mqtt认证")
|
@ApiOperation("mqtt认证")
|
||||||
@PostMapping("/mqtt/auth")
|
@PostMapping("/mqtt/auth")
|
||||||
public ResponseEntity mqttAuth(@RequestParam String clientid, @RequestParam String username, @RequestParam String password) throws Exception {
|
public ResponseEntity mqttAuth(@RequestParam String clientid, @RequestParam String username, @RequestParam String password) throws Exception {
|
||||||
try {
|
|
||||||
if (clientid.startsWith("server")) {
|
if (clientid.startsWith("server")) {
|
||||||
// 服务端配置账号认证
|
// 服务端认证:配置的账号密码认证
|
||||||
if (mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) {
|
if (mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) {
|
||||||
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
|
log.info("-----------服务端mqtt认证成功,clientId:" + clientid + "---------------");
|
||||||
return ResponseEntity.ok().body("ok");
|
return ResponseEntity.ok().body("ok");
|
||||||
|
} else {
|
||||||
|
return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "mqtt账号和密码与认证服务器配置不匹配");
|
||||||
}
|
}
|
||||||
} else if (clientid.startsWith("web") || clientid.startsWith("phone")) {
|
} else if (clientid.startsWith("web") || clientid.startsWith("phone")) {
|
||||||
// web端和手机端token认证
|
// web端和移动端认证:token认证
|
||||||
String token = password;
|
String token = password;
|
||||||
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
|
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
|
||||||
token = token.replace(Constants.TOKEN_PREFIX, "");
|
token = token.replace(Constants.TOKEN_PREFIX, "");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
Claims claims = Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||||
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
|
log.info("-----------移动端/Web端mqtt认证成功,clientId:" + clientid + "---------------");
|
||||||
return ResponseEntity.ok().body("ok");
|
return ResponseEntity.ok().body("ok");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
return returnUnauthorized(clientid, username, password, ex.getMessage());
|
return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), ex.getMessage());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 设备端
|
// 设备端认证:加密认证(E)和简单认证(S,配置的账号密码认证)
|
||||||
String[] clientInfo = clientid.split("&");
|
String[] clientArray = clientid.split("&");
|
||||||
if (clientInfo.length != 2) {
|
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 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 authType = clientArray[0];
|
||||||
}
|
String deviceNumber = clientArray[1];
|
||||||
// 设备加密认证
|
Long productId = Long.valueOf(clientArray[2]);
|
||||||
String deviceNum = clientInfo[0];
|
Long userId = Long.valueOf(clientArray[3]);
|
||||||
Long productId = Long.valueOf(clientInfo[1]);
|
// 产品认证信息
|
||||||
AuthenticateInputModel authenticateInputModel = new AuthenticateInputModel(deviceNum, productId);
|
ProductAuthenticateModel model = deviceService.selectProductAuthenticate(new AuthenticateInputModel(deviceNumber, productId));
|
||||||
DeviceAuthenticateModel model = deviceService.selectDeviceAuthenticate(authenticateInputModel);
|
|
||||||
if (model == null) {
|
if (model == null) {
|
||||||
return returnUnauthorized(clientid, username, password, "认证信息有误");
|
return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备认证,通过产品ID查询不到信息");
|
||||||
}
|
}
|
||||||
// 密码解密,密码加密格式 password & productId & userId & expireTime
|
if (model.getProductStatus() != 2) {
|
||||||
String decryptPassword = AESUtils.decrypt(password, model.getMqttSecret());
|
// 产品必须为发布状态:1-未发布,2-已发布
|
||||||
if (decryptPassword == null || decryptPassword == "") {
|
return returnUnauthorized(new MqttAuthenticationModel(clientid, username, password), "设备认证,设备对应产品还未发布");
|
||||||
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 (authType.equals("S")) {
|
||||||
if (model.getDeviceId() != null && model.getDeviceId() != 0 && model.getStatus() != 2) {
|
// 设备简单认证
|
||||||
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
|
return simpleMqttAuthentication(new MqttAuthenticationModel(clientid, username, password, deviceNumber, productId, userId), model);
|
||||||
// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
|
|
||||||
// authorizeService.boundProductAuthorize(authorize);
|
} else if (authType.equals("E")) {
|
||||||
|
// 设备加密认证
|
||||||
|
return encryptAuthentication(new MqttAuthenticationModel(clientid, username, password, deviceNumber, productId, userId), model);
|
||||||
|
} else {
|
||||||
|
return returnUnauthorized(new MqttAuthenticationModel(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");
|
return ResponseEntity.ok().body("ok");
|
||||||
} else {
|
} else {
|
||||||
// 自动添加设备
|
// 自动添加设备
|
||||||
int result = deviceService.insertDeviceAuto(deviceNum, userId, productId);
|
int result = deviceService.insertDeviceAuto(mqttModel.getDeviceNumber(), mqttModel.getUserId(), mqttModel.getProductId());
|
||||||
if (result == 1) {
|
if (result == 1) {
|
||||||
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
|
log.info("-----------设备简单认证成功,并自动添加设备到系统,clientId:" + mqttModel.getClientId() + "---------------");
|
||||||
// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
|
|
||||||
// authorizeService.boundProductAuthorize(authorize);
|
|
||||||
return ResponseEntity.ok().body("ok");
|
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密码解密失败");
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
String[] passwordArray = decryptPassword.split("&");
|
||||||
// ex.printStackTrace();
|
if (passwordArray.length != 2 && passwordArray.length != 3) {
|
||||||
return returnUnauthorized(clientid, username, password, ex.getMessage());
|
// 密码加密格式 password & expireTime (& authCode 可选)
|
||||||
|
return returnUnauthorized(mqttModel, "设备加密认证,mqtt密码加密格式为:密码 & 过期时间 & 授权码,其中授权码为可选");
|
||||||
}
|
}
|
||||||
return returnUnauthorized(clientid, username, password, "认证信息有误");
|
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) {
|
private ResponseEntity returnUnauthorized(MqttAuthenticationModel mqttModel, String message) {
|
||||||
System.out.println("认证失败:" + message
|
log.warn("认证失败:" + message
|
||||||
+ "\nclientid:" + clientid
|
+ "\nclientid:" + mqttModel.getClientId()
|
||||||
+ "\nusername:" + username
|
+ "\nusername:" + mqttModel.getUserName()
|
||||||
+ "\npassword:" + password);
|
+ "\npassword:" + mqttModel.getPassword());
|
||||||
log.error("认证失败:" + message
|
|
||||||
+ "\nclientid:" + clientid
|
|
||||||
+ "\nusername:" + username
|
|
||||||
+ "\npassword:" + password);
|
|
||||||
return ResponseEntity.status(401).body("Unauthorized");
|
return ResponseEntity.status(401).body("Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation("mqtt钩子处理")
|
@ApiOperation("mqtt钩子处理")
|
||||||
@PostMapping("/mqtt/webhook")
|
@PostMapping("/mqtt/webhook")
|
||||||
public void webHookProcess(@RequestBody MqttClientConnectModel model) {
|
public void webHookProcess(@RequestBody MqttClientConnectModel model) {
|
||||||
|
|||||||
@@ -48,13 +48,9 @@ public class ProductAuthorize extends BaseEntity
|
|||||||
public ProductAuthorize() {
|
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.authorizeCode = authorizeCode;
|
||||||
this.productId = productId;
|
this.productId = productId;
|
||||||
this.deviceId = deviceId;
|
|
||||||
this.serialNumber = serialNumber;
|
|
||||||
this.userId = userId;
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAuthorizeId(Long authorizeId)
|
public void setAuthorizeId(Long authorizeId)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.ruoyi.iot.mapper;
|
|||||||
import com.ruoyi.iot.domain.Device;
|
import com.ruoyi.iot.domain.Device;
|
||||||
import com.ruoyi.iot.model.AuthenticateInputModel;
|
import com.ruoyi.iot.model.AuthenticateInputModel;
|
||||||
import com.ruoyi.iot.model.DeviceAllShortOutput;
|
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.DeviceShortOutput;
|
||||||
import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesInput;
|
import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesInput;
|
||||||
import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesOutput;
|
import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesOutput;
|
||||||
@@ -50,7 +50,7 @@ public interface DeviceMapper
|
|||||||
* @param model 设备编号和产品ID
|
* @param model 设备编号和产品ID
|
||||||
* @return 设备
|
* @return 设备
|
||||||
*/
|
*/
|
||||||
public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model);
|
public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询设备和运行状态
|
* 查询设备和运行状态
|
||||||
|
|||||||
@@ -66,17 +66,10 @@ public interface ProductAuthorizeMapper
|
|||||||
*/
|
*/
|
||||||
public int insertBatchAuthorize(List<ProductAuthorize> list);
|
public int insertBatchAuthorize(List<ProductAuthorize> list);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据产品id查询一条未绑定的授权码
|
|
||||||
* @param authorize
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
ProductAuthorize selectOneUnboundAuthorizeByProductId(ProductAuthorize authorize);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据授权码查询一条未绑定的授权码
|
* 根据授权码查询一条未绑定的授权码
|
||||||
* @param authorize
|
* @param authorize
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ProductAuthorize selectOneUnboundAuthorizeByAuthorizeCode(ProductAuthorize authorize);
|
ProductAuthorize selectFirstAuthorizeByAuthorizeCode(ProductAuthorize authorize);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
|||||||
* @author kerwincui
|
* @author kerwincui
|
||||||
* @date 2021-12-16
|
* @date 2021-12-16
|
||||||
*/
|
*/
|
||||||
public class MqttAuthModel
|
public class MqttInfoModel
|
||||||
{
|
{
|
||||||
private String clientid;
|
private String clientid;
|
||||||
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.ruoyi.iot.model;
|
package com.ruoyi.iot.model;
|
||||||
|
|
||||||
|
|
||||||
public class DeviceAuthenticateModel {
|
import com.ruoyi.common.annotation.Excel;
|
||||||
|
|
||||||
|
public class ProductAuthenticateModel {
|
||||||
/** 产品分类ID */
|
/** 产品分类ID */
|
||||||
private Long deviceId;
|
private Long deviceId;
|
||||||
|
|
||||||
@@ -20,6 +22,9 @@ public class DeviceAuthenticateModel {
|
|||||||
/** 产品状态 1-未发布,2-已发布 */
|
/** 产品状态 1-未发布,2-已发布 */
|
||||||
private int productStatus;
|
private int productStatus;
|
||||||
|
|
||||||
|
/** 是否启用授权码(0-否,1-是) */
|
||||||
|
private Integer isAuthorize;
|
||||||
|
|
||||||
/** 设备编号 */
|
/** 设备编号 */
|
||||||
private String serialNumber;
|
private String serialNumber;
|
||||||
|
|
||||||
@@ -32,6 +37,14 @@ public class DeviceAuthenticateModel {
|
|||||||
/** 产品秘钥 */
|
/** 产品秘钥 */
|
||||||
private String mqttSecret;
|
private String mqttSecret;
|
||||||
|
|
||||||
|
public Integer getIsAuthorize() {
|
||||||
|
return isAuthorize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsAuthorize(Integer isAuthorize) {
|
||||||
|
this.isAuthorize = isAuthorize;
|
||||||
|
}
|
||||||
|
|
||||||
public int getProductStatus() {
|
public int getProductStatus() {
|
||||||
return productStatus;
|
return productStatus;
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public interface IDeviceService
|
|||||||
* @param model 设备编号和产品ID
|
* @param model 设备编号和产品ID
|
||||||
* @return 设备
|
* @return 设备
|
||||||
*/
|
*/
|
||||||
public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model);
|
public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询设备和运行状态
|
* 查询设备和运行状态
|
||||||
|
|||||||
@@ -67,12 +67,4 @@ public interface IProductAuthorizeService
|
|||||||
*/
|
*/
|
||||||
public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO);
|
public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据产品id和设备序列号绑定授权码
|
|
||||||
* @param productAuthorize
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public int boundProductAuthorize(ProductAuthorize productAuthorize);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,8 +116,8 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
* @return 设备
|
* @return 设备
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DeviceAuthenticateModel selectDeviceAuthenticate(AuthenticateInputModel model) {
|
public ProductAuthenticateModel selectProductAuthenticate(AuthenticateInputModel model) {
|
||||||
return deviceMapper.selectDeviceAuthenticate(model);
|
return deviceMapper.selectProductAuthenticate(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,8 +26,7 @@ import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;
|
|||||||
* @date 2022-04-11
|
* @date 2022-04-11
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
public class ProductAuthorizeServiceImpl implements IProductAuthorizeService {
|
||||||
{
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProductAuthorizeMapper productAuthorizeMapper;
|
private ProductAuthorizeMapper productAuthorizeMapper;
|
||||||
|
|
||||||
@@ -38,8 +37,7 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 产品授权码
|
* @return 产品授权码
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ProductAuthorize selectProductAuthorizeByAuthorizeId(Long authorizeId)
|
public ProductAuthorize selectProductAuthorizeByAuthorizeId(Long authorizeId) {
|
||||||
{
|
|
||||||
return productAuthorizeMapper.selectProductAuthorizeByAuthorizeId(authorizeId);
|
return productAuthorizeMapper.selectProductAuthorizeByAuthorizeId(authorizeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,8 +48,7 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 产品授权码
|
* @return 产品授权码
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<ProductAuthorize> selectProductAuthorizeList(ProductAuthorize productAuthorize)
|
public List<ProductAuthorize> selectProductAuthorizeList(ProductAuthorize productAuthorize) {
|
||||||
{
|
|
||||||
return productAuthorizeMapper.selectProductAuthorizeList(productAuthorize);
|
return productAuthorizeMapper.selectProductAuthorizeList(productAuthorize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,8 +59,7 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int insertProductAuthorize(ProductAuthorize productAuthorize)
|
public int insertProductAuthorize(ProductAuthorize productAuthorize) {
|
||||||
{
|
|
||||||
productAuthorize.setCreateTime(DateUtils.getNowDate());
|
productAuthorize.setCreateTime(DateUtils.getNowDate());
|
||||||
return productAuthorizeMapper.insertProductAuthorize(productAuthorize);
|
return productAuthorizeMapper.insertProductAuthorize(productAuthorize);
|
||||||
}
|
}
|
||||||
@@ -75,8 +71,7 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int updateProductAuthorize(ProductAuthorize productAuthorize)
|
public int updateProductAuthorize(ProductAuthorize productAuthorize) {
|
||||||
{
|
|
||||||
productAuthorize.setUpdateTime(DateUtils.getNowDate());
|
productAuthorize.setUpdateTime(DateUtils.getNowDate());
|
||||||
return productAuthorizeMapper.updateProductAuthorize(productAuthorize);
|
return productAuthorizeMapper.updateProductAuthorize(productAuthorize);
|
||||||
}
|
}
|
||||||
@@ -88,8 +83,7 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int deleteProductAuthorizeByAuthorizeIds(Long[] authorizeIds)
|
public int deleteProductAuthorizeByAuthorizeIds(Long[] authorizeIds) {
|
||||||
{
|
|
||||||
return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeIds(authorizeIds);
|
return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeIds(authorizeIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,18 +94,18 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int deleteProductAuthorizeByAuthorizeId(Long authorizeId)
|
public int deleteProductAuthorizeByAuthorizeId(Long authorizeId) {
|
||||||
{
|
|
||||||
return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeId(authorizeId);
|
return productAuthorizeMapper.deleteProductAuthorizeByAuthorizeId(authorizeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据数量批量新增产品授权码
|
* 根据数量批量新增产品授权码
|
||||||
|
*
|
||||||
* @param productAuthorizeVO
|
* @param productAuthorizeVO
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO) {
|
public int addProductAuthorizeByNum(ProductAuthorizeVO productAuthorizeVO) {
|
||||||
Long productId = productAuthorizeVO.getProductId();
|
Long productId = productAuthorizeVO.getProductId();
|
||||||
int createNum = productAuthorizeVO.getCreateNum();
|
int createNum = productAuthorizeVO.getCreateNum();
|
||||||
@@ -128,29 +122,4 @@ public class ProductAuthorizeServiceImpl implements IProductAuthorizeService
|
|||||||
return productAuthorizeMapper.insertBatchAuthorize(list);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,10 @@ public class AESUtils {
|
|||||||
IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
|
IvParameterSpec iv = new IvParameterSpec(ivString.getBytes());
|
||||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
||||||
//先用base64解密
|
//先用base64解密
|
||||||
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
|
byte[] encrypted = new BASE64Decoder().decodeBuffer(sSrc);
|
||||||
try {
|
try {
|
||||||
byte[] original = cipher.doFinal(encrypted1);
|
byte[] original = cipher.doFinal(encrypted);
|
||||||
String originalString = new String(original);
|
String originalString = new String(original,"UTF-8");
|
||||||
return originalString;
|
return originalString;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.toString());
|
System.out.println(e.toString());
|
||||||
|
|||||||
@@ -68,13 +68,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<result property="latitude" column="latitude" />
|
<result property="latitude" column="latitude" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<resultMap type="com.ruoyi.iot.model.DeviceAuthenticateModel" id="DeviceAuthenticateResult">
|
<resultMap type="com.ruoyi.iot.model.ProductAuthenticateModel" id="DeviceAuthenticateResult">
|
||||||
<result property="deviceId" column="device_id" />
|
<result property="deviceId" column="device_id" />
|
||||||
<result property="deviceName" column="device_name" />
|
<result property="deviceName" column="device_name" />
|
||||||
<result property="status" column="status" />
|
<result property="status" column="status" />
|
||||||
<result property="productId" column="product_id" />
|
<result property="productId" column="product_id" />
|
||||||
<result property="productName" column="product_name" />
|
<result property="productName" column="product_name" />
|
||||||
<result property="productStatus" column="product_status" />
|
<result property="productStatus" column="product_status" />
|
||||||
|
<result property="isAuthorize" column="is_authorize" />
|
||||||
<result property="serialNumber" column="serial_number" />
|
<result property="serialNumber" column="serial_number" />
|
||||||
<result property="mqttAccount" column="mqtt_account" />
|
<result property="mqttAccount" column="mqtt_account" />
|
||||||
<result property="mqttPassword" column="mqtt_password" />
|
<result property="mqttPassword" column="mqtt_password" />
|
||||||
@@ -197,8 +198,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where device_id = #{deviceId}
|
where device_id = #{deviceId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectDeviceAuthenticate" parameterType="com.ruoyi.iot.model.AuthenticateInputModel" resultMap="DeviceAuthenticateResult">
|
<select id="selectProductAuthenticate" parameterType="com.ruoyi.iot.model.AuthenticateInputModel" resultMap="DeviceAuthenticateResult">
|
||||||
SELECT p.mqtt_password,p.mqtt_account, p.mqtt_secret,p.STATUS as product_status,d.device_id,d.device_name,d.STATUS,d.product_id,d.product_name,d.serial_number
|
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
|
FROM iot_product p
|
||||||
LEFT JOIN ( SELECT device_id, device_name, STATUS, product_id, product_name, serial_number
|
LEFT JOIN ( SELECT device_id, device_name, STATUS, product_id, product_name, serial_number
|
||||||
FROM iot_device
|
FROM iot_device
|
||||||
|
|||||||
@@ -42,23 +42,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<include refid="selectProductAuthorizeVo"/>
|
<include refid="selectProductAuthorizeVo"/>
|
||||||
where authorize_id = #{authorizeId}
|
where authorize_id = #{authorizeId}
|
||||||
</select>
|
</select>
|
||||||
<select id="selectOneUnboundAuthorizeByProductId" parameterType="com.ruoyi.iot.domain.ProductAuthorize" resultType="com.ruoyi.iot.domain.ProductAuthorize">
|
|
||||||
<include refid="selectProductAuthorizeVo"/>
|
|
||||||
where
|
|
||||||
del_flag = 0
|
|
||||||
and product_id = #{productId}
|
|
||||||
and serial_number is null
|
|
||||||
and device_id is null
|
|
||||||
limit 1
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<select id="selectOneUnboundAuthorizeByAuthorizeCode" parameterType="com.ruoyi.iot.domain.ProductAuthorize" resultType="com.ruoyi.iot.domain.ProductAuthorize">
|
<select id="selectFirstAuthorizeByAuthorizeCode" parameterType="com.ruoyi.iot.domain.ProductAuthorize" resultMap="ProductAuthorizeResult">
|
||||||
<include refid="selectProductAuthorizeVo"/>
|
<include refid="selectProductAuthorizeVo"/>
|
||||||
where
|
where
|
||||||
del_flag = 0
|
del_flag = 0
|
||||||
and authorize_code = #{authorizeCode}
|
and authorize_code = #{authorizeCode}
|
||||||
and serial_number is null
|
and product_id = #{productId}
|
||||||
and device_id is null
|
|
||||||
limit 1
|
limit 1
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user