🔨 修改系统设置逻辑.

This commit is contained in:
lijiahang
2025-01-23 10:13:54 +08:00
parent 0abd4a893b
commit abf384dd3c
12 changed files with 207 additions and 289 deletions

View File

@@ -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<String, String> 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<String, String> getSystemSettingByType(@RequestParam("type") String type) {
return systemSettingService.getSystemSettingByType(type);
}

View File

@@ -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();

View File

@@ -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 = "<sb>{}</sb> - <sb>{}</sb> - <sb>{}</sb>";
public static final String UPDATE_BATCH_TEXT = "<sb>{}</sb>";
public static final String UPDATE = "system-setting:update";
@Override
public OperatorType[] types() {
return new OperatorType[]{
new OperatorType(M, UPDATE, "更新系统设置 ${text}"),
new OperatorType(M, UPDATE, "更新系统设置 <sb>${type}</sb>"),
};
}

View File

@@ -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")

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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> T parseModel(Map<String, String> 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;
}

View File

@@ -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<String, String> getSystemAggregateSetting();
/**
* 生成密钥对
*
* @return keypair
*/
EncryptSystemSettingModel generatorKeypair();
RsaKeyPairVO generatorKeypair();
/**
* 通过类型查询系统设置
*
* @param type type
* @param <T> T
* @return row
* @return rows
*/
<T> T getSystemSettingByType(String type);
Map<String, String> getSystemSettingByType(String type);
/**
* 更新部分系统设置
* 更新系统设置-单个
*
* @param request request
*/
void updateSystemSetting(SystemSettingUpdateRequest request);
/**
* 更新系统设置
* 更新系统设置-多个
*
* @param request request
*/

View File

@@ -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<String, String> getSystemAggregateSetting() {
// 查询缓存
SystemSettingAggregateVO cache = RedisStrings.getJson(SystemSettingKeyDefine.SETTING);
SystemSettingAggregateVO result = Objects1.def(cache, SystemSettingAggregateVO::new);
if (cache == null) {
Map<String, String> cache = RedisMaps.entities(SystemSettingKeyDefine.SETTING);
if (Maps.isEmpty(cache)) {
// 查询数据库
Map<String, List<SystemSettingDO>> 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<String, String> 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<RSAPublicKey, RSAPrivateKey> pair = RSA.generatorKeys();
return EncryptSystemSettingModel.builder()
return RsaKeyPairVO.builder()
.publicKey(Keys.getPublicKey(pair.getKey()))
.privateKey(Keys.getPrivateKey(pair.getValue()))
.build();
}
@Override
public <T> T getSystemSettingByType(String type) {
SystemSettingTypeEnum settingType = SystemSettingTypeEnum.of(type);
Valid.notNull(settingType, ErrorMessage.ERROR_TYPE);
// 查询数据库
Map<String, String> settings = systemSettingDAO.of()
public Map<String, String> 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<SystemSettingDO> 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<String, String> settings = request.getSettings();
// 删除
LambdaQueryWrapper<SystemSettingDO> deleteWrapper = systemSettingDAO.lambda()
.eq(SystemSettingDO::getType, type)
.in(SystemSettingDO::getItem, settings.keySet());
systemSettingDAO.delete(deleteWrapper);
systemSettingDAO.delete(Conditions.in(SystemSettingDO::getConfigKey, settings.keySet()));
// 插入
List<SystemSettingDO> 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<String, String> 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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -5,9 +5,8 @@
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="org.dromara.visor.module.infra.entity.domain.SystemSettingDO">
<id column="id" property="id"/>
<result column="config_key" property="configKey"/>
<result column="type" property="type"/>
<result column="item" property="item"/>
<result column="config_key" property="configKey"/>
<result column="value" property="value"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
@@ -18,7 +17,7 @@
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
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
</sql>
</mapper>