diff --git a/dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java b/dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java index dd3aabd..f3c6594 100644 --- a/dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java +++ b/dk-api/api-system/src/main/java/org/dromara/system/api/model/LoginUser.java @@ -135,6 +135,12 @@ public class LoginUser implements Serializable { */ private List posts; + + /** + * 设备sn权限集合 + */ + private List deviceList; + /** * 获取登录id */ diff --git a/dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java b/dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java index 82b1bc5..42b4140 100644 --- a/dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java +++ b/dk-auth/src/main/java/org/dromara/auth/DKAuthApplication.java @@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; +import org.springframework.cloud.openfeign.EnableFeignClients; /** * 认证授权中心 @@ -12,6 +13,7 @@ import org.springframework.boot.context.metrics.buffering.BufferingApplicationSt * @author ruoyi */ @EnableDubbo +@EnableFeignClients @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class DKAuthApplication { public static void main(String[] args) { diff --git a/dk-auth/src/main/java/org/dromara/auth/feign/FeignDeviceGroup.java b/dk-auth/src/main/java/org/dromara/auth/feign/FeignDeviceGroup.java new file mode 100644 index 0000000..1acfc28 --- /dev/null +++ b/dk-auth/src/main/java/org/dromara/auth/feign/FeignDeviceGroup.java @@ -0,0 +1,24 @@ +package org.dromara.auth.feign; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; +import java.util.Map; + +@FeignClient(name = "gateway",path = "sample") +public interface FeignDeviceGroup { + + + @GetMapping("/manage/api/v1/device/group/feign/user") + public List listDeviceGroup(@RequestParam("userId") Long userId); + + + @GetMapping("/manage/api/v1/device/group/feign/device") + public List listDevice(@RequestParam("userId") Long userId); + + + @GetMapping("/device/flight/count") + public Map getDevices(); +} diff --git a/dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java b/dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java index 0071ea4..3b7efe8 100644 --- a/dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java +++ b/dk-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java @@ -3,10 +3,12 @@ package org.dromara.auth.service.impl; import cn.dev33.satoken.secure.BCrypt; import cn.dev33.satoken.stp.SaLoginModel; import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.collection.ListUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.dromara.auth.domain.vo.LoginVo; +import org.dromara.auth.feign.FeignDeviceGroup; import org.dromara.auth.form.PasswordLoginBody; import org.dromara.auth.properties.CaptchaProperties; import org.dromara.auth.service.IAuthStrategy; @@ -26,8 +28,12 @@ import org.dromara.common.tenant.helper.TenantHelper; import org.dromara.system.api.RemoteUserService; import org.dromara.system.api.domain.vo.RemoteClientVo; import org.dromara.system.api.model.LoginUser; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** * 密码认证策略 * @@ -45,6 +51,9 @@ public class PasswordAuthStrategy implements IAuthStrategy { @DubboReference private RemoteUserService remoteUserService; + @Autowired + FeignDeviceGroup feignDeviceGroup; + @Override public LoginVo login(String body, RemoteClientVo client) { PasswordLoginBody loginBody = JsonUtils.parseObject(body, PasswordLoginBody.class); @@ -73,6 +82,19 @@ public class PasswordAuthStrategy implements IAuthStrategy { model.setTimeout(client.getTimeout()); model.setActiveTimeout(client.getActiveTimeout()); model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId()); + + //获取设备权限 + List deviceList = new ArrayList<>(); + + try { + deviceList = feignDeviceGroup.listDevice(loginUser.getUserId()); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + + + loginUser.setDeviceList(deviceList); + // 生成token LoginHelper.login(loginUser, model); diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java b/dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java index b58bc1e..1a0918b 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/manage/service/impl/DeviceProServiceImpl.java @@ -88,7 +88,7 @@ public class DeviceProServiceImpl implements IDeviceProService { @Autowired private IDeviceProUserMapper deviceProUserMapper; - private final ObjectMapper objectMapper = new ObjectMapper(); +// private final ObjectMapper objectMapper = new ObjectMapper(); @Override public PaginationData listDeviceProEntityMap(Page page, DeviceProDTO deviceProDTO) { @@ -131,52 +131,52 @@ public class DeviceProServiceImpl implements IDeviceProService { public Boolean updateDeviceProUser(List userEntity,Integer proId) { try { //获取项目组信息 - DeviceProEntity deviceProEntity = deviceProMapper.selectOne(new LambdaQueryWrapper().eq(DeviceProEntity::getId, proId)); +// DeviceProEntity deviceProEntity = deviceProMapper.selectOne(new LambdaQueryWrapper().eq(DeviceProEntity::getId, proId)); //查询之前项目所属的人员信息 - List proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper().eq(DeviceProUserEntity::getDeviceProId, proId)); - - List difference = new ArrayList<>(); +// List proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper().eq(DeviceProUserEntity::getDeviceProId, proId)); +// +// List difference = new ArrayList<>(); // 判断两个集合的差值(基于 userId) - difference = proUserEntityList.stream() - .filter(proUser -> userEntity.stream() - .noneMatch(user -> user.getUserId().equals(proUser.getUserId()))) - .toList(); +// difference = proUserEntityList.stream() +// .filter(proUser -> userEntity.stream() +// .noneMatch(user -> user.getUserId().equals(proUser.getUserId()))) +// .toList(); //--------------------------------------------缓存相关操作---------------------------------------------------------------- //人员如果解绑了项目删除缓存数据 - difference.forEach(proUserEntity -> { - //删除人员项目组信息 - List projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); - List projectList = projects.stream() - .filter(Map.class::isInstance) - .map(Map.class::cast) - .toList(); - - //删除人员中所拥有的当前的项目组,然后构建新的存入redis - List deviceProEntities = projectList.stream().filter(p -> !Integer.valueOf(p.get("id") + "").equals(proId)).toList(); - //删除 - RedisUtils.deleteObject(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); - //重新插入 - RedisUtils.setCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()),deviceProEntities); - - - //删除人员所拥有的设备信息 - List cacheList = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId())); - List deviceList = cacheList.stream() - .filter(Map.class::isInstance) - .map(Map.class::cast) - .toList(); - - //过滤除了这个删除的项目组之外的设备 - deviceList = deviceList.stream().filter(p-> !Integer.valueOf(p.get("proId") + "").equals(proId)).toList(); - //删除 - RedisUtils.deleteObject(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId())); - - RedisUtils.setCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()),deviceList); - }); +// difference.forEach(proUserEntity -> { +// //删除人员项目组信息 +// List projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); +// List projectList = projects.stream() +// .filter(Map.class::isInstance) +// .map(Map.class::cast) +// .toList(); +// +// //删除人员中所拥有的当前的项目组,然后构建新的存入redis +// List deviceProEntities = projectList.stream().filter(p -> !Integer.valueOf(p.get("id") + "").equals(proId)).toList(); +// //删除 +// RedisUtils.deleteObject(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); +// //重新插入 +// RedisUtils.setCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()),deviceProEntities); +// +// +// //删除人员所拥有的设备信息 +// List cacheList = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId())); +// List deviceList = cacheList.stream() +// .filter(Map.class::isInstance) +// .map(Map.class::cast) +// .toList(); +// +// //过滤除了这个删除的项目组之外的设备 +// deviceList = deviceList.stream().filter(p-> !Integer.valueOf(p.get("proId") + "").equals(proId)).toList(); +// //删除 +// RedisUtils.deleteObject(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId())); +// +// RedisUtils.setCacheList(String.format(Constants.PRO_DEVICE, proUserEntity.getUserId()),deviceList); +// }); //--------------------------------------------缓存相关操作---------------------------------------------------------------- @@ -188,18 +188,18 @@ public class DeviceProServiceImpl implements IDeviceProService { deviceProUserMapper.insert(userEntity); //更新缓存记录 - userEntity.forEach(proUserEntity -> { - List projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); - List projectList = projects.stream() - .filter(Map.class::isInstance) - .map(Map.class::cast) - .toList(); - - if (!projectList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(proUserEntity.getDeviceProId()))){ - Map proMap = objectMapper.convertValue(deviceProEntity, Map.class); - RedisUtils.addCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()), proMap); - } - }); +// userEntity.forEach(proUserEntity -> { +// List projects = RedisUtils.getCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId())); +// List projectList = projects.stream() +// .filter(Map.class::isInstance) +// .map(Map.class::cast) +// .toList(); +// +// if (!projectList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(proUserEntity.getDeviceProId()))){ +// Map proMap = objectMapper.convertValue(deviceProEntity, Map.class); +// RedisUtils.addCacheList(String.format(Constants.PRO_GROUP, proUserEntity.getUserId()), proMap); +// } +// }); } return true; @@ -229,29 +229,31 @@ public class DeviceProServiceImpl implements IDeviceProService { wrapper.set(DeviceEntity::getProId, proId); wrapper.eq(DeviceEntity::getId, deviceId); - if (deviceMapper.update(wrapper) > 0) { - DeviceEntity deviceEntity = deviceMapper.selectOne(new LambdaQueryWrapper().eq(DeviceEntity::getId, deviceId)); - //根据proid获取人员用于存储人员的设备权限 - List proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper().eq(DeviceProUserEntity::getDeviceProId, proId)); - proUserEntityList.forEach(deviceProUserEntity -> { - List devices = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId())); - List deviceList = devices.stream() - .filter(Map.class::isInstance) - .map(Map.class::cast) - .toList(); - - if (!deviceList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(deviceEntity.getId()))){ - Map deviceMap = objectMapper.convertValue(deviceEntity, Map.class); - - RedisUtils.addCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()), deviceMap); - } - }); - } + + return deviceMapper.update(wrapper) > 0; + + +// if (deviceMapper.update(wrapper) > 0) { +// DeviceEntity deviceEntity = deviceMapper.selectOne(new LambdaQueryWrapper().eq(DeviceEntity::getId, deviceId)); +// //根据proid获取人员用于存储人员的设备权限 +// List proUserEntityList = deviceProUserMapper.selectList(new LambdaQueryWrapper().eq(DeviceProUserEntity::getDeviceProId, proId)); +// proUserEntityList.forEach(deviceProUserEntity -> { +// List devices = RedisUtils.getCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId())); +// List deviceList = devices.stream() +// .filter(Map.class::isInstance) +// .map(Map.class::cast) +// .toList(); +// +// if (!deviceList.stream().anyMatch(p-> Integer.valueOf(p.get("id") + "").equals(deviceEntity.getId()))){ +// Map deviceMap = objectMapper.convertValue(deviceEntity, Map.class); +// +// RedisUtils.addCacheList(String.format(Constants.PRO_DEVICE, deviceProUserEntity.getUserId()), deviceMap); +// } +// }); +// } } catch (Exception e) { return false; } - - return true; } /**