From abf384dd3ce4753ee1c90c98624bab211a843811 Mon Sep 17 00:00:00 2001 From: lijiahang Date: Thu, 23 Jan 2025 10:13:54 +0800 Subject: [PATCH] =?UTF-8?q?:hammer:=20=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=80=BB=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SystemSettingController.java | 10 +- .../define/cache/SystemSettingKeyDefine.java | 7 +- .../operator/SystemSettingOperatorType.java | 6 +- .../infra/entity/domain/SystemSettingDO.java | 10 +- .../system/SystemSettingUpdateRequest.java | 4 +- .../vo/RsaKeyPairVO.java} | 18 +- .../entity/vo/SystemSettingAggregateVO.java | 53 ----- .../infra/enums/SystemSettingTypeEnum.java | 66 ++----- .../infra/service/SystemSettingService.java | 18 +- .../impl/SystemSettingServiceImpl.java | 185 ++++-------------- .../module/infra/utils/SystemUuidUtils.java | 114 +++++++++++ .../resources/mapper/SystemSettingMapper.xml | 5 +- 12 files changed, 207 insertions(+), 289 deletions(-) rename orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/{handler/setting/model/EncryptSystemSettingModel.java => entity/vo/RsaKeyPairVO.java} (76%) delete mode 100644 orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/SystemSettingAggregateVO.java create mode 100644 orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/utils/SystemUuidUtils.java diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/SystemSettingController.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/SystemSettingController.java index f8d0a5b4..ebb86427 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/SystemSettingController.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/controller/SystemSettingController.java @@ -35,8 +35,7 @@ import org.dromara.visor.module.infra.define.operator.SystemSettingOperatorType; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest; import org.dromara.visor.module.infra.entity.vo.AppInfoVO; -import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; -import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; +import org.dromara.visor.module.infra.entity.vo.RsaKeyPairVO; import org.dromara.visor.module.infra.service.SystemSettingService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; @@ -44,6 +43,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.annotation.security.PermitAll; +import java.util.Map; /** * 系统设置服务 @@ -74,7 +74,7 @@ public class SystemSettingController { @IgnoreLog(IgnoreLogMode.RET) @GetMapping("/setting") @Operation(summary = "获取系统聚合设置") - public SystemSettingAggregateVO getSystemAggregateSetting() { + public Map getSystemAggregateSetting() { return systemSettingService.getSystemAggregateSetting(); } @@ -83,7 +83,7 @@ public class SystemSettingController { @GetMapping("/generator-keypair") @Operation(summary = "生成密钥对") @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") - public EncryptSystemSettingModel generatorKeypair() { + public RsaKeyPairVO generatorKeypair() { return systemSettingService.generatorKeypair(); } @@ -92,7 +92,7 @@ public class SystemSettingController { @Operation(summary = "查询系统设置") @Parameter(name = "type", description = "type", required = true) @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") - public Object getSystemSettingByType(@RequestParam("type") String type) { + public Map getSystemSettingByType(@RequestParam("type") String type) { return systemSettingService.getSystemSettingByType(type); } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/SystemSettingKeyDefine.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/SystemSettingKeyDefine.java index f2caa62b..fafaa315 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/SystemSettingKeyDefine.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/cache/SystemSettingKeyDefine.java @@ -25,7 +25,6 @@ package org.dromara.visor.module.infra.define.cache; import cn.orionsec.kit.lang.define.cache.key.CacheKeyBuilder; import cn.orionsec.kit.lang.define.cache.key.CacheKeyDefine; import cn.orionsec.kit.lang.define.cache.key.struct.RedisCacheStruct; -import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; import java.util.concurrent.TimeUnit; @@ -39,10 +38,10 @@ import java.util.concurrent.TimeUnit; public interface SystemSettingKeyDefine { CacheKeyDefine SETTING = new CacheKeyBuilder() - .key("system:setting:agg") + .key("system:setting:view") .desc("系统聚合设置") - .type(SystemSettingAggregateVO.class) - .struct(RedisCacheStruct.STRING) + .type(String.class) + .struct(RedisCacheStruct.HASH) .timeout(8, TimeUnit.HOURS) .build(); diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/operator/SystemSettingOperatorType.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/operator/SystemSettingOperatorType.java index a6cf3da3..611054f8 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/operator/SystemSettingOperatorType.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/define/operator/SystemSettingOperatorType.java @@ -38,16 +38,12 @@ import static org.dromara.visor.framework.biz.operator.log.core.enums.OperatorRi @Module("infra:system-setting") public class SystemSettingOperatorType extends InitializingOperatorTypes { - public static final String UPDATE_TEXT = "{} - {} - {}"; - - public static final String UPDATE_BATCH_TEXT = "{}"; - public static final String UPDATE = "system-setting:update"; @Override public OperatorType[] types() { return new OperatorType[]{ - new OperatorType(M, UPDATE, "更新系统设置 ${text}"), + new OperatorType(M, UPDATE, "更新系统设置 ${type}"), }; } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/domain/SystemSettingDO.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/domain/SystemSettingDO.java index aef3cf84..ff69a4e7 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/domain/SystemSettingDO.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/domain/SystemSettingDO.java @@ -52,17 +52,13 @@ public class SystemSettingDO extends BaseDO { @TableId(value = "id", type = IdType.AUTO) private Long id; - @Schema(description = "配置key") - @TableField("config_key") - private String configKey; - @Schema(description = "配置类型") @TableField("type") private String type; - @Schema(description = "配置项") - @TableField("item") - private String item; + @Schema(description = "配置key") + @TableField("config_key") + private String configKey; @Schema(description = "配置值") @TableField("value") diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdateRequest.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdateRequest.java index e6f90bd2..c83781a8 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdateRequest.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdateRequest.java @@ -54,9 +54,9 @@ public class SystemSettingUpdateRequest implements Serializable { private String type; @NotBlank - @Size(max = 32) + @Size(max = 64) @Schema(description = "配置项") - private String item; + private String configKey; @Schema(description = "配置值") private String value; diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/model/EncryptSystemSettingModel.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/RsaKeyPairVO.java similarity index 76% rename from orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/model/EncryptSystemSettingModel.java rename to orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/RsaKeyPairVO.java index 0c4a7468..c30a88ad 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/model/EncryptSystemSettingModel.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/RsaKeyPairVO.java @@ -20,34 +20,32 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dromara.visor.module.infra.handler.setting.model; +package org.dromara.visor.module.infra.entity.vo; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** - * 加密系统设置模型 + * RSA 密钥对响应对象 * * @author Jiahang Li * @version 1.0.0 - * @since 2024/10/9 11:45 + * @since 2025/1/3 17:46 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor -public class EncryptSystemSettingModel { +@Schema(name = "RsaKeyPairVO", description = "RSA 密钥对响应对象") +public class RsaKeyPairVO { - /** - * 加密公钥 - */ + @Schema(description = "公钥") private String publicKey; - /** - * 加密私钥 - */ + @Schema(description = "私钥") private String privateKey; } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/SystemSettingAggregateVO.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/SystemSettingAggregateVO.java deleted file mode 100644 index ed810cc3..00000000 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/vo/SystemSettingAggregateVO.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2023 - present Dromara, All rights reserved. - * - * https://visor.dromara.org - * https://visor.dromara.org.cn - * https://visor.orionsec.cn - * - * Members: - * Jiahang Li - ljh1553488six@139.com - author - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.dromara.visor.module.infra.entity.vo; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; -import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel; - -/** - * 系统设置 聚合响应对象 - * - * @author Jiahang Li - * @version 1.0.0 - * @since 2025/1/3 17:46 - */ -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -@Schema(name = "SystemSettingAggregateVO", description = "系统设置 聚合响应对象") -public class SystemSettingAggregateVO { - - @Schema(description = "SFTP 设置") - private SftpSystemSettingModel sftp; - - @Schema(description = "加密设置") - private EncryptSystemSettingModel encrypt; - -} diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java index 97b7615e..6fa453f8 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/enums/SystemSettingTypeEnum.java @@ -22,14 +22,8 @@ */ package org.dromara.visor.module.infra.enums; -import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Getter; -import org.dromara.visor.common.constant.Const; -import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; -import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel; - -import java.util.Map; /** * 系统设置类型枚举 @@ -43,52 +37,32 @@ import java.util.Map; public enum SystemSettingTypeEnum { /** - * SFTP 配置 + * SFTP 设置 */ - SFTP("sftp", SftpSystemSettingModel.class), + SFTP("sftp"), /** - * 加密配置 + * 加密设置 */ - ENCRYPT("encrypt", EncryptSystemSettingModel.class), + ENCRYPT("encrypt"), + + /** + * 登录设置 + */ + LOGIN("login"), + + /** + * 日志设置 + */ + LOG("log"), + + /** + * 自动清理设置 + */ + AUTO_CLEAR("autoClear"), ; - private final String prefix; - - private final Class modelClass; - - /** - * 解析 - * - * @param settings settings - * @return model - */ - @SuppressWarnings("unchecked") - public T parseModel(Map settings) { - return (T) JSON.parseObject(JSON.toJSONString(settings)).toJavaObject(modelClass); - } - - public static SystemSettingTypeEnum of(String type) { - if (type == null) { - return null; - } - for (SystemSettingTypeEnum value : values()) { - if (value.name().equals(type)) { - return value; - } - } - return null; - } - - /** - * 获取 key - * - * @param item item - * @return key - */ - public String getConfigKey(String item) { - return prefix + Const.DOT + item; - } + private final String type; } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemSettingService.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemSettingService.java index 62fa03e7..d5a59e56 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemSettingService.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/SystemSettingService.java @@ -25,8 +25,9 @@ package org.dromara.visor.module.infra.service; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest; import org.dromara.visor.module.infra.entity.vo.AppInfoVO; -import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; -import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; +import org.dromara.visor.module.infra.entity.vo.RsaKeyPairVO; + +import java.util.Map; /** * 系统设置服务 @@ -49,33 +50,32 @@ public interface SystemSettingService { * * @return setting */ - SystemSettingAggregateVO getSystemAggregateSetting(); + Map getSystemAggregateSetting(); /** * 生成密钥对 * * @return keypair */ - EncryptSystemSettingModel generatorKeypair(); + RsaKeyPairVO generatorKeypair(); /** * 通过类型查询系统设置 * * @param type type - * @param T - * @return row + * @return rows */ - T getSystemSettingByType(String type); + Map getSystemSettingByType(String type); /** - * 更新部分系统设置 + * 更新系统设置-单个 * * @param request request */ void updateSystemSetting(SystemSettingUpdateRequest request); /** - * 更新系统设置 + * 更新系统设置-多个 * * @param request request */ diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java index 7995e2ee..0174102f 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/service/impl/SystemSettingServiceImpl.java @@ -22,43 +22,32 @@ */ package org.dromara.visor.module.infra.service.impl; -import cn.orionsec.kit.ext.process.ProcessAwaitExecutor; import cn.orionsec.kit.lang.define.wrapper.Pair; import cn.orionsec.kit.lang.function.Functions; -import cn.orionsec.kit.lang.support.Attempt; -import cn.orionsec.kit.lang.utils.Arrays1; -import cn.orionsec.kit.lang.utils.Objects1; import cn.orionsec.kit.lang.utils.Strings; +import cn.orionsec.kit.lang.utils.collect.Maps; import cn.orionsec.kit.lang.utils.crypto.Keys; import cn.orionsec.kit.lang.utils.crypto.RSA; -import cn.orionsec.kit.lang.utils.crypto.Signatures; -import cn.orionsec.kit.lang.utils.io.Streams; import cn.orionsec.kit.spring.SpringHolder; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.dromara.visor.common.constant.AppConst; -import org.dromara.visor.common.constant.Const; -import org.dromara.visor.common.constant.ErrorMessage; -import org.dromara.visor.common.utils.Valid; -import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs; +import org.dromara.visor.common.constant.ConfigKeys; import org.dromara.visor.framework.config.core.event.ConfigUpdateEvent; -import org.dromara.visor.framework.redis.core.utils.RedisStrings; +import org.dromara.visor.framework.mybatis.core.query.Conditions; +import org.dromara.visor.framework.redis.core.utils.RedisMaps; import org.dromara.visor.framework.redis.core.utils.RedisUtils; import org.dromara.visor.module.infra.dao.SystemSettingDAO; import org.dromara.visor.module.infra.define.cache.SystemSettingKeyDefine; -import org.dromara.visor.module.infra.define.operator.SystemSettingOperatorType; import org.dromara.visor.module.infra.entity.domain.SystemSettingDO; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateBatchRequest; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest; import org.dromara.visor.module.infra.entity.vo.AppInfoVO; -import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; -import org.dromara.visor.module.infra.enums.SystemSettingTypeEnum; -import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; -import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingModel; +import org.dromara.visor.module.infra.entity.vo.RsaKeyPairVO; import org.dromara.visor.module.infra.service.SystemSettingService; +import org.dromara.visor.module.infra.utils.SystemUuidUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.io.ByteArrayOutputStream; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.List; @@ -75,8 +64,6 @@ import java.util.stream.Collectors; @Service public class SystemSettingServiceImpl implements SystemSettingService { - private String uuid; - @Resource private SystemSettingDAO systemSettingDAO; @@ -84,129 +71,102 @@ public class SystemSettingServiceImpl implements SystemSettingService { public AppInfoVO getAppInfo() { return AppInfoVO.builder() .version(AppConst.VERSION) - .uuid(this.getSystemUuid()) + .uuid(SystemUuidUtils.getSystemUuid()) .build(); } @Override - public SystemSettingAggregateVO getSystemAggregateSetting() { + public Map getSystemAggregateSetting() { // 查询缓存 - SystemSettingAggregateVO cache = RedisStrings.getJson(SystemSettingKeyDefine.SETTING); - SystemSettingAggregateVO result = Objects1.def(cache, SystemSettingAggregateVO::new); - if (cache == null) { + Map cache = RedisMaps.entities(SystemSettingKeyDefine.SETTING); + if (Maps.isEmpty(cache)) { // 查询数据库 - Map> typeGroup = systemSettingDAO.of() + cache = systemSettingDAO.of() .createWrapper() - .select(SystemSettingDO::getType, - SystemSettingDO::getItem, + .select(SystemSettingDO::getConfigKey, SystemSettingDO::getValue) - .in(SystemSettingDO::getType, - SystemSettingTypeEnum.SFTP.name(), - SystemSettingTypeEnum.ENCRYPT.name()) + .in(SystemSettingDO::getConfigKey, + ConfigKeys.ENCRYPT_PUBLIC_KEY, + ConfigKeys.LOG_WEB_SCROLL_LINES) .then() .stream() - .collect(Collectors.groupingBy(SystemSettingDO::getType)); - // 数据组合 - typeGroup.forEach((k, v) -> { - // 类型数据 - SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(k); - Map typeSettings = v.stream() - .collect(Collectors.toMap( - SystemSettingDO::getItem, - SystemSettingDO::getValue, - Functions.right())); - Object setting = settingType.parseModel(typeSettings); - if (SystemSettingTypeEnum.SFTP.equals(settingType)) { - // SFTP 设置 - result.setSftp((SftpSystemSettingModel) setting); - } else if (SystemSettingTypeEnum.ENCRYPT.equals(settingType)) { - // 加密设置 - EncryptSystemSettingModel encryptSetting = (EncryptSystemSettingModel) setting; - encryptSetting.setPrivateKey(null); - result.setEncrypt(encryptSetting); - } - }); + .collect(Collectors.toMap(SystemSettingDO::getConfigKey, + SystemSettingDO::getValue, + Functions.right())); // 设置缓存 - RedisStrings.setJson(SystemSettingKeyDefine.SETTING, result); + RedisMaps.putAll(SystemSettingKeyDefine.SETTING, cache); } - return result; + return cache; } @Override - public EncryptSystemSettingModel generatorKeypair() { + public RsaKeyPairVO generatorKeypair() { // 生成密钥对 Pair pair = RSA.generatorKeys(); - return EncryptSystemSettingModel.builder() + return RsaKeyPairVO.builder() .publicKey(Keys.getPublicKey(pair.getKey())) .privateKey(Keys.getPrivateKey(pair.getValue())) .build(); } @Override - public T getSystemSettingByType(String type) { - SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(type); - Valid.notNull(settingType, ErrorMessage.ERROR_TYPE); - // 查询数据库 - Map settings = systemSettingDAO.of() + public Map getSystemSettingByType(String type) { + return systemSettingDAO.of() .createWrapper() .eq(SystemSettingDO::getType, type) .then() .stream() .collect(Collectors.toMap( - SystemSettingDO::getItem, + SystemSettingDO::getConfigKey, SystemSettingDO::getValue, Functions.right())); - // 解析 - return settingType.parseModel(settings); } @Override public void updateSystemSetting(SystemSettingUpdateRequest request) { String type = request.getType(); - SystemSettingTypeEnum settingType = Valid.valid(SystemSettingTypeEnum::of, type); - String item = request.getItem(); + String configKey = request.getConfigKey(); String value = request.getValue(); + // 删除 + systemSettingDAO.delete(Conditions.eq(SystemSettingDO::getConfigKey, configKey)); + // 插入 + SystemSettingDO insert = SystemSettingDO.builder() + .type(type) + .configKey(configKey) + .value(Strings.def(value)) + .build(); + systemSettingDAO.insert(insert); // 更新 SystemSettingDO update = new SystemSettingDO(); update.setValue(value); LambdaQueryWrapper wrapper = systemSettingDAO.lambda() - .eq(SystemSettingDO::getType, type) - .eq(SystemSettingDO::getItem, item); + .eq(SystemSettingDO::getConfigKey, configKey); systemSettingDAO.update(update, wrapper); // 删除缓存 RedisUtils.delete(SystemSettingKeyDefine.SETTING); - // 设置日志参数 - OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_TEXT, type, item, value)); // 触发修改事件 - SpringHolder.publishEvent(ConfigUpdateEvent.of(settingType.getConfigKey(item), value)); + SpringHolder.publishEvent(ConfigUpdateEvent.of(configKey, value)); } @Override public void updateSystemSettingBatch(SystemSettingUpdateBatchRequest request) { String type = request.getType(); - SystemSettingTypeEnum settingType = Valid.valid(SystemSettingTypeEnum::of, type); Map settings = request.getSettings(); // 删除 - LambdaQueryWrapper deleteWrapper = systemSettingDAO.lambda() - .eq(SystemSettingDO::getType, type) - .in(SystemSettingDO::getItem, settings.keySet()); - systemSettingDAO.delete(deleteWrapper); + systemSettingDAO.delete(Conditions.in(SystemSettingDO::getConfigKey, settings.keySet())); // 插入 List rows = settings.entrySet() .stream() .map(s -> SystemSettingDO.builder() - .configKey(settingType.getConfigKey(s.getKey())) .type(type) - .item(s.getKey()) - .value(s.getValue()) + .configKey(s.getKey()) + .value(Strings.def(s.getValue())) .build()) .collect(Collectors.toList()); // 插入 systemSettingDAO.insertBatch(rows); // 删除缓存 RedisUtils.delete(SystemSettingKeyDefine.SETTING); - // 设置日志参数 - OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_BATCH_TEXT, type)); // 触发修改事件 Map eventConfig = rows.stream() .collect(Collectors.toMap( @@ -216,69 +176,4 @@ public class SystemSettingServiceImpl implements SystemSettingService { SpringHolder.publishEvent(ConfigUpdateEvent.of(eventConfig)); } - /** - * 获取系统 uuid - * - * @return uuid - */ - private String getSystemUuid() { - if (this.uuid != null) { - return this.uuid; - } - String[][] cmd = new String[][]{ - new String[]{"/bin/sh", "-c", "cat /sys/class/dmi/id/product_serial"}, - new String[]{"/bin/bash", "-c", "cat /sys/class/dmi/id/product_serial"}, - new String[]{"/bin/sh", "-c", "dmidecode -s system-uuid"}, - new String[]{"/bin/bash", "-c", "dmidecode -s system-uuid"}, - new String[]{"cmd", "/c", "wmic csproduct get uuid"} - }; - for (String[] s : cmd) { - try { - String uuid = this.getCommandOutput(s); - if (Strings.isBlank(uuid)) { - continue; - } - // 去除符号并且转为大写 - uuid = uuid.replaceAll(Const.DASHED, Const.EMPTY) - .toUpperCase() - .trim(); - // 去除 \n - String extraUuid = Arrays1.last(uuid.trim().split(Const.LF)); - if (!Strings.isBlank(extraUuid)) { - uuid = extraUuid.trim(); - } - // 去除 : - extraUuid = Arrays1.last(uuid.trim().split(Const.COLON)); - if (!Strings.isBlank(extraUuid)) { - uuid = extraUuid.trim(); - } - return this.uuid = Signatures.md5(uuid); - } catch (Exception e) { - // IGNORED - } - } - return this.uuid = Const.UNKNOWN; - } - - /** - * 获取输出结果 - * - * @param command command - * @return result - */ - private String getCommandOutput(String[] command) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ProcessAwaitExecutor executor = new ProcessAwaitExecutor(command); - try { - executor.streamHandler(i -> Attempt.uncheck(Streams::transfer, i, out)) - .waitFor() - .sync() - .exec(); - return out.toString(); - } finally { - Streams.close(out); - Streams.close(executor); - } - } - } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/utils/SystemUuidUtils.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/utils/SystemUuidUtils.java new file mode 100644 index 00000000..2876044f --- /dev/null +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/utils/SystemUuidUtils.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2023 - present Dromara, All rights reserved. + * + * https://visor.dromara.org + * https://visor.dromara.org.cn + * https://visor.orionsec.cn + * + * Members: + * Jiahang Li - ljh1553488six@139.com - author + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.dromara.visor.module.infra.utils; + +import cn.orionsec.kit.ext.process.ProcessAwaitExecutor; +import cn.orionsec.kit.lang.support.Attempt; +import cn.orionsec.kit.lang.utils.Arrays1; +import cn.orionsec.kit.lang.utils.Strings; +import cn.orionsec.kit.lang.utils.crypto.Signatures; +import cn.orionsec.kit.lang.utils.io.Streams; +import org.dromara.visor.common.constant.Const; + +import java.io.ByteArrayOutputStream; + +/** + * 系统 UUID 工具类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/1/16 11:07 + */ +public class SystemUuidUtils { + + private static String uuid; + + private SystemUuidUtils() { + } + + /** + * 获取系统 uuid + * + * @return uuid + */ + public static String getSystemUuid() { + if (SystemUuidUtils.uuid != null) { + return SystemUuidUtils.uuid; + } + String[][] cmd = new String[][]{ + new String[]{"/bin/sh", "-c", "cat /sys/class/dmi/id/product_serial"}, + new String[]{"/bin/bash", "-c", "cat /sys/class/dmi/id/product_serial"}, + new String[]{"/bin/sh", "-c", "dmidecode -s system-uuid"}, + new String[]{"/bin/bash", "-c", "dmidecode -s system-uuid"}, + new String[]{"cmd", "/c", "wmic csproduct get uuid"} + }; + for (String[] s : cmd) { + try { + String uuid = SystemUuidUtils.getCommandOutput(s); + if (Strings.isBlank(uuid)) { + continue; + } + // 去除符号并且转为大写 + uuid = uuid.replaceAll(Const.DASHED, Const.EMPTY) + .toUpperCase() + .trim(); + // 去除 \n + String extraUuid = Arrays1.last(uuid.trim().split(Const.LF)); + if (!Strings.isBlank(extraUuid)) { + uuid = extraUuid.trim(); + } + // 去除 : + extraUuid = Arrays1.last(uuid.trim().split(Const.COLON)); + if (!Strings.isBlank(extraUuid)) { + uuid = extraUuid.trim(); + } + return SystemUuidUtils.uuid = Signatures.md5(uuid); + } catch (Exception e) { + // IGNORED + } + } + return SystemUuidUtils.uuid = Const.UNKNOWN; + } + + /** + * 获取输出结果 + * + * @param command command + * @return result + */ + public static String getCommandOutput(String[] command) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ProcessAwaitExecutor executor = new ProcessAwaitExecutor(command); + try { + executor.streamHandler(i -> Attempt.uncheck(Streams::transfer, i, out)) + .waitFor() + .sync() + .exec(); + return out.toString(); + } finally { + Streams.close(out); + Streams.close(executor); + } + } + +} diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/SystemSettingMapper.xml b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/SystemSettingMapper.xml index 05e6ee05..457806f2 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/SystemSettingMapper.xml +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/resources/mapper/SystemSettingMapper.xml @@ -5,9 +5,8 @@ - - + @@ -18,7 +17,7 @@ - id, config_key, type, item, value, create_time, update_time, creator, updater, deleted + id, config_key, type, value, create_time, update_time, creator, updater, deleted