Commit 77292d07 authored by zengtianlai3's avatar zengtianlai3

Merge branch 'm33' into 'master'

添加设备黑名单功能

See merge request !30
parents e777f499 d712f93f
...@@ -2,9 +2,12 @@ package iot.sixiang.license.auth; ...@@ -2,9 +2,12 @@ package iot.sixiang.license.auth;
import iot.sixiang.license.device.DeviceManager; import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.entity.Apply; import iot.sixiang.license.entity.Apply;
import iot.sixiang.license.entity.DeviceBlack;
import iot.sixiang.license.model.PageInfoModel; import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.vo.AppVo; import iot.sixiang.license.model.vo.AppVo;
import iot.sixiang.license.model.vo.DeviceVo;
import iot.sixiang.license.service.ApplyService; import iot.sixiang.license.service.ApplyService;
import iot.sixiang.license.service.DeviceBlackService;
import iot.sixiang.license.util.HmacUtil; import iot.sixiang.license.util.HmacUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -22,16 +25,22 @@ public class AuthManager { ...@@ -22,16 +25,22 @@ public class AuthManager {
private ApplyService applyService; private ApplyService applyService;
@Autowired @Autowired
private DeviceManager deviceManager; private DeviceManager deviceManager;
@Autowired
private DeviceBlackService deviceBlackService;
private Map<String, Apply> allApply = null; private Map<String, Apply> allApply = null;
private Map<String, DeviceBlack> deviceBlackMap = null;
public AuthManager() { public AuthManager() {
allApply = new HashMap<>(); allApply = new HashMap<>();
deviceBlackMap = new HashMap<>();
} }
@PostConstruct @PostConstruct
public void init() { public void init() {
initApps(); initApps();
initDeviceBlacks();
} }
public void initApps() { public void initApps() {
...@@ -44,6 +53,15 @@ public class AuthManager { ...@@ -44,6 +53,15 @@ public class AuthManager {
} }
} }
public void initDeviceBlacks() {
deviceBlackMap = new HashMap<>();
PageInfoModel<DeviceBlack> records = deviceBlackService.getDeviceBlackList(1, 10000);
List<DeviceBlack> deviceBlackList = records.getResult();
for (DeviceBlack deviceBlack : deviceBlackList) {
String deviceId = deviceBlack.getDeviceId().toString();
deviceBlackMap.put(deviceId, deviceBlack);
}
}
public boolean auth(String appId, String sn, String sign) { public boolean auth(String appId, String sn, String sign) {
...@@ -54,7 +72,11 @@ public class AuthManager { ...@@ -54,7 +72,11 @@ public class AuthManager {
if (!deviceManager.getContainSn(sn)) { if (!deviceManager.getContainSn(sn)) {
return false; return false;
} }
DeviceVo device = deviceManager.getDevice(sn);
int deviceId = device.getDeviceId();
if(deviceBlackMap.containsKey(deviceId)){
return false;
}
Apply apply = allApply.get(appId); Apply apply = allApply.get(appId);
String appKey = apply.getAppKey(); String appKey = apply.getAppKey();
String input = "app_id=" + appId + "&sn=" + sn; String input = "app_id=" + appId + "&sn=" + sn;
...@@ -65,4 +87,9 @@ public class AuthManager { ...@@ -65,4 +87,9 @@ public class AuthManager {
return false; return false;
} }
} }
public synchronized Map<String, DeviceBlack> getDeviceBlack() {
return deviceBlackMap;
}
} }
...@@ -7,6 +7,7 @@ import com.github.xiaoymin.knife4j.annotations.DynamicParameter; ...@@ -7,6 +7,7 @@ import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters; import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import iot.sixiang.license.device.DeviceManager; import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.entity.DeviceBlack;
import iot.sixiang.license.log.BusinessType; import iot.sixiang.license.log.BusinessType;
import iot.sixiang.license.log.MyLog; import iot.sixiang.license.log.MyLog;
import iot.sixiang.license.model.BaseResult; import iot.sixiang.license.model.BaseResult;
...@@ -14,6 +15,7 @@ import iot.sixiang.license.model.PageInfoModel; ...@@ -14,6 +15,7 @@ import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.PageResult; import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.vo.DeviceDetailVo; import iot.sixiang.license.model.vo.DeviceDetailVo;
import iot.sixiang.license.model.vo.DeviceVo; import iot.sixiang.license.model.vo.DeviceVo;
import iot.sixiang.license.service.DeviceBlackService;
import iot.sixiang.license.service.DeviceService; import iot.sixiang.license.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -38,6 +40,9 @@ public class DeviceController { ...@@ -38,6 +40,9 @@ public class DeviceController {
@Autowired @Autowired
private DeviceManager deviceManager; private DeviceManager deviceManager;
@Autowired
private DeviceBlackService deviceBlackService;
/** /**
* 添加device * 添加device
* @param jsonObject * @param jsonObject
...@@ -117,5 +122,72 @@ public class DeviceController { ...@@ -117,5 +122,72 @@ public class DeviceController {
List<DeviceDetailVo> pageResult = records.getResult(); List<DeviceDetailVo> pageResult = records.getResult();
return new PageResult(200, "查找成功", pageNo, pages, total, pageResult); return new PageResult(200, "查找成功", pageNo, pages, total, pageResult);
} }
/**
* 添加device_black
* @param jsonObject
* @return
*/
@ApiOperation(value = "添加设备黑名单接口", notes = "用于添加设备黑名单")
@PostMapping("device_black/add")
@ApiOperationSupport(params = @DynamicParameters(name = "jsonObject",properties = {
@DynamicParameter(name = "deviceId",value = "设备Id",required = true,dataTypeClass = Integer.class)
}))
@MyLog(title = "添加设备黑名单", optParam = "#{jsonObject}", businessType = BusinessType.INSERT)
public BaseResult addDeviceBlack(@RequestBody JSONObject jsonObject) {
int deviceId = jsonObject.getIntValue("deviceId");
boolean res = deviceBlackService.addDeviceBlack(deviceId);
if (res) {
return BaseResult.success();
} else {
return BaseResult.failed();
}
}
/**
* 删除device_black
* @param jsonObject
* @return
*/
@ApiOperation(value = "设备黑名单删除接口", notes = "删除设备黑名单")
@PostMapping("device_black/delete")
@ApiOperationSupport(params = @DynamicParameters(name = "jsonObject",properties = {
@DynamicParameter(name = "deviceId",value = "设备Id",required = true,dataTypeClass = Integer.class)
}))
@MyLog(title = "删除设备黑名单", optParam = "#{deviceId}", businessType = BusinessType.DELETE)
public BaseResult deleteDeviceBlack(@RequestBody JSONObject jsonObject) {
int deviceId = jsonObject.getIntValue("deviceId");
boolean res = deviceBlackService.deleteDeviceBlack(deviceId);
if (res) {
return BaseResult.success();
} else {
return BaseResult.failed();
}
}
/**
* 分页查询所有黑名单设备
* @param pageNo
* @param pageSize
* @return
*/
@ApiOperation(value = "获取设备黑名单列表接口", notes = "用于获取设备黑名单列表")
@GetMapping("device_black/list")
@MyLog(title = "获取设备黑名单列表", optParam = "#{pageNo},#{pageSize}", businessType = BusinessType.SELECT)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNo",value = "当前在第几页", required = true, dataType="int"),
@ApiImplicitParam(name = "pageSize",value = "每页显示多少页", required = true, dataType="int")
})
public PageResult<DeviceBlack> getDeviceBlackList(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo, @RequestParam(value = "pageSize", defaultValue = "0") int pageSize){
PageInfoModel<DeviceBlack> records = deviceBlackService.getDeviceBlackList(pageNo,pageSize);
int total = records.getTotal();
int pages = total/pageSize;//pages为总页数
int mod = total%pageSize;
if(mod!=0){
pages = pages +1;
}
List<DeviceBlack> pageResult = records.getResult();
return new PageResult(200,"查找成功",pageNo,pages,total,pageResult);
}
} }
...@@ -178,4 +178,8 @@ public class DeviceManager { ...@@ -178,4 +178,8 @@ public class DeviceManager {
return detailVoPageInfoModel; return detailVoPageInfoModel;
} }
public DeviceVo getDevice(String sn) {
DeviceVo deviceVo = allDevice.get(sn);
return deviceVo;
}
} }
package iot.sixiang.license.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author m33
* @since 2022-06-15
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class DeviceBlack implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private Integer deviceId;
}
package iot.sixiang.license.mapper;
import iot.sixiang.license.entity.DeviceBlack;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/**
* <p>
* Mapper 接口
* </p>
*
* @author m33
* @since 2022-06-15
*/
public interface DeviceBlackMapper extends BaseMapper<DeviceBlack> {
boolean addDeviceBlack(int deviceId);
boolean deleteDeviceBlack(int deviceId);
List<DeviceBlack> getDeviceBlackList();
}
...@@ -25,4 +25,7 @@ public class DeviceVo implements Serializable { ...@@ -25,4 +25,7 @@ public class DeviceVo implements Serializable {
@ApiModelProperty("设备编号") @ApiModelProperty("设备编号")
private String sn; private String sn;
@ApiModelProperty("设备状态 0:正常 1:禁用")
private int blackFlag;
} }
package iot.sixiang.license.service;
import iot.sixiang.license.entity.DeviceBlack;
import com.baomidou.mybatisplus.extension.service.IService;
import iot.sixiang.license.model.PageInfoModel;
/**
* <p>
* 服务类
* </p>
*
* @author m33
* @since 2022-06-15
*/
public interface DeviceBlackService extends IService<DeviceBlack> {
boolean addDeviceBlack(int deviceId);
boolean deleteDeviceBlack(int deviceId);
PageInfoModel<DeviceBlack> getDeviceBlackList(int pageNo, int pageSize);
}
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.DeviceBlack;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.DeviceBlackMapper;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.service.DeviceBlackService;
import iot.sixiang.license.util.SpringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>
* 服务实现类
* </p>
*
* @author m33
* @since 2022-06-15
*/
@Service
public class DeviceBlackServiceImpl extends ServiceImpl<DeviceBlackMapper, DeviceBlack> implements DeviceBlackService {
@Resource
private DeviceBlackMapper deviceBlackMapper;
@Override
public boolean addDeviceBlack(int deviceId) {
if (deviceId == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
AuthManager authManager = SpringUtil.getBean(AuthManager.class);
if (authManager.getDeviceBlack().containsKey(deviceId)) {
throw new IotLicenseException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMsg());
}
boolean res = deviceBlackMapper.addDeviceBlack(deviceId);
if (res) {
authManager.initDeviceBlacks();
return true;
}
return false;
}
@Override
public boolean deleteDeviceBlack(int deviceId) {
boolean res = deviceBlackMapper.deleteDeviceBlack(deviceId);
if (res) {
AuthManager authManager = SpringUtil.getBean(AuthManager.class);
authManager.initDeviceBlacks();
return true;
}
return false;
}
@Override
public PageInfoModel<DeviceBlack> getDeviceBlackList(int pageNo, int pageSize) {
if(pageNo == 0 || pageSize == 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMsg());
}
List<DeviceBlack> records = deviceBlackMapper.getDeviceBlackList();
records = records.stream().sorted(Comparator.comparing(DeviceBlack::getId)).collect(Collectors.toList());
List<DeviceBlack> result = new ArrayList<>();
int begin = (pageNo - 1) * pageSize;
if (begin >= 0 && records.size() > 0) {
result = records.stream().skip(begin).limit(pageSize).collect(Collectors.toList());
}
PageInfoModel<DeviceBlack> objectPageInfoModel = new PageInfoModel<>();
objectPageInfoModel.setTotal(records.size());
objectPageInfoModel.setResult(result);
return objectPageInfoModel;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="iot.sixiang.license.mapper.DeviceBlackMapper">
<insert id="addDeviceBlack" parameterType="iot.sixiang.license.entity.DeviceBlack">
insert into device_black(device_id) values (#{deviceId})
</insert>
<delete id="deleteDeviceBlack" parameterType="iot.sixiang.license.entity.DeviceBlack">
delete from device_black where device_id = #{deviceId}
</delete>
<select id="getDeviceBlackList" resultType="iot.sixiang.license.entity.DeviceBlack">
select id, device_id from device_black
</select>
</mapper>
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<mapper namespace="iot.sixiang.license.mapper.DeviceMapper"> <mapper namespace="iot.sixiang.license.mapper.DeviceMapper">
<select id="getDeviceList" resultType="iot.sixiang.license.model.vo.DeviceVo"> <select id="getDeviceList" resultType="iot.sixiang.license.model.vo.DeviceVo">
SELECT de.device_id,app_name,user_name,sn FROM device AS de SELECT de.device_id,app_name,user_name,sn,de.device_id IN (select device_id from device_black) AS blackFlag FROM device AS de
JOIN apply AS app ON de.app_id = app.app_id JOIN apply AS app ON de.app_id = app.app_id
JOIN USER AS us ON us.user_id = app.user_id JOIN USER AS us ON us.user_id = app.user_id
where 1=1 where 1=1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment