diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigRef.java b/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigRef.java index 70204026..7f009f59 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigRef.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigRef.java @@ -24,6 +24,7 @@ package org.dromara.visor.common.config; import lombok.extern.slf4j.Slf4j; +import java.util.function.BiConsumer; import java.util.function.Function; /** @@ -34,14 +35,14 @@ import java.util.function.Function; * @since 2025/1/6 18:01 */ @Slf4j -public class ConfigRef { +public abstract class ConfigRef { public final String key; - private final Function convert; - public T value; + protected final Function convert; + public ConfigRef(String key, Function convert) { this.key = key; this.convert = convert; @@ -52,21 +53,28 @@ public class ConfigRef { * * @param value value */ - public void override(String value) { - try { - this.value = convert.apply(value); - } catch (Exception e) { - log.error("ConfigRef trigger override error key: {}, value: {}", key, value, e); - } - } + public abstract void override(String value); /** - * 设置值 + * 修改配置 * * @param value value */ - public void set(T value) { - this.value = value; - } + public abstract void set(T value); + + /** + * 获取配置 + * + * @return value + */ + public abstract T get(); + + /** + * 修改回调 + * + * @param changeEvent changeEvent + * @return this + */ + public abstract ConfigRef onChange(BiConsumer changeEvent); } diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigStore.java b/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigStore.java index f87d9882..d320a4e6 100644 --- a/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigStore.java +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/config/ConfigStore.java @@ -33,6 +33,91 @@ import java.util.function.Function; */ public interface ConfigStore { + /** + * 获取 string 配置 + * + * @param key key + * @return config + */ + String getString(String key); + + /** + * 获取 string 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + String getString(String key, String defaultValue); + + /** + * 获取 int 配置 + * + * @param key key + * @return config + */ + Integer getInteger(String key); + + /** + * 获取 int 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + Integer getInteger(String key, Integer defaultValue); + + /** + * 获取 long 配置 + * + * @param key key + * @return config + */ + Long getLong(String key); + + /** + * 获取 long 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + Long getLong(String key, Long defaultValue); + + /** + * 获取 double 配置 + * + * @param key key + * @return config + */ + Double getDouble(String key); + + /** + * 获取 double 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + Double getDouble(String key, Double defaultValue); + + /** + * 获取 boolean 配置 + * + * @param key key + * @return config + */ + Boolean getBoolean(String key); + + /** + * 获取 boolean 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + Boolean getBoolean(String key, Boolean defaultValue); + /** * 获取配置 * @@ -64,6 +149,7 @@ public interface ConfigStore { * 获取配置 * * @param key key + * @param convert convert * @param defaultValue defaultValue * @param T * @return conf diff --git a/orion-visor-common/src/main/java/org/dromara/visor/common/constant/ConfigKeys.java b/orion-visor-common/src/main/java/org/dromara/visor/common/constant/ConfigKeys.java new file mode 100644 index 00000000..3ef9c0ad --- /dev/null +++ b/orion-visor-common/src/main/java/org/dromara/visor/common/constant/ConfigKeys.java @@ -0,0 +1,44 @@ +/* + * 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.common.constant; + +/** + * 配置项常量 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/1/14 16:15 + */ +public interface ConfigKeys { + + /** + * 加密公钥 + */ + String ENCRYPT_PUBLIC_KEY = "encrypt.publicKey"; + + /** + * 加密私钥 + */ + String ENCRYPT_PRIVATE_KEY = "encrypt.privateKey"; + +} diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/ref/ConfigRefImpl.java b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/ref/ConfigRefImpl.java new file mode 100644 index 00000000..d82841bf --- /dev/null +++ b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/ref/ConfigRefImpl.java @@ -0,0 +1,82 @@ +/* + * 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.framework.config.core.ref; + +import cn.orionsec.kit.lang.utils.Objects1; +import lombok.extern.slf4j.Slf4j; +import org.dromara.visor.common.config.ConfigRef; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +/** + * 配置引用实现类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2025/1/14 16:10 + */ +@Slf4j +public class ConfigRefImpl extends ConfigRef { + + protected BiConsumer changeEvent; + + public ConfigRefImpl(String key, Function convert) { + super(key, convert); + } + + @Override + public void override(String value) { + try { + this.set(convert.apply(value)); + } catch (Exception e) { + log.error("ConfigRef trigger override error key: {}, value: {}", key, value, e); + } + } + + @Override + public void set(T value) { + T before = this.value; + this.value = value; + // 被修改 + if (!Objects1.eq(before, value)) { + log.info("ConfigRef changed key: {}, value: {}", key, value); + // 触发事件 + if (changeEvent != null) { + changeEvent.accept(value, before); + } + } + } + + @Override + public T get() { + return value; + } + + @Override + public ConfigRef onChange(BiConsumer changeEvent) { + this.changeEvent = changeEvent; + return this; + } + +} diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/store/ManagementConfigStoreImpl.java b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/store/ManagementConfigStoreImpl.java index 2c2b07ef..7d4a84a0 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/store/ManagementConfigStoreImpl.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/store/ManagementConfigStoreImpl.java @@ -22,10 +22,11 @@ */ package org.dromara.visor.framework.config.core.store; -import org.dromara.visor.common.config.ConfigRef; -import org.dromara.visor.framework.config.core.service.ConfigFrameworkService; import cn.orionsec.kit.lang.utils.collect.Lists; import lombok.extern.slf4j.Slf4j; +import org.dromara.visor.common.config.ConfigRef; +import org.dromara.visor.framework.config.core.ref.ConfigRefImpl; +import org.dromara.visor.framework.config.core.service.ConfigFrameworkService; import java.util.ArrayList; import java.util.List; @@ -59,7 +60,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore { @Override public void override(String key, String value) { - log.info("ManagementConfigStore.override key: {}, value: {}", key, value); + log.info("ConfigStore.override key: {}, value: {}", key, value); // 修改配置 configMap.put(key, value); // 修改引用 @@ -72,11 +73,61 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore { @Override public void register(ConfigRef ref) { String key = ref.key; - log.info("ManagementConfigStore.register ref key: {}", key); + log.info("ConfigStore.register ref key: {}", key); // 注册引用 configRefs.computeIfAbsent(key, k -> new ArrayList<>()).add(ref); } + @Override + public String getString(String key) { + return this.getConfig(key); + } + + @Override + public String getString(String key, String defaultValue) { + return this.getConfig(key, defaultValue); + } + + @Override + public Integer getInteger(String key) { + return this.getConfig(key, Integer::valueOf, null); + } + + @Override + public Integer getInteger(String key, Integer defaultValue) { + return this.getConfig(key, Integer::valueOf, defaultValue); + } + + @Override + public Long getLong(String key) { + return this.getConfig(key, Long::valueOf, null); + } + + @Override + public Long getLong(String key, Long defaultValue) { + return this.getConfig(key, Long::valueOf, defaultValue); + } + + @Override + public Double getDouble(String key) { + return this.getConfig(key, Double::valueOf, null); + } + + @Override + public Double getDouble(String key, Double defaultValue) { + return this.getConfig(key, Double::valueOf, defaultValue); + } + + @Override + public Boolean getBoolean(String key) { + return this.getConfig(key, Boolean::valueOf, null); + } + + @Override + public Boolean getBoolean(String key, Boolean defaultValue) { + return this.getConfig(key, Boolean::valueOf, defaultValue); + } + @Override public String getConfig(String key) { return this.getConfig(key, Function.identity(), null); @@ -162,7 +213,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore { @Override public ConfigRef ref(String key, Function convert, T defaultValue) { // 创建引用 - ConfigRef ref = new ConfigRef<>(key, convert); + ConfigRef ref = new ConfigRefImpl<>(key, convert); // 设置值 String value = configMap.get(key); if (value != null) { diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/utils/ConfigStores.java b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/utils/ConfigStores.java index 1c372556..c3145518 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/utils/ConfigStores.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-config/src/main/java/org/dromara/visor/framework/config/core/utils/ConfigStores.java @@ -42,6 +42,111 @@ public class ConfigStores { private ConfigStores() { } + /** + * 获取 string 配置 + * + * @param key key + * @return config + */ + public String getString(String key) { + return delegate.getString(key); + } + + /** + * 获取 string 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + public String getString(String key, String defaultValue) { + return delegate.getString(key, defaultValue); + } + + /** + * 获取 int 配置 + * + * @param key key + * @return config + */ + public Integer getInteger(String key) { + return delegate.getInteger(key); + } + + /** + * 获取 int 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + public Integer getInteger(String key, Integer defaultValue) { + return delegate.getInteger(key, defaultValue); + } + + /** + * 获取 long 配置 + * + * @param key key + * @return config + */ + public Long getLong(String key) { + return delegate.getLong(key); + } + + /** + * 获取 long 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + public Long getLong(String key, Long defaultValue) { + return delegate.getLong(key, defaultValue); + } + + /** + * 获取 double 配置 + * + * @param key key + * @return config + */ + public Double getDouble(String key) { + return delegate.getDouble(key); + } + + /** + * 获取 double 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + public Double getDouble(String key, Double defaultValue) { + return delegate.getDouble(key, defaultValue); + } + + /** + * 获取 boolean 配置 + * + * @param key key + * @return config + */ + public Boolean getBoolean(String key) { + return delegate.getBoolean(key); + } + + /** + * 获取 boolean 配置 + * + * @param key key + * @param defaultValue defaultValue + * @return config + */ + public Boolean getBoolean(String key, Boolean defaultValue) { + return delegate.getBoolean(key, defaultValue); + } + /** * 获取配置 * diff --git a/orion-visor-framework/orion-visor-spring-boot-starter-encrypt/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaEncryptorImpl.java b/orion-visor-framework/orion-visor-spring-boot-starter-encrypt/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaEncryptorImpl.java index e6ec41bb..be53e73a 100644 --- a/orion-visor-framework/orion-visor-spring-boot-starter-encrypt/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaEncryptorImpl.java +++ b/orion-visor-framework/orion-visor-spring-boot-starter-encrypt/src/main/java/org/dromara/visor/framework/encrypt/core/impl/RsaEncryptorImpl.java @@ -25,6 +25,7 @@ package org.dromara.visor.framework.encrypt.core.impl; import cn.orionsec.kit.lang.utils.crypto.RSA; import org.dromara.visor.common.config.ConfigRef; import org.dromara.visor.common.config.ConfigStore; +import org.dromara.visor.common.constant.ConfigKeys; import org.dromara.visor.common.interfaces.RsaEncryptor; import java.security.interfaces.RSAPrivateKey; @@ -44,8 +45,8 @@ public class RsaEncryptorImpl implements RsaEncryptor { private final ConfigRef privateKey; public RsaEncryptorImpl(ConfigStore configStore) { - this.publicKey = configStore.ref("encrypt.publicKey", RSA::getPublicKey); - this.privateKey = configStore.ref("encrypt.privateKey", RSA::getPrivateKey); + this.publicKey = configStore.ref(ConfigKeys.ENCRYPT_PUBLIC_KEY, RSA::getPublicKey); + this.privateKey = configStore.ref(ConfigKeys.ENCRYPT_PRIVATE_KEY, RSA::getPrivateKey); } @Override diff --git a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/handler/host/transfer/model/SftpFileBackupParams.java b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/handler/host/transfer/model/SftpFileBackupParams.java index 07fd5427..63ca63f7 100644 --- a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/handler/host/transfer/model/SftpFileBackupParams.java +++ b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/handler/host/transfer/model/SftpFileBackupParams.java @@ -22,10 +22,10 @@ */ package org.dromara.visor.module.asset.handler.host.transfer.model; -import lombok.AllArgsConstructor; -import lombok.Builder; +import cn.orionsec.kit.lang.utils.time.Dates; import lombok.Data; -import lombok.NoArgsConstructor; + +import java.util.Date; /** * sftp 文件备份参数 @@ -35,9 +35,6 @@ import lombok.NoArgsConstructor; * @since 2024/4/15 23:13 */ @Data -@Builder -@NoArgsConstructor -@AllArgsConstructor public class SftpFileBackupParams { /** @@ -50,4 +47,16 @@ public class SftpFileBackupParams { */ private Long timestamp; + /** + * 当前时间 + */ + private String time; + + public SftpFileBackupParams(String fileName) { + this.fileName = fileName; + Date date = new Date(); + this.timestamp = date.getTime(); + this.time = Dates.format(date, Dates.YMD_HMS2); + } + } diff --git a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/utils/SftpUtils.java b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/utils/SftpUtils.java index d83e65f7..ce1ea510 100644 --- a/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/utils/SftpUtils.java +++ b/orion-visor-modules/orion-visor-module-asset/orion-visor-module-asset-service/src/main/java/org/dromara/visor/module/asset/utils/SftpUtils.java @@ -58,7 +58,7 @@ public class SftpUtils { SftpFile file = executor.getFile(path); if (file != null) { // 文件存在则备份 - SftpFileBackupParams backupParams = new SftpFileBackupParams(file.getName(), System.currentTimeMillis()); + SftpFileBackupParams backupParams = new SftpFileBackupParams(file.getName()); String target = Strings.format(config.getBackupFileName(), JSON.parseObject(JSON.toJSONString(backupParams))); // 移动 executor.move(path, target); 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 c6cf3d2b..f2caa62b 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 @@ -30,7 +30,7 @@ import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO; import java.util.concurrent.TimeUnit; /** - * 系统配置缓存 key + * 系统设置缓存 key * * @author Jiahang Li * @version 1.0.0 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 f244ddfe..97b7615e 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 @@ -32,7 +32,7 @@ import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingMod import java.util.Map; /** - * 系统配置类型枚举 + * 系统设置类型枚举 * * @author Jiahang Li * @version 1.0.0 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 index 8f276278..0c4a7468 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/handler/setting/model/EncryptSystemSettingModel.java @@ -28,7 +28,7 @@ import lombok.Data; import lombok.NoArgsConstructor; /** - * 加密系统配置模型 + * 加密系统设置模型 * * @author Jiahang Li * @version 1.0.0 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 a2863d41..aae87049 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 @@ -28,7 +28,7 @@ import lombok.Data; import lombok.NoArgsConstructor; /** - * SFTP 系统配置模型 + * SFTP 系统设置模型 * * @author Jiahang Li * @version 1.0.0