refactor: 优化部分代码

1.修复 Sonar 扫描问题
2.使用常量替代部分魔法值
This commit is contained in:
2024-01-27 23:10:27 +08:00
parent ef0e99de83
commit 75fe5377df
9 changed files with 102 additions and 61 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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 @@ import java.util.concurrent.TimeUnit;
@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 {

View File

@@ -20,6 +20,7 @@ import io.swagger.v3.oas.models.Components;
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 @@ import org.springframework.boot.context.properties.NestedConfigurationProperty;
* @since 1.0.1
*/
@Data
@ConfigurationProperties(prefix = "springdoc")
@ConfigurationProperties(prefix = PropertiesConstants.SPRINGDOC)
public class SpringDocExtensionProperties {
/**

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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 @@ import top.charles7c.continew.starter.auth.justauth.core.JustAuthStateCacheRedis
*/
@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.");

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
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 @@ import top.charles7c.continew.starter.core.util.GeneralPropertySourceFactory;
@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 {

View File

@@ -24,6 +24,7 @@ import lombok.RequiredArgsConstructor;
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.condition.ConditionalOnProperty;
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 @@ import java.util.List;
@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 class RedissonAutoConfiguration {
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<String> 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<String> 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<String> 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<String> 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());
}
}
}

View File

@@ -42,6 +42,16 @@ public class 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 配置
*/

View File

@@ -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 @@ import java.lang.annotation.*;
@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 {
}

View File

@@ -35,6 +35,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
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 @@ import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermi
@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 {

View File

@@ -100,6 +100,7 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
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;