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 01a70260..f8d0a5b4 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 @@ -32,16 +32,18 @@ import org.dromara.visor.framework.log.core.enums.IgnoreLogMode; import org.dromara.visor.framework.web.core.annotation.DemoDisableApi; import org.dromara.visor.framework.web.core.annotation.RestWrapper; import org.dromara.visor.module.infra.define.operator.SystemSettingOperatorType; -import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdatePartialRequest; +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.service.SystemSettingService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.util.Map; +import javax.annotation.security.PermitAll; /** * 系统设置服务 @@ -68,30 +70,49 @@ public class SystemSettingController { return systemSettingService.getAppInfo(); } + @PermitAll @IgnoreLog(IgnoreLogMode.RET) @GetMapping("/setting") + @Operation(summary = "获取系统聚合设置") + public SystemSettingAggregateVO getSystemAggregateSetting() { + return systemSettingService.getSystemAggregateSetting(); + } + + @DemoDisableApi + @IgnoreLog(IgnoreLogMode.RET) + @GetMapping("/generator-keypair") + @Operation(summary = "生成密钥对") + @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") + public EncryptSystemSettingModel generatorKeypair() { + return systemSettingService.generatorKeypair(); + } + + @IgnoreLog(IgnoreLogMode.RET) + @GetMapping("/get") @Operation(summary = "查询系统设置") @Parameter(name = "type", description = "type", required = true) - public Map getSystemSettingByType(@RequestParam("type") String type) { + @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") + public Object getSystemSettingByType(@RequestParam("type") String type) { return systemSettingService.getSystemSettingByType(type); } @DemoDisableApi @OperatorLog(SystemSettingOperatorType.UPDATE) @PutMapping("/update") - @Operation(summary = "更新系统设置") + @Operation(summary = "更新系统设置-单个") @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") - public Integer updateSystemSetting(@Validated @RequestBody SystemSettingUpdateRequest request) { - return systemSettingService.updateSystemSetting(request); + public Boolean updateSystemSetting(@Validated @RequestBody SystemSettingUpdateRequest request) { + systemSettingService.updateSystemSetting(request); + return true; } @DemoDisableApi @OperatorLog(SystemSettingOperatorType.UPDATE) - @PutMapping("/update-partial") - @Operation(summary = "更新部分系统设置") + @PutMapping("/update-batch") + @Operation(summary = "更新系统设置-多个") @PreAuthorize("@ss.hasPermission('infra:system-setting:update')") - public Boolean updatePartialSystemSetting(@Validated @RequestBody SystemSettingUpdatePartialRequest request) { - systemSettingService.updatePartialSystemSetting(request); + public Boolean updateSystemSettingBatch(@Validated @RequestBody SystemSettingUpdateBatchRequest request) { + systemSettingService.updateSystemSettingBatch(request); return true; } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/convert/SystemSettingConvert.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/convert/SystemSettingConvert.java index af3c623a..5c487995 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/convert/SystemSettingConvert.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/convert/SystemSettingConvert.java @@ -25,7 +25,6 @@ package org.dromara.visor.module.infra.convert; import org.dromara.visor.module.infra.entity.domain.SystemSettingDO; import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdateRequest; import org.mapstruct.Mapper; -import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; /** @@ -40,7 +39,6 @@ public interface SystemSettingConvert { SystemSettingConvert MAPPER = Mappers.getMapper(SystemSettingConvert.class); - @Mapping(target = "value", ignore = true) SystemSettingDO to(SystemSettingUpdateRequest request); } 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 22580202..c6cf3d2b 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,7 @@ 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 cn.orionsec.kit.lang.define.wrapper.Ref; +import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; import java.util.concurrent.TimeUnit; @@ -39,10 +39,10 @@ import java.util.concurrent.TimeUnit; public interface SystemSettingKeyDefine { CacheKeyDefine SETTING = new CacheKeyBuilder() - .key("system:setting:{}") - .desc("系统设置 ${type}") - .type(Ref.class) - .struct(RedisCacheStruct.HASH) + .key("system:setting:agg") + .desc("系统聚合设置") + .type(SystemSettingAggregateVO.class) + .struct(RedisCacheStruct.STRING) .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 7058a37e..a6cf3da3 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 @@ -40,7 +40,7 @@ public class SystemSettingOperatorType extends InitializingOperatorTypes { public static final String UPDATE_TEXT = "{} - {} - {}"; - public static final String UPDATE_PARTIAL_TEXT = "{}"; + public static final String UPDATE_BATCH_TEXT = "{}"; public static final String UPDATE = "system-setting:update"; 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 a38e8c17..aef3cf84 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,6 +52,10 @@ 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; 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/SystemSettingUpdatePartialRequest.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/SystemSettingUpdateBatchRequest.java similarity index 85% rename from orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdatePartialRequest.java rename to orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/entity/request/system/SystemSettingUpdateBatchRequest.java index 97b76d04..bd7ebbe9 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/SystemSettingUpdatePartialRequest.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/SystemSettingUpdateBatchRequest.java @@ -35,7 +35,7 @@ import java.io.Serializable; import java.util.Map; /** - * 系统设置部分 更新请求对象 + * 系统设置 批量更新请求对象 * * @author Jiahang Li * @version 3.0.0 @@ -45,8 +45,8 @@ import java.util.Map; @Builder @NoArgsConstructor @AllArgsConstructor -@Schema(name = "SystemSettingUpdatePartialRequest", description = "系统设置部分 更新请求对象") -public class SystemSettingUpdatePartialRequest implements Serializable { +@Schema(name = "SystemSettingUpdateBatchRequest", description = "系统设置 批量更新请求对象") +public class SystemSettingUpdateBatchRequest implements Serializable { private static final long serialVersionUID = 1L; @@ -57,6 +57,6 @@ public class SystemSettingUpdatePartialRequest implements Serializable { @NotEmpty @Schema(description = "配置") - private Map settings; + private Map settings; } 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 213b0116..e6f90bd2 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 @@ -59,6 +59,6 @@ public class SystemSettingUpdateRequest implements Serializable { private String item; @Schema(description = "配置值") - private Object value; + 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/strategy/SftpSystemSettingStrategy.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 similarity index 53% rename from orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/strategy/SftpSystemSettingStrategy.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/SystemSettingAggregateVO.java index dc4eed64..ed810cc3 100644 --- a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/strategy/SftpSystemSettingStrategy.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 @@ -20,37 +20,34 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.dromara.visor.module.infra.handler.setting.strategy; +package org.dromara.visor.module.infra.entity.vo; -import cn.orionsec.kit.lang.utils.Exceptions; -import org.dromara.visor.common.handler.data.strategy.AbstractGenericsDataStrategy; +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; -import org.springframework.stereotype.Component; /** - * SFTP 系统配置策略 + * 系统设置 聚合响应对象 * * @author Jiahang Li * @version 1.0.0 - * @since 2024/10/9 11:44 + * @since 2025/1/3 17:46 */ -@Component -public class SftpSystemSettingStrategy extends AbstractGenericsDataStrategy { +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Schema(name = "SystemSettingAggregateVO", description = "系统设置 聚合响应对象") +public class SystemSettingAggregateVO { - public SftpSystemSettingStrategy() { - super(SftpSystemSettingModel.class); - } + @Schema(description = "SFTP 设置") + private SftpSystemSettingModel sftp; - @Override - public SftpSystemSettingModel getDefault() { - return SftpSystemSettingModel.builder() - .previewSize(2) - .build(); - } + @Schema(description = "加密设置") + private EncryptSystemSettingModel encrypt; - @Override - public SftpSystemSettingModel parse(String serialModel) { - throw Exceptions.unsupported(); - } - -} \ No newline at end of file +} 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 6fb38344..f244ddfe 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,12 +22,14 @@ */ package org.dromara.visor.module.infra.enums; +import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Getter; -import org.dromara.visor.common.handler.data.GenericsStrategyDefinition; -import org.dromara.visor.common.handler.data.model.GenericsDataModel; -import org.dromara.visor.common.handler.data.strategy.GenericsDataStrategy; -import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSettingStrategy; +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; /** * 系统配置类型枚举 @@ -38,34 +40,55 @@ import org.dromara.visor.module.infra.handler.setting.strategy.SftpSystemSetting */ @Getter @AllArgsConstructor -public enum SystemSettingTypeEnum implements GenericsStrategyDefinition { +public enum SystemSettingTypeEnum { /** * SFTP 配置 */ - SFTP(SftpSystemSettingStrategy.class), + SFTP("sftp", SftpSystemSettingModel.class), + + /** + * 加密配置 + */ + ENCRYPT("encrypt", EncryptSystemSettingModel.class), ; - SystemSettingTypeEnum(Class> strategyClass) { - this.type = this.name(); - this.strategyClass = strategyClass; + 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); } - private final String type; - - private final Class> strategyClass; - public static SystemSettingTypeEnum of(String type) { if (type == null) { return null; } for (SystemSettingTypeEnum value : values()) { - if (value.type.equals(type)) { + 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; + } + } diff --git a/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/framework/service/impl/ConfigFrameworkServiceImpl.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/framework/service/impl/ConfigFrameworkServiceImpl.java new file mode 100644 index 00000000..bbf65d35 --- /dev/null +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/framework/service/impl/ConfigFrameworkServiceImpl.java @@ -0,0 +1,60 @@ +/* + * 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.framework.service.impl; + +import cn.orionsec.kit.lang.function.Functions; +import lombok.extern.slf4j.Slf4j; +import org.dromara.visor.framework.config.core.service.ConfigFrameworkService; +import org.dromara.visor.module.infra.dao.SystemSettingDAO; +import org.dromara.visor.module.infra.entity.domain.SystemSettingDO; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 配置框架服务实现 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/1/6 23:35 + */ +@Slf4j +@Service +public class ConfigFrameworkServiceImpl implements ConfigFrameworkService { + + @Resource + private SystemSettingDAO systemSettingDAO; + + @Override + public Map getAllConfig() { + return systemSettingDAO.of() + .stream() + .collect(Collectors.toMap( + SystemSettingDO::getConfigKey, + SystemSettingDO::getValue, + Functions.right())); + } + +} 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/handler/setting/model/EncryptSystemSettingModel.java new file mode 100644 index 00000000..8f276278 --- /dev/null +++ b/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 @@ -0,0 +1,53 @@ +/* + * 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.handler.setting.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 加密系统配置模型 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2024/10/9 11:45 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class EncryptSystemSettingModel { + + /** + * 加密公钥 + */ + private String publicKey; + + /** + * 加密私钥 + */ + 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/handler/setting/model/SftpSystemSettingModel.java b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/model/SftpSystemSettingModel.java index f1e27b2e..a2863d41 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/SftpSystemSettingModel.java +++ b/orion-visor-modules/orion-visor-module-infra/orion-visor-module-infra-service/src/main/java/org/dromara/visor/module/infra/handler/setting/model/SftpSystemSettingModel.java @@ -26,7 +26,6 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.dromara.visor.common.handler.data.model.GenericsDataModel; /** * SFTP 系统配置模型 @@ -39,7 +38,7 @@ import org.dromara.visor.common.handler.data.model.GenericsDataModel; @Builder @NoArgsConstructor @AllArgsConstructor -public class SftpSystemSettingModel implements GenericsDataModel { +public class SftpSystemSettingModel { /** * 预览大小 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 32d28c72..62fa03e7 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 @@ -22,11 +22,11 @@ */ package org.dromara.visor.module.infra.service; -import org.dromara.visor.module.infra.entity.request.system.SystemSettingUpdatePartialRequest; +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 java.util.Map; +import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; +import org.dromara.visor.module.infra.handler.setting.model.EncryptSystemSettingModel; /** * 系统设置服务 @@ -45,26 +45,40 @@ public interface SystemSettingService { AppInfoVO getAppInfo(); /** - * 更新系统设置 + * 获取系统聚合设置 * - * @param request request - * @return effect + * @return setting */ - Integer updateSystemSetting(SystemSettingUpdateRequest request); + SystemSettingAggregateVO getSystemAggregateSetting(); + + /** + * 生成密钥对 + * + * @return keypair + */ + EncryptSystemSettingModel generatorKeypair(); + + /** + * 通过类型查询系统设置 + * + * @param type type + * @param T + * @return row + */ + T getSystemSettingByType(String type); /** * 更新部分系统设置 * * @param request request */ - void updatePartialSystemSetting(SystemSettingUpdatePartialRequest request); + void updateSystemSetting(SystemSettingUpdateRequest request); /** - * 通过类型查询系统设置 + * 更新系统设置 * - * @param type type - * @return row + * @param request request */ - Map getSystemSettingByType(String type); + void updateSystemSettingBatch(SystemSettingUpdateBatchRequest 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 20b620ae..7995e2ee 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 @@ -23,38 +23,46 @@ 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.Refs; +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.framework.redis.core.utils.RedisMaps; +import org.dromara.visor.framework.config.core.event.ConfigUpdateEvent; +import org.dromara.visor.framework.redis.core.utils.RedisStrings; 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.SystemSettingUpdatePartialRequest; +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.service.SystemSettingService; 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; import java.util.Map; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -81,28 +89,103 @@ public class SystemSettingServiceImpl implements SystemSettingService { } @Override - public Integer updateSystemSetting(SystemSettingUpdateRequest request) { - String type = request.getType(); - String item = request.getItem(); - Object value = request.getValue(); - // 更新 - SystemSettingDO update = new SystemSettingDO(); - update.setValue(Refs.json(value)); - LambdaQueryWrapper wrapper = systemSettingDAO.lambda() - .eq(SystemSettingDO::getType, type) - .eq(SystemSettingDO::getItem, item); - int effect = systemSettingDAO.update(update, wrapper); - // 删除缓存 - RedisUtils.delete(SystemSettingKeyDefine.SETTING.format(type)); - // 设置日志参数 - OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_TEXT, type, item, value)); - return effect; + public SystemSettingAggregateVO getSystemAggregateSetting() { + // 查询缓存 + SystemSettingAggregateVO cache = RedisStrings.getJson(SystemSettingKeyDefine.SETTING); + SystemSettingAggregateVO result = Objects1.def(cache, SystemSettingAggregateVO::new); + if (cache == null) { + // 查询数据库 + Map> typeGroup = systemSettingDAO.of() + .createWrapper() + .select(SystemSettingDO::getType, + SystemSettingDO::getItem, + SystemSettingDO::getValue) + .in(SystemSettingDO::getType, + SystemSettingTypeEnum.SFTP.name(), + SystemSettingTypeEnum.ENCRYPT.name()) + .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); + } + }); + // 设置缓存 + RedisStrings.setJson(SystemSettingKeyDefine.SETTING, result); + } + return result; } @Override - public void updatePartialSystemSetting(SystemSettingUpdatePartialRequest request) { + public EncryptSystemSettingModel generatorKeypair() { + // 生成密钥对 + Pair pair = RSA.generatorKeys(); + return EncryptSystemSettingModel.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() + .createWrapper() + .eq(SystemSettingDO::getType, type) + .then() + .stream() + .collect(Collectors.toMap( + SystemSettingDO::getItem, + SystemSettingDO::getValue, + Functions.right())); + // 解析 + return settingType.parseModel(settings); + } + + @Override + public void updateSystemSetting(SystemSettingUpdateRequest request) { String type = request.getType(); - Map settings = request.getSettings(); + SystemSettingTypeEnum settingType = Valid.valid(SystemSettingTypeEnum::of, type); + String item = request.getItem(); + String value = request.getValue(); + // 更新 + SystemSettingDO update = new SystemSettingDO(); + update.setValue(value); + LambdaQueryWrapper wrapper = systemSettingDAO.lambda() + .eq(SystemSettingDO::getType, type) + .eq(SystemSettingDO::getItem, item); + 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)); + } + + @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) @@ -112,63 +195,25 @@ public class SystemSettingServiceImpl implements SystemSettingService { List rows = settings.entrySet() .stream() .map(s -> SystemSettingDO.builder() + .configKey(settingType.getConfigKey(s.getKey())) .type(type) .item(s.getKey()) - .value(Refs.json(s.getValue())) + .value(s.getValue()) .build()) .collect(Collectors.toList()); // 插入 systemSettingDAO.insertBatch(rows); // 删除缓存 - RedisUtils.delete(SystemSettingKeyDefine.SETTING.format(type)); + RedisUtils.delete(SystemSettingKeyDefine.SETTING); // 设置日志参数 - OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_PARTIAL_TEXT, type)); - } - - @Override - public Map getSystemSettingByType(String type) { - SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(type); - Valid.notNull(settingType, ErrorMessage.ERROR_TYPE); - // 查询缓存 - String key = SystemSettingKeyDefine.SETTING.format(type); - Map settings = RedisMaps.entities(key); - boolean setCache = Maps.isEmpty(settings); - // 查询数据库 - if (Maps.isEmpty(settings)) { - settings = systemSettingDAO.of() - .createWrapper() - .eq(SystemSettingDO::getType, type) - .then() - .stream() - .collect(Collectors.toMap( - SystemSettingDO::getItem, - SystemSettingDO::getValue, - Functions.right())); - } - // 初始化 - if (Maps.isEmpty(settings)) { - // 获取默认值 - Map defaultConfig = settingType.getDefault().toMap(); - settings = Maps.map(defaultConfig, Function.identity(), Refs::json); - // 插入默认值 - List entities = settings - .entrySet() - .stream() - .map(s -> { - SystemSettingDO entity = new SystemSettingDO(); - entity.setType(type); - entity.setItem(s.getKey()); - entity.setValue(s.getValue()); - return entity; - }).collect(Collectors.toList()); - systemSettingDAO.insertBatch(entities); - } - // 设置缓存 - if (setCache) { - RedisMaps.putAll(key, SystemSettingKeyDefine.SETTING, settings); - } - // unRef - return Maps.map(settings, Function.identity(), Refs::unref); + OperatorLogs.add(OperatorLogs.TEXT, Strings.format(SystemSettingOperatorType.UPDATE_BATCH_TEXT, type)); + // 触发修改事件 + Map eventConfig = rows.stream() + .collect(Collectors.toMap( + SystemSettingDO::getConfigKey, + SystemSettingDO::getValue, + Functions.right())); + SpringHolder.publishEvent(ConfigUpdateEvent.of(eventConfig)); } /** 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 7e990e2a..05e6ee05 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,6 +5,7 @@ + @@ -17,7 +18,7 @@ - id, type, item, value, create_time, update_time, creator, updater, deleted + id, config_key, type, item, value, create_time, update_time, creator, updater, deleted