From 75fe5377df1810748c20ec2e347b7bd72d78189b Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 27 Jan 2024 23:10:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.修复 Sonar 扫描问题 2.使用常量替代部分魔法值 --- .../SpringDocAutoConfiguration.java | 3 +- .../SpringDocExtensionProperties.java | 3 +- .../JustAuthAutoConfiguration.java | 5 +- .../SaTokenAutoConfiguration.java | 3 +- .../RedissonAutoConfiguration.java | 132 +++++++++++------- .../core/constant/PropertiesConstants.java | 10 ++ .../ConditionalOnEnabledDataPermission.java | 3 +- .../MybatisPlusAutoConfiguration.java | 3 +- .../DataPermissionHandlerImpl.java | 1 + 9 files changed, 102 insertions(+), 61 deletions(-) diff --git a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java index cc01b7d8..d12c6ee3 100644 --- a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java +++ b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java @@ -38,6 +38,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; import top.charles7c.continew.starter.core.util.GeneralPropertySourceFactory; import java.util.List; @@ -53,7 +54,7 @@ @Slf4j @EnableWebMvc @AutoConfiguration -@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true") +@ConditionalOnProperty(prefix = PropertiesConstants.SPRINGDOC_SWAGGER_UI, name = PropertiesConstants.ENABLED, havingValue = "true") @EnableConfigurationProperties(SpringDocExtensionProperties.class) @PropertySource(value = "classpath:default-api-doc.yml", factory = GeneralPropertySourceFactory.class) public class SpringDocAutoConfiguration implements WebMvcConfigurer { diff --git a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocExtensionProperties.java b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocExtensionProperties.java index a1977b4b..bd0ddb46 100644 --- a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocExtensionProperties.java +++ b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocExtensionProperties.java @@ -20,6 +20,7 @@ import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; /** * API 文档扩展配置属性 @@ -28,7 +29,7 @@ * @since 1.0.1 */ @Data -@ConfigurationProperties(prefix = "springdoc") +@ConfigurationProperties(prefix = PropertiesConstants.SPRINGDOC) public class SpringDocExtensionProperties { /** diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java index 6a1c570d..716378c8 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import top.charles7c.continew.starter.auth.justauth.core.JustAuthStateCacheRedisImpl; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; /** * JustAuth 自动配置 @@ -34,7 +35,7 @@ */ @Slf4j @AutoConfiguration(before = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class) -@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(prefix = "justauth", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) public class JustAuthAutoConfiguration { /** @@ -42,7 +43,7 @@ public class JustAuthAutoConfiguration { */ @Bean @ConditionalOnClass(RedisClient.class) - @ConditionalOnProperty(prefix = "justauth.cache", value = "type", havingValue = "redis") + @ConditionalOnProperty(prefix = "justauth.cache", name = "type", havingValue = "redis") public AuthStateCache authStateCache() { JustAuthStateCacheRedisImpl impl = new JustAuthStateCacheRedisImpl(); log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Redis' completed initialization."); diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java index 79257a36..3738ce3f 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenAutoConfiguration.java @@ -32,6 +32,7 @@ import org.springframework.context.annotation.*; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; import top.charles7c.continew.starter.core.constant.StringConstants; import top.charles7c.continew.starter.core.util.GeneralPropertySourceFactory; @@ -46,7 +47,7 @@ @RequiredArgsConstructor @ComponentScan("top.charles7c.continew.starter.auth.satoken.exception") @EnableConfigurationProperties(SaTokenExtensionProperties.class) -@ConditionalOnProperty(prefix = "sa-token.extension", name = "enabled", havingValue = "true") +@ConditionalOnProperty(prefix = "sa-token.extension", name = PropertiesConstants.ENABLED, havingValue = "true") @PropertySource(value = "classpath:default-auth-satoken.yml", factory = GeneralPropertySourceFactory.class) public class SaTokenAutoConfiguration implements WebMvcConfigurer { diff --git a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/autoconfigure/RedissonAutoConfiguration.java b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/autoconfigure/RedissonAutoConfiguration.java index 608c3bf4..e61fb2cd 100644 --- a/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/autoconfigure/RedissonAutoConfiguration.java +++ b/continew-starter-cache/continew-starter-cache-redisson/src/main/java/top/charles7c/continew/starter/cache/redisson/autoconfigure/RedissonAutoConfiguration.java @@ -24,6 +24,7 @@ import lombok.extern.slf4j.Slf4j; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.ClusterServersConfig; +import org.redisson.config.Config; import org.redisson.config.SentinelServersConfig; import org.redisson.config.SingleServerConfig; import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; @@ -32,6 +33,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; import top.charles7c.continew.starter.core.constant.StringConstants; import java.util.List; @@ -46,7 +48,7 @@ @Slf4j @AutoConfiguration @RequiredArgsConstructor -@ConditionalOnProperty(prefix = "spring.data.redisson", name = "enabled", havingValue = "true") +@ConditionalOnProperty(prefix = "spring.data.redisson", name = PropertiesConstants.ENABLED, havingValue = "true") @EnableConfigurationProperties(RedissonProperties.class) public class RedissonAutoConfiguration { @@ -60,63 +62,85 @@ public RedissonAutoConfigurationCustomizer redissonAutoConfigurationCustomizer() RedissonProperties.Mode mode = properties.getMode(); String protocol = redisProperties.getSsl().isEnabled() ? "rediss://" : "redis://"; switch (mode) { - case CLUSTER -> { - ClusterServersConfig clusterServersConfig = config.useClusterServers(); - ClusterServersConfig customClusterServersConfig = properties.getClusterServersConfig(); - if (null != customClusterServersConfig) { - BeanUtil.copyProperties(customClusterServersConfig, clusterServersConfig); - clusterServersConfig.setNodeAddresses(customClusterServersConfig.getNodeAddresses()); - } - // 下方配置如果为空,则使用 Redis 的配置 - if (CollUtil.isEmpty(clusterServersConfig.getNodeAddresses())) { - List nodeList = redisProperties.getCluster().getNodes(); - nodeList.stream().map(node -> protocol + node).forEach(clusterServersConfig::addNodeAddress); - } - if (StrUtil.isBlank(clusterServersConfig.getPassword())) { - clusterServersConfig.setPassword(redisProperties.getPassword()); - } - } - case SENTINEL -> { - SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); - SentinelServersConfig customSentinelServersConfig = properties.getSentinelServersConfig(); - if (null != customSentinelServersConfig) { - BeanUtil.copyProperties(customSentinelServersConfig, sentinelServersConfig); - sentinelServersConfig.setSentinelAddresses(customSentinelServersConfig.getSentinelAddresses()); - } - // 下方配置如果为空,则使用 Redis 的配置 - if (CollUtil.isEmpty(sentinelServersConfig.getSentinelAddresses())) { - List nodeList = redisProperties.getSentinel().getNodes(); - nodeList.stream() - .map(node -> protocol + node) - .forEach(sentinelServersConfig::addSentinelAddress); - } - if (StrUtil.isBlank(sentinelServersConfig.getPassword())) { - sentinelServersConfig.setPassword(redisProperties.getPassword()); - } - if (StrUtil.isBlank(sentinelServersConfig.getMasterName())) { - sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster()); - } - } - default -> { - SingleServerConfig singleServerConfig = config.useSingleServer(); - SingleServerConfig customSingleServerConfig = properties.getSingleServerConfig(); - if (null != customSingleServerConfig) { - BeanUtil.copyProperties(properties.getSingleServerConfig(), singleServerConfig); - } - // 下方配置如果为空,则使用 Redis 的配置 - singleServerConfig.setDatabase(redisProperties.getDatabase()); - if (StrUtil.isBlank(singleServerConfig.getPassword())) { - singleServerConfig.setPassword(redisProperties.getPassword()); - } - if (StrUtil.isBlank(singleServerConfig.getAddress())) { - singleServerConfig.setAddress(protocol + redisProperties - .getHost() + StringConstants.COLON + redisProperties.getPort()); - } - } + case CLUSTER -> this.buildClusterModeConfig(config, protocol); + case SENTINEL -> this.buildSentinelModeConfig(config, protocol); + default -> this.buildSingleModeConfig(config, protocol); } // Jackson 处理 config.setCodec(new JsonJacksonCodec(objectMapper)); log.debug("[ContiNew Starter] - Auto Configuration 'Redisson' completed initialization."); }; } + + /** + * 构建集群模式配置 + * + * @param config 配置 + * @param protocol 协议 + */ + private void buildClusterModeConfig(Config config, String protocol) { + ClusterServersConfig clusterServersConfig = config.useClusterServers(); + ClusterServersConfig customClusterServersConfig = properties.getClusterServersConfig(); + if (null != customClusterServersConfig) { + BeanUtil.copyProperties(customClusterServersConfig, clusterServersConfig); + clusterServersConfig.setNodeAddresses(customClusterServersConfig.getNodeAddresses()); + } + // 下方配置如果为空,则使用 Redis 的配置 + if (CollUtil.isEmpty(clusterServersConfig.getNodeAddresses())) { + List nodeList = redisProperties.getCluster().getNodes(); + nodeList.stream().map(node -> protocol + node).forEach(clusterServersConfig::addNodeAddress); + } + if (StrUtil.isBlank(clusterServersConfig.getPassword())) { + clusterServersConfig.setPassword(redisProperties.getPassword()); + } + } + + /** + * 构建哨兵模式配置 + * + * @param config 配置 + * @param protocol 协议 + */ + private void buildSentinelModeConfig(Config config, String protocol) { + SentinelServersConfig sentinelServersConfig = config.useSentinelServers(); + SentinelServersConfig customSentinelServersConfig = properties.getSentinelServersConfig(); + if (null != customSentinelServersConfig) { + BeanUtil.copyProperties(customSentinelServersConfig, sentinelServersConfig); + sentinelServersConfig.setSentinelAddresses(customSentinelServersConfig.getSentinelAddresses()); + } + // 下方配置如果为空,则使用 Redis 的配置 + if (CollUtil.isEmpty(sentinelServersConfig.getSentinelAddresses())) { + List nodeList = redisProperties.getSentinel().getNodes(); + nodeList.stream().map(node -> protocol + node).forEach(sentinelServersConfig::addSentinelAddress); + } + if (StrUtil.isBlank(sentinelServersConfig.getPassword())) { + sentinelServersConfig.setPassword(redisProperties.getPassword()); + } + if (StrUtil.isBlank(sentinelServersConfig.getMasterName())) { + sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster()); + } + } + + /** + * 构建单机模式配置 + * + * @param config 配置 + * @param protocol 协议 + */ + private void buildSingleModeConfig(Config config, String protocol) { + SingleServerConfig singleServerConfig = config.useSingleServer(); + SingleServerConfig customSingleServerConfig = properties.getSingleServerConfig(); + if (null != customSingleServerConfig) { + BeanUtil.copyProperties(properties.getSingleServerConfig(), singleServerConfig); + } + // 下方配置如果为空,则使用 Redis 的配置 + singleServerConfig.setDatabase(redisProperties.getDatabase()); + if (StrUtil.isBlank(singleServerConfig.getPassword())) { + singleServerConfig.setPassword(redisProperties.getPassword()); + } + if (StrUtil.isBlank(singleServerConfig.getAddress())) { + singleServerConfig.setAddress(protocol + redisProperties.getHost() + StringConstants.COLON + redisProperties + .getPort()); + } + } } diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/PropertiesConstants.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/PropertiesConstants.java index 6c02be19..2521b321 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/PropertiesConstants.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/constant/PropertiesConstants.java @@ -42,6 +42,16 @@ private PropertiesConstants() { */ public static final String THREAD_POOL = CONTINEW_STARTER + ".thread-pool"; + /** + * Spring Doc 配置 + */ + public static final String SPRINGDOC = "springdoc"; + + /** + * Spring Doc Swagger UI 配置 + */ + public static final String SPRINGDOC_SWAGGER_UI = SPRINGDOC + ".swagger-ui"; + /** * Web 配置 */ diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/ConditionalOnEnabledDataPermission.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/ConditionalOnEnabledDataPermission.java index 49a4bdc7..d4a89d67 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/ConditionalOnEnabledDataPermission.java +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/ConditionalOnEnabledDataPermission.java @@ -17,6 +17,7 @@ package top.charles7c.continew.starter.data.mybatis.plus.autoconfigure; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; import java.lang.annotation.*; @@ -29,6 +30,6 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented -@ConditionalOnProperty(prefix = "mybatis-plus.extension.data-permission", name = "enabled", havingValue = "true") +@ConditionalOnProperty(prefix = "mybatis-plus.extension.data-permission", name = PropertiesConstants.ENABLED, havingValue = "true") public @interface ConditionalOnEnabledDataPermission { } \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java index 1dc2844c..2a82ba21 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java @@ -35,6 +35,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.annotation.EnableTransactionManagement; +import top.charles7c.continew.starter.core.constant.PropertiesConstants; import top.charles7c.continew.starter.core.util.GeneralPropertySourceFactory; import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionFilter; import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionHandlerImpl; @@ -50,7 +51,7 @@ @MapperScan("${mybatis-plus.extension.mapper-package}") @EnableTransactionManagement(proxyTargetClass = true) @EnableConfigurationProperties(MyBatisPlusExtensionProperties.class) -@ConditionalOnProperty(prefix = "mybatis-plus.extension", name = "enabled", havingValue = "true") +@ConditionalOnProperty(prefix = "mybatis-plus.extension", name = PropertiesConstants.ENABLED, havingValue = "true") @PropertySource(value = "classpath:default-data-mybatis-plus.yml", factory = GeneralPropertySourceFactory.class) public class MybatisPlusAutoConfiguration { diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/datapermission/DataPermissionHandlerImpl.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/datapermission/DataPermissionHandlerImpl.java index 15147025..d340577d 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/datapermission/DataPermissionHandlerImpl.java +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/datapermission/DataPermissionHandlerImpl.java @@ -100,6 +100,7 @@ private Expression buildDataScopeFilter(DataPermission dataPermission, Expressio case DEPT -> expression = this.buildDeptExpression(dataPermission, currentUser, expression); case SELF -> expression = this.buildSelfExpression(dataPermission, currentUser, expression); case CUSTOM -> expression = this.buildCustomExpression(dataPermission, role, expression); + default -> throw new IllegalArgumentException(String.format("暂不支持 [%s] 数据权限", dataScope)); } } return null != where ? new AndExpression(where, new Parenthesis(expression)) : expression;