From 670250a6d3f75e5d8a98504276d2ea89beb7464c Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Fri, 10 May 2024 00:10:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(minio=E5=B7=A5=E5=85=B7=E7=B1=BB):=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=81=E5=A2=9E=E5=8A=A0=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...nstant.java => CommonMessageConstant.java} | 6 +- .../common/constant/MailMessageConstant.java | 14 + .../common/constant/MinioMessageConstant.java | 13 + .../common/constant/SQLAutoFillConstant.java | 3 + .../common/constant/SecurityConstant.java | 2 - .../properties/SnowflakeProperties.java | 2 +- .../exception/GlobalExceptionHandler.java | 6 +- .../module/mail/utils/MailSendCheckUtil.java | 10 +- module/module-minio/pom.xml | 5 + .../bunny/module/minio/utils/MinioUtil.java | 675 +++++++++++++++++- service/pom.xml | 5 + .../security/CustomUserDetailsService.java | 6 +- .../service/impl/SysUserServiceImpl.java | 8 +- service/src/main/resources/application.yml | 34 +- 14 files changed, 746 insertions(+), 43 deletions(-) rename common/common-utils/src/main/java/cn/bunny/common/constant/{MessageConstant.java => CommonMessageConstant.java} (86%) create mode 100644 common/common-utils/src/main/java/cn/bunny/common/constant/MailMessageConstant.java create mode 100644 common/common-utils/src/main/java/cn/bunny/common/constant/MinioMessageConstant.java diff --git a/common/common-utils/src/main/java/cn/bunny/common/constant/MessageConstant.java b/common/common-utils/src/main/java/cn/bunny/common/constant/CommonMessageConstant.java similarity index 86% rename from common/common-utils/src/main/java/cn/bunny/common/constant/MessageConstant.java rename to common/common-utils/src/main/java/cn/bunny/common/constant/CommonMessageConstant.java index 4ce72f8..910d164 100644 --- a/common/common-utils/src/main/java/cn/bunny/common/constant/MessageConstant.java +++ b/common/common-utils/src/main/java/cn/bunny/common/constant/CommonMessageConstant.java @@ -6,7 +6,7 @@ import lombok.Data; * 信息提示常量类 */ @Data -public class MessageConstant { +public class CommonMessageConstant { public static final String PASSWORD_ERROR = "密码错误"; public static final String OLD_PASSWORD_ERROR = "旧密码不匹配"; public static final String OLD_PASSWORD_SAME_NEW_PASSWORD = "旧密码与新密码相同"; @@ -35,8 +35,4 @@ public class MessageConstant { public static final String LOGIN_DTO_IS_EMPTY = "登录参数不能为空"; public static final String TOKEN_IS_EMPTY = "token为空"; public static final String DATA_IS_EMPTY = "数据为空"; - public static final String EMPTY_SEND_OBJECT = "空发送对象"; - public static final String ADDRESS_NOT_NULL = "收件人不能为空"; - public static final String TITLE_NOT_NULL = "标题不能为空"; - public static final String SEND_MESSAGE_NOT_NULL = "发送消息不能为空"; } diff --git a/common/common-utils/src/main/java/cn/bunny/common/constant/MailMessageConstant.java b/common/common-utils/src/main/java/cn/bunny/common/constant/MailMessageConstant.java new file mode 100644 index 0000000..458ef48 --- /dev/null +++ b/common/common-utils/src/main/java/cn/bunny/common/constant/MailMessageConstant.java @@ -0,0 +1,14 @@ +package cn.bunny.common.constant; + +import lombok.Data; + +/** + * 邮箱消息 + */ +@Data +public class MailMessageConstant { + public static final String EMPTY_SEND_OBJECT = "空发送对象"; + public static final String ADDRESS_NOT_NULL = "收件人不能为空"; + public static final String TITLE_NOT_NULL = "标题不能为空"; + public static final String SEND_MESSAGE_NOT_NULL = "发送消息不能为空"; +} diff --git a/common/common-utils/src/main/java/cn/bunny/common/constant/MinioMessageConstant.java b/common/common-utils/src/main/java/cn/bunny/common/constant/MinioMessageConstant.java new file mode 100644 index 0000000..ab6e6a6 --- /dev/null +++ b/common/common-utils/src/main/java/cn/bunny/common/constant/MinioMessageConstant.java @@ -0,0 +1,13 @@ +package cn.bunny.common.constant; + +import lombok.Data; + +@Data +public class MinioMessageConstant { + public static final String BUCKET_EXISTS_EXCEPTION = "查询文化部对象失败"; + public static final String DELETE_BUCKET_EXCEPTION = "删除文件对象失败"; + public static final String GET_BUCKET_EXCEPTION = "获取文件信息失败"; + public static final String QUERY_BUCKET_EXCEPTION = "查询文件信息失败"; + public static final String CREATE_BUCKET_EXCEPTION = "创建文件对象失败"; + public static final String UPDATE_BUCKET_EXCEPTION = "更新文件对象失败"; +} diff --git a/common/common-utils/src/main/java/cn/bunny/common/constant/SQLAutoFillConstant.java b/common/common-utils/src/main/java/cn/bunny/common/constant/SQLAutoFillConstant.java index 67cb03a..b9b6205 100644 --- a/common/common-utils/src/main/java/cn/bunny/common/constant/SQLAutoFillConstant.java +++ b/common/common-utils/src/main/java/cn/bunny/common/constant/SQLAutoFillConstant.java @@ -1,8 +1,11 @@ package cn.bunny.common.constant; +import lombok.Data; + /** * 数据库中自动填充字段 */ +@Data public class SQLAutoFillConstant { public static final String SET_CREATE_TIME = "setCreateTime"; public static final String SET_UPDATE_TIME = "setUpdateTime"; diff --git a/common/common-utils/src/main/java/cn/bunny/common/constant/SecurityConstant.java b/common/common-utils/src/main/java/cn/bunny/common/constant/SecurityConstant.java index f19be58..da71e65 100644 --- a/common/common-utils/src/main/java/cn/bunny/common/constant/SecurityConstant.java +++ b/common/common-utils/src/main/java/cn/bunny/common/constant/SecurityConstant.java @@ -1,12 +1,10 @@ package cn.bunny.common.constant; import lombok.Data; -import org.springframework.context.annotation.Configuration; import java.util.Arrays; import java.util.List; -@Configuration @Data public class SecurityConstant { public static String[] annotations = {"/", "/test/**", "/diagram-viewer/**", "/editor-app/**", "/*.html", "/admin/system/index/login", "/favicon.ico", "/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**", "/doc.html"}; diff --git a/common/common-utils/src/main/java/cn/bunny/common/properties/SnowflakeProperties.java b/common/common-utils/src/main/java/cn/bunny/common/properties/SnowflakeProperties.java index 362d453..fe073ad 100644 --- a/common/common-utils/src/main/java/cn/bunny/common/properties/SnowflakeProperties.java +++ b/common/common-utils/src/main/java/cn/bunny/common/properties/SnowflakeProperties.java @@ -5,7 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component -@ConfigurationProperties(prefix = "snowflake") +@ConfigurationProperties(prefix = "bunny.snowflake") @Data public class SnowflakeProperties { // 数据中心id diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/exception/GlobalExceptionHandler.java b/common/service-utils/src/main/java/cn/bunny/common/service/exception/GlobalExceptionHandler.java index 9c5fc60..715dfc0 100644 --- a/common/service-utils/src/main/java/cn/bunny/common/service/exception/GlobalExceptionHandler.java +++ b/common/service-utils/src/main/java/cn/bunny/common/service/exception/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package cn.bunny.common.service.exception; -import cn.bunny.common.constant.MessageConstant; +import cn.bunny.common.constant.CommonMessageConstant; import cn.bunny.common.result.Result; import cn.bunny.enums.ResultCodeEnum; import lombok.extern.slf4j.Slf4j; @@ -72,10 +72,10 @@ public class GlobalExceptionHandler { // 截取用户名 String username = message.split(" ")[2]; // 错误信息 - String errorMessage = username + MessageConstant.ALREADY_EXISTS; + String errorMessage = username + CommonMessageConstant.ALREADY_EXISTS; return Result.error(errorMessage); } else { - return Result.error(MessageConstant.UNKNOWN_ERROR); + return Result.error(CommonMessageConstant.UNKNOWN_ERROR); } } } diff --git a/module/module-mail/src/main/java/cn/bunny/module/mail/utils/MailSendCheckUtil.java b/module/module-mail/src/main/java/cn/bunny/module/mail/utils/MailSendCheckUtil.java index 9295238..6ac37b5 100644 --- a/module/module-mail/src/main/java/cn/bunny/module/mail/utils/MailSendCheckUtil.java +++ b/module/module-mail/src/main/java/cn/bunny/module/mail/utils/MailSendCheckUtil.java @@ -1,6 +1,6 @@ package cn.bunny.module.mail.utils; -import cn.bunny.common.constant.MessageConstant; +import cn.bunny.common.constant.MailMessageConstant; import cn.bunny.common.service.utils.EmptyUtil; import cn.bunny.entity.email.EmailSend; @@ -12,12 +12,12 @@ public class MailSendCheckUtil { */ public static void check(EmailSend emailSend) { // 空发送对象 - EmptyUtil.isEmpty(emailSend, MessageConstant.EMPTY_SEND_OBJECT); + EmptyUtil.isEmpty(emailSend, MailMessageConstant.EMPTY_SEND_OBJECT); // 收件人不能为空 - EmptyUtil.isEmpty(emailSend.getSendTo(), MessageConstant.ADDRESS_NOT_NULL); + EmptyUtil.isEmpty(emailSend.getSendTo(), MailMessageConstant.ADDRESS_NOT_NULL); // 标题不能为空 - EmptyUtil.isEmpty(emailSend.getSubject(), MessageConstant.TITLE_NOT_NULL); + EmptyUtil.isEmpty(emailSend.getSubject(), MailMessageConstant.TITLE_NOT_NULL); // 发送消息不能为空 - EmptyUtil.isEmpty(emailSend.getMessage(), MessageConstant.SEND_MESSAGE_NOT_NULL); + EmptyUtil.isEmpty(emailSend.getMessage(), MailMessageConstant.SEND_MESSAGE_NOT_NULL); } } diff --git a/module/module-minio/pom.xml b/module/module-minio/pom.xml index cdc48fe..8e812f7 100644 --- a/module/module-minio/pom.xml +++ b/module/module-minio/pom.xml @@ -18,6 +18,11 @@ + + cn.bunny + service-utils + 0.0.1-SNAPSHOT + io.minio diff --git a/module/module-minio/src/main/java/cn/bunny/module/minio/utils/MinioUtil.java b/module/module-minio/src/main/java/cn/bunny/module/minio/utils/MinioUtil.java index b273856..c1e24ba 100644 --- a/module/module-minio/src/main/java/cn/bunny/module/minio/utils/MinioUtil.java +++ b/module/module-minio/src/main/java/cn/bunny/module/minio/utils/MinioUtil.java @@ -1,4 +1,677 @@ package cn.bunny.module.minio.utils; +import cn.bunny.common.constant.MinioMessageConstant; +import cn.bunny.common.service.exception.BunnyException; +import io.minio.*; +import io.minio.messages.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.InputStream; +import java.util.List; +import java.util.Map; + +/** + * Minio操作工具类 + * 简化操作步骤 + */ +@Component +@Slf4j public class MinioUtil { -} + @Autowired + private MinioClient minioClient; + + /** + * 判断桶是否存在 + * + * @param bucketName 桶名称 + * @return 布尔值,是否存在 + */ + public boolean bucketExists(String bucketName) { + boolean found = false; + try { + found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + return found; + } catch (Exception exception) { + log.error("判断桶是否存在 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + } + throw new BunnyException(MinioMessageConstant.BUCKET_EXISTS_EXCEPTION); + } + + /** + * 删除桶的加密配置 + * + * @param bucketName 桶名称 + * @return 是否删除成功,返回布尔值 + */ + public boolean deleteBucketEncryption(String bucketName) { + try { + minioClient.deleteBucketEncryption(DeleteBucketEncryptionArgs.builder().build()); + log.info("删除桶的加密配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的加密配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + } + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + + /** + * 删除桶的生命周期配置 + * + * @param bucketName 桶名称 + * @return 返回布尔值,是否删除成功 + */ + public boolean deleteBucketLifecycle(String bucketName) { + try { + minioClient.deleteBucketLifecycle(DeleteBucketLifecycleArgs.builder().build()); + log.info("删除桶的生命周期配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的生命周期配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶的通知配置 + * + * @param bucketName 桶的名称 + * @return 返回布尔值是否删除成功 + */ + public boolean deleteBucketNotification(String bucketName) { + try { + minioClient.deleteBucketNotification(DeleteBucketNotificationArgs.builder().bucket(bucketName).build()); + log.info("删除桶的通知配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的加密配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶策略配置 + * + * @param bucketName 桶的名称 + * @return 返回布尔值是否删除成功 + */ + public boolean deleteBucketPolicy(String bucketName) { + try { + minioClient.deleteBucketPolicy(DeleteBucketPolicyArgs.builder().bucket(bucketName).build()); + log.info("删除桶中的对象锁配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的加密配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶的桶复制配置 + * + * @param bucketName 桶的名称 + * @return 返回布尔值是否删除成功 + */ + public boolean deleteBucketReplication(String bucketName) { + try { + minioClient.deleteBucketReplication(DeleteBucketReplicationArgs.builder().bucket(bucketName).build()); + log.info("删除桶的桶复制配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的桶复制配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶的标签 + * + * @param bucketName 桶的名称 + * @return 返回布尔值是否删除成功 + */ + public boolean deleteBucketTags(String bucketName) { + try { + minioClient.deleteBucketTags(DeleteBucketTagsArgs.builder().bucket(bucketName).build()); + log.info("删除桶的标签 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶的桶复制配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶中的对象锁配置 + * + * @param bucketName 桶的名称 + * @return 返回布尔值是否删除成功 + */ + public boolean deleteObjectLockConfiguration(String bucketName) { + try { + minioClient.deleteObjectLockConfiguration(DeleteObjectLockConfigurationArgs.builder().bucket(bucketName).build()); + log.info("删除桶中的对象锁配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶中的对象锁配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 获取存储桶的加密配置 + * + * @param bucketName 桶的名称 + * @return SseConfiguration 获取成功不为null + */ + public SseConfiguration getBucketEncryption(String bucketName) { + SseConfiguration conf = null; + + try { + conf = minioClient.getBucketEncryption(GetBucketEncryptionArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶的加密配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶的加密配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return conf; + } + + /** + * 获取存储桶的生命周期配置 + * + * @param bucketName 桶的名称 + * @return LifecycleConfiguration 获取成功不为null + */ + public LifecycleConfiguration getBucketLifecycle(String bucketName) { + LifecycleConfiguration lifecycle = null; + try { + lifecycle = minioClient.getBucketLifecycle(GetBucketLifecycleArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶的生命周期配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶的生命周期配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return lifecycle; + } + + /** + * 获取存储桶的通知配置 + * + * @param bucketName 桶的名称 + * @return NotificationConfiguration 获取成功不为null + */ + public NotificationConfiguration getBucketNotification(String bucketName) { + NotificationConfiguration configuration = null; + + try { + configuration = minioClient.getBucketNotification(GetBucketNotificationArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶的通知配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶的通知配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return configuration; + } + + /** + * 获取桶的桶策略配置 + * + * @param bucketName 桶的名称 + * @return String 获取成功不为null + */ + public String getBucketPolicy(String bucketName) { + String config = null; + + try { + config = minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build()); + log.info("获取桶的桶策略配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取桶的桶策略配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return config; + } + + /** + * 获取桶复制参数 + * + * @param bucketName 桶的名称 + * @return ReplicationConfiguration 获取成功不为null + */ + public ReplicationConfiguration getBucketReplication(String bucketName) { + ReplicationConfiguration configuration = null; + + try { + configuration = minioClient.getBucketReplication(GetBucketReplicationArgs.builder().bucket(bucketName).build()); + log.info("获取桶复制参数 ------ 成功"); + } catch (Exception exception) { + log.error("获取桶复制参数 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return configuration; + } + + /** + * 获取存储桶的标签 + * + * @param bucketName 桶的名称 + * @return Tags 获取成功不为null + */ + public Tags getBucketTags(String bucketName) { + Tags tags = null; + + try { + tags = minioClient.getBucketTags(GetBucketTagsArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶的标签 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶的标签 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + return tags; + } + + /** + * 获取存储桶的版本控制配置 + * + * @param bucketName 桶的名称 + * @return Tags 获取成功不为null + */ + public VersioningConfiguration getBucketVersioning(String bucketName) { + VersioningConfiguration configuration = null; + + try { + configuration = minioClient.getBucketVersioning(GetBucketVersioningArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶的版本控制配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶的版本控制配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return configuration; + } + + /** + * 获取存储桶中的对象锁配置 + * + * @param bucketName 桶的名称 + * @return Tags 获取成功不为null + */ + public ObjectLockConfiguration getObjectLockConfiguration(String bucketName) { + ObjectLockConfiguration configuration = null; + try { + configuration = minioClient.getObjectLockConfiguration(GetObjectLockConfigurationArgs.builder().bucket(bucketName).build()); + log.info("获取存储桶中的对象锁配置 ------ 成功"); + } catch (Exception exception) { + log.error("获取存储桶中的对象锁配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.GET_BUCKET_EXCEPTION); + } + + return configuration; + } + + /** + * 列出所有桶的桶信息 + * + * @return Tags 获取成功不为null + */ + public List listBuckets() { + List buckets = null; + + try { + buckets = minioClient.listBuckets(); + log.info("列出所有桶的桶信息 ------ 成功"); + } catch (Exception exception) { + log.error("列出所有桶的桶信息 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.QUERY_BUCKET_EXCEPTION); + } + + return buckets; + } + + /** + * 创建桶 + * + * @param bucketName 桶的名称 + * @return boolean 返回是否创建成功 + */ + public boolean makeBucket(String bucketName) { + try { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + log.info("创建桶 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("创建桶 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.CREATE_BUCKET_EXCEPTION); + } + } + + /** + * 创建桶 + * + * @param bucketName 桶的名称 + * @return boolean 返回是否创建成功 + */ + public boolean makeBucket(String bucketName, String region) { + try { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region(region).build()); + log.info("创建桶 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("创建桶 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.CREATE_BUCKET_EXCEPTION); + } + } + + /** + * 创建桶 + * + * @param bucketName 桶的名称 + * @return boolean 返回是否创建成功 + */ + public boolean makeBucket(String bucketName, String region, boolean objectLock) { + try { + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region(region).objectLock(objectLock).build()); + log.info("创建桶 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("创建桶 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.CREATE_BUCKET_EXCEPTION); + } + } + + /** + * 删除桶 + * + * @param bucketName 桶的名称 + * @return boolean 是否删除成功 + */ + public boolean removeBucket(String bucketName) { + try { + minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); + log.info("删除桶 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("删除桶 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.DELETE_BUCKET_EXCEPTION); + } + } + + /** + * 设置桶的加密配置 + * + * @param bucketName 同名称 + * @param configuration 配置信息 + * @return 布尔值是否完成 + */ + public boolean setBucketEncryption(String bucketName, SseConfiguration configuration) { + try { + minioClient.setBucketEncryption(SetBucketEncryptionArgs.builder() + .bucket(bucketName) + .config(configuration) + .build()); + log.info("设置桶的加密配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("设置桶的加密配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.UPDATE_BUCKET_EXCEPTION); + } + } + + /** + * 设置桶策略 + * + * @param bucketName 同名称 + * @param policyJson 配置信息 + * @return 布尔值是否完成 + */ + public boolean setBucketPolicy(String bucketName, String policyJson) { + try { + minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policyJson).build()); + log.info("查看桶策略 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("查看桶策略 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.UPDATE_BUCKET_EXCEPTION); + } + } + + /** + * 设置桶复制参数 + * + * @param bucketName 同名称 + * @param config 配置信息 + * @return 布尔值是否完成 + */ + public boolean setBucketReplication(String bucketName, ReplicationConfiguration config) { + try { + minioClient.setBucketReplication(SetBucketReplicationArgs.builder().bucket(bucketName).config(config).build()); + log.info("设置桶复制参数 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("设置桶复制参数 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.UPDATE_BUCKET_EXCEPTION); + } + } + + /** + * 设置/修改桶标签 + * + * @param bucketName 桶名称 + * @param map Map map集合 + * @return 布尔值是否完成 + */ + public boolean setBucketTags(String bucketName, Map map) { + try { + minioClient.setBucketTags(SetBucketTagsArgs.builder().bucket(bucketName).tags(map).build()); + log.info("设置/修改桶标签 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("设置/修改桶标签 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.UPDATE_BUCKET_EXCEPTION); + } + } + + /** + * 设置桶的版本配置 + * + * @param bucketName 同名称 + * @param configuration VersioningConfiguration 配置信息 + * @return 布尔值是否完成 + */ + public boolean setBucketVersioning(String bucketName, VersioningConfiguration configuration) { + try { + minioClient.setBucketVersioning( + SetBucketVersioningArgs.builder().bucket(bucketName).config(configuration).build()); + log.info("设置桶的版本配置 ------ 成功"); + return true; + } catch (Exception exception) { + log.error("设置桶的版本配置 ------ 失败消息:{}", exception.getLocalizedMessage()); + exception.getStackTrace(); + throw new BunnyException(MinioMessageConstant.UPDATE_BUCKET_EXCEPTION); + } + } + + // TODO setObjectLockConfiguration + // TODO https://min.io/docs/minio/linux/developers/java/API.html#getBucketTags + + /** + * 复制另一个桶中文件 + * + * @param bucketName 桶中名字 + * @param objectName 复制名称 + * @param sourceBucketName 复制到桶中名称 + * @param sourceObjectName 复制完成后名称 + * @return 布尔值是否完成 + */ + public boolean copyObject(String bucketName, String objectName, String sourceBucketName, String sourceObjectName) { + boolean isComplete = false; + + try { + minioClient.copyObject( + CopyObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .source( + CopySource.builder() + .bucket(sourceBucketName) + .object(sourceObjectName) + .build()) + .build()); + isComplete = true; + } catch (Exception exception) { + exception.printStackTrace(); + } + + return isComplete; + } + + /** + * 删除对象的标记。 + * + * @param bucketName 桶中名字 + * @param objectName 复制名称 + * @return 布尔值是否完成 + */ + public boolean deleteObjectTags(String bucketName, String objectName) { + boolean isComplete = false; + + try { + minioClient.deleteObjectTags(DeleteObjectTagsArgs.builder().bucket(bucketName).object(objectName).build()); + isComplete = true; + } catch (Exception exception) { + exception.printStackTrace(); + } + + return isComplete; + } + + /** + * 下载文件 + * + * @param bucketName 桶名称 + * @param objectName 对象名称 + * @param filename 文件名 + */ + public void downloadObject(String bucketName, String objectName, String filename) { + try { + minioClient.downloadObject( + DownloadObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .filename(filename) + .build()); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + + /** + * 获取文件 + * + * @param bucketName 桶名称 + * @param objectName 对象名称 + */ + public InputStream getObject(String bucketName, String objectName) { + try { + return minioClient.getObject(GetObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .build()); + } catch (Exception exception) { + exception.printStackTrace(); + return null; // 或者抛出自定义异常 + } + } + + /** + * 上传文件 + * + * @param bucketName 桶名称 + * @param filename 文件名 + * @param inputStream 输入流 + * @param size 大小 + */ + public void putObject(String bucketName, String filename, InputStream inputStream, Long size) { + try { + minioClient.putObject( + PutObjectArgs.builder().bucket(bucketName) + .object(filename) + .stream(inputStream, size, -1) + .build()); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + + + /** + * 上传文件 + * + * @param bucketName 桶名称 + * @param filename 文件名 + * @param contentType 文件类型 + * @param inputStream 输入流 + * @param size 大小 + */ + public void putObject(String bucketName, String filename, String contentType, InputStream inputStream, Long size) { + try { + minioClient.putObject( + PutObjectArgs.builder().bucket(bucketName) + .object(filename) + .stream(inputStream, size, -1) + .contentType(contentType) + .build()); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + + /** + * 删除文件 + * + * @param bucketName 桶名称 + * @param objectName 对象名称 + */ + public void removeObject(String bucketName, String objectName) { + try { + minioClient.removeObject( + RemoveObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .build()); + } catch (Exception exception) { + exception.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/service/pom.xml b/service/pom.xml index 49cd167..980dab7 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -30,6 +30,11 @@ module-mail 0.0.1-SNAPSHOT + + cn.bunny + module-minio + 0.0.1-SNAPSHOT + cn.bunny diff --git a/service/src/main/java/cn/bunny/service/security/CustomUserDetailsService.java b/service/src/main/java/cn/bunny/service/security/CustomUserDetailsService.java index e425f44..b4d248b 100644 --- a/service/src/main/java/cn/bunny/service/security/CustomUserDetailsService.java +++ b/service/src/main/java/cn/bunny/service/security/CustomUserDetailsService.java @@ -1,6 +1,6 @@ package cn.bunny.service.security; -import cn.bunny.common.constant.MessageConstant; +import cn.bunny.common.constant.CommonMessageConstant; import cn.bunny.common.service.exception.BunnyException; import cn.bunny.entity.system.SysRole; import cn.bunny.entity.system.SysUser; @@ -28,11 +28,11 @@ public class CustomUserDetailsService implements cn.bunny.security.service.Custo SysUser sysUser = sysUserMapper.selectOne(Wrappers.lambdaQuery().eq(SysUser::getUsername, username)); List sysRoleList = sysRoleMapper.selectList(null); if (sysUser == null) { - throw new UsernameNotFoundException(MessageConstant.USER_DOES_NOT_EXIST); + throw new UsernameNotFoundException(CommonMessageConstant.USER_DOES_NOT_EXIST); } if (sysUser.getStatus() == 0) { - throw new BunnyException(MessageConstant.ACCOUNT_LOCKED); + throw new BunnyException(CommonMessageConstant.ACCOUNT_LOCKED); } List roleAuthoritieList = sysRoleList.stream().map(SysRole::getRoleCode).toList(); diff --git a/service/src/main/java/cn/bunny/service/service/impl/SysUserServiceImpl.java b/service/src/main/java/cn/bunny/service/service/impl/SysUserServiceImpl.java index ac017cf..73897ae 100644 --- a/service/src/main/java/cn/bunny/service/service/impl/SysUserServiceImpl.java +++ b/service/src/main/java/cn/bunny/service/service/impl/SysUserServiceImpl.java @@ -1,6 +1,6 @@ package cn.bunny.service.service.impl; -import cn.bunny.common.constant.MessageConstant; +import cn.bunny.common.constant.CommonMessageConstant; import cn.bunny.common.service.exception.BunnyException; import cn.bunny.common.service.utils.JwtHelper; import cn.bunny.entity.system.Login; @@ -48,18 +48,18 @@ public class SysUserServiceImpl extends ServiceImpl impl // 用户是否存在 if (sysUser == null) { - throw new BunnyException(MessageConstant.ACCOUNT_NOT_FOUND); + throw new BunnyException(CommonMessageConstant.ACCOUNT_NOT_FOUND); } // 判断是否被禁用 if (sysUser.getStatus() == 0) { - throw new BunnyException(MessageConstant.ACCOUNT_LOCKED); + throw new BunnyException(CommonMessageConstant.ACCOUNT_LOCKED); } // 判断密码 String md5DigestAsHexPassword = DigestUtils.md5DigestAsHex(password.getBytes()); if (!md5DigestAsHexPassword.equals(sysUser.getPassword())) { - throw new BunnyException(MessageConstant.PASSWORD_ERROR); + throw new BunnyException(CommonMessageConstant.PASSWORD_ERROR); } // 添加token String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername()); diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml index 8921738..d92e080 100644 --- a/service/src/main/resources/application.yml +++ b/service/src/main/resources/application.yml @@ -30,10 +30,6 @@ spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 - security: - user: - name: user - password: 1 mybatis-plus: mapper-locations: classpath:mapper/*.xml @@ -50,18 +46,18 @@ logging: file: path: "logs/${spring.application.name}" -snowflake: - #数据中心id位数 - datacenterBits: 5 - # 机器id位数 - workerBits: 5 - # 序列id所占位数 - sequenceBits: 12 - # 数据中心id,范围0-2^5-1 - datacenterId: 1 - # 机器id,范围0-2^5-1 - workerId: 1 - # 时间戳起始点(2024-01-01 00::00:00 的毫秒数) - twepoch: 1704038400000 - #单次批量生成id的最大数量 默认10万 - maxBatchCount: 100000 \ No newline at end of file +bunny: + minio: + endpointUrl: ${bunny.minio.endpointUrl} + accessKey: ${bunny.minio.accessKey} + secretKey: ${bunny.minio.secretKey} + bucket-name: ${bunny.minio.bucket-name} + + snowflake: + datacenterBits: 5 # 数据中心id位数 + workerBits: 5 # 机器id位数 + sequenceBits: 12 # 序列id所占位数 + datacenterId: 1 # 数据中心id,范围0-2^5-1 + workerId: 1 # 机器id,范围0-2^5-1 + twepoch: 1704038400000 # 时间戳起始点(2024-01-01 00::00:00 的毫秒数) + maxBatchCount: 100000 #单次批量生成id的最大数量 默认10万 \ No newline at end of file