refactor(system/storage): 修改存储配置时,保持Secret Key为空将不更改

This commit is contained in:
2025-08-28 21:54:31 +08:00
parent b5acdb1c1c
commit 2d86b0f249
3 changed files with 12 additions and 25 deletions

View File

@@ -47,7 +47,7 @@ public class StorageReq implements Serializable {
/**
* 名称
*/
@Schema(description = "名称", example = "存储1")
@Schema(description = "名称", example = "S3对象存储")
@NotBlank(message = "名称不能为空")
@Length(max = 100, message = "名称长度不能超过 {max} 个字符")
private String name;
@@ -55,7 +55,7 @@ public class StorageReq implements Serializable {
/**
* 编码
*/
@Schema(description = "编码", example = "local")
@Schema(description = "编码", example = "s3_aliyun")
@NotBlank(message = "编码不能为空")
@Pattern(regexp = RegexConstants.GENERAL_CODE, message = "编码长度为 2-30 个字符,支持大小写字母、数字、下划线,以字母开头")
private String code;
@@ -70,7 +70,7 @@ public class StorageReq implements Serializable {
/**
* Access Key
*/
@Schema(description = "Access Key", example = "")
@Schema(description = "Access Key", example = "LBAI4Fp4dXYcZamU5EXTBdTa")
@Length(max = 255, message = "Access Key长度不能超过 {max} 个字符")
@NotBlank(message = "Access Key不能为空", groups = ValidationGroup.Storage.OSS.class)
private String accessKey;
@@ -78,14 +78,13 @@ public class StorageReq implements Serializable {
/**
* Secret Key
*/
@Schema(description = "Secret Key", example = "")
@NotBlank(message = "Secret Key不能为空", groups = ValidationGroup.Storage.OSS.class)
@Schema(description = "Secret Key", example = "RSA 公钥加密的 Secret Key")
private String secretKey;
/**
* Endpoint
*/
@Schema(description = "Endpoint", example = "")
@Schema(description = "Endpoint", example = "http://oss-cn-shanghai.aliyuncs.com")
@Length(max = 255, message = "Endpoint长度不能超过 {max} 个字符")
@NotBlank(message = "Endpoint不能为空", groups = ValidationGroup.Storage.OSS.class)
@Pattern(regexp = RegexConstants.URL_HTTP, message = "Endpoint格式不正确", groups = ValidationGroup.Storage.OSS.class)
@@ -94,7 +93,7 @@ public class StorageReq implements Serializable {
/**
* Bucket/存储路径
*/
@Schema(description = "Bucket/存储路径", example = "C:/continew-admin/data/file/")
@Schema(description = "Bucket/存储路径", example = "continew-admin")
@Length(max = 255, message = "Bucket长度不能超过 {max} 个字符", groups = ValidationGroup.Storage.OSS.class)
@Length(max = 255, message = "存储路径长度不能超过 {max} 个字符", groups = ValidationGroup.Storage.Local.class)
@NotBlank(message = "Bucket不能为空", groups = ValidationGroup.Storage.OSS.class)
@@ -104,7 +103,7 @@ public class StorageReq implements Serializable {
/**
* 域名/访问路径
*/
@Schema(description = "域名/访问路径", example = "http://localhost:8000/file")
@Schema(description = "域名/访问路径", example = "https://continew-admin.file.continew.top/")
@Length(max = 255, message = "域名长度不能超过 {max} 个字符", groups = ValidationGroup.Storage.OSS.class)
@Length(max = 255, message = "访问路径长度不能超过 {max} 个字符", groups = ValidationGroup.Storage.Local.class)
@NotBlank(message = "访问路径不能为空", groups = ValidationGroup.Storage.Local.class)

View File

@@ -21,7 +21,6 @@ import lombok.Data;
import top.continew.admin.common.base.model.resp.BaseDetailResp;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.system.enums.StorageTypeEnum;
import top.continew.starter.security.mask.annotation.JsonMask;
import java.io.Serial;
@@ -68,13 +67,6 @@ public class StorageResp extends BaseDetailResp {
@Schema(description = "Access Key", example = "")
private String accessKey;
/**
* Secret Key
*/
@Schema(description = "Secret Key", example = "")
@JsonMask(left = 4, right = 3)
private String secretKey;
/**
* Endpoint
*/

View File

@@ -28,9 +28,9 @@ import org.dromara.x.file.storage.core.FileStorageServiceBuilder;
import org.dromara.x.file.storage.core.platform.FileStorage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.common.model.req.CommonStatusUpdateReq;
import top.continew.admin.common.base.service.BaseServiceImpl;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.model.req.CommonStatusUpdateReq;
import top.continew.admin.common.util.SecureUtils;
import top.continew.admin.system.enums.StorageTypeEnum;
import top.continew.admin.system.mapper.StorageMapper;
@@ -40,7 +40,6 @@ import top.continew.admin.system.model.req.StorageReq;
import top.continew.admin.system.model.resp.StorageResp;
import top.continew.admin.system.service.FileService;
import top.continew.admin.system.service.StorageService;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.core.util.SpringWebUtils;
import top.continew.starter.core.util.validation.CheckUtils;
@@ -68,6 +67,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
public void beforeCreate(StorageReq req) {
// 解密密钥
if (StorageTypeEnum.OSS.equals(req.getType())) {
ValidationUtils.throwIfBlank(req.getSecretKey(), "Secret Key不能为空");
req.setSecretKey(this.decryptSecretKey(req.getSecretKey(), null));
}
// 指定配置参数校验及预处理
@@ -228,13 +228,9 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
* @return 解密后的 SecretKey
*/
private String decryptSecretKey(String encryptSecretKey, StorageDO oldStorage) {
// 修改时SecretKey 为空或带 *将不更改
if (oldStorage != null) {
boolean isSecretKeyNotUpdate = StrUtil.isBlank(encryptSecretKey) || encryptSecretKey
.contains(StringConstants.ASTERISK);
if (isSecretKeyNotUpdate) {
return oldStorage.getSecretKey();
}
// 修改时SecretKey 为空将不更改
if (oldStorage != null && StrUtil.isBlank(encryptSecretKey)) {
return oldStorage.getSecretKey();
}
// 解密
String secretKey = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(encryptSecretKey));