Skip to content

Commit

Permalink
refactor: 优化部分代码
Browse files Browse the repository at this point in the history
1.修复 Sonar 扫描问题
2.使用常量替代部分魔法值
  • Loading branch information
Charles7c committed Jan 27, 2024
1 parent ef0e99d commit 75fe537
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 文档扩展配置属性
Expand All @@ -28,7 +29,7 @@
* @since 1.0.1
*/
@Data
@ConfigurationProperties(prefix = "springdoc")
@ConfigurationProperties(prefix = PropertiesConstants.SPRINGDOC)
public class SpringDocExtensionProperties {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 自动配置
Expand All @@ -34,15 +35,15 @@
*/
@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 {

/**
* 自定义 State 缓存实现
*/
@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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand All @@ -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<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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 配置
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 75fe537

Please sign in to comment.