From 8598e6d109c1ca6be3e973ceb41c6dd7bd93c333 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Fri, 2 Feb 2024 20:44:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(captcha/behavior):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=A1=8C=E4=B8=BA=E9=AA=8C=E8=AF=81=E7=A0=81=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/SaTokenDaoConfiguration.java | 2 +- .../auth/satoken/enums/SaTokenDaoType.java | 2 +- .../BehaviorCaptchaAutoConfiguration.java | 8 ++- .../BehaviorCaptchaProperties.java | 20 +------ .../BehaviorCaptchaCacheConfiguration.java | 55 +++++++++++++------ .../BehaviorCaptchaCacheServiceImpl.java | 2 +- .../captcha/behavior/enums/StorageType.java | 4 +- ...obalExceptionHandlerAutoConfiguration.java | 2 +- 8 files changed, 52 insertions(+), 43 deletions(-) rename continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/{ => cache}/BehaviorCaptchaCacheConfiguration.java (64%) rename continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/{core => autoconfigure/cache}/BehaviorCaptchaCacheServiceImpl.java (95%) diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/dao/SaTokenDaoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/dao/SaTokenDaoConfiguration.java index b4e13039..e7f6782b 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/dao/SaTokenDaoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/dao/SaTokenDaoConfiguration.java @@ -44,7 +44,7 @@ public class SaTokenDaoConfiguration { } /** - * 自定义持久层实现-默认 + * 自定义持久层实现-默认(内存) */ @ConditionalOnMissingBean(SaTokenDao.class) @ConditionalOnClass(RedisClient.class) diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoType.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoType.java index 08f7aed9..81ae25d8 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoType.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/enums/SaTokenDaoType.java @@ -25,7 +25,7 @@ package top.charles7c.continew.starter.auth.satoken.enums; public enum SaTokenDaoType { /** - * 默认 + * 默认(内存) */ DEFAULT, diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java index 425293bf..db36f074 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java @@ -36,6 +36,7 @@ import org.springframework.context.annotation.Import; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; +import top.charles7c.continew.starter.captcha.behavior.autoconfigure.cache.BehaviorCaptchaCacheConfiguration; import top.charles7c.continew.starter.core.constant.PropertiesConstants; import java.util.HashMap; @@ -49,8 +50,8 @@ import java.util.Properties; * @since 1.1.0 */ @AutoConfiguration -@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true") @EnableConfigurationProperties(BehaviorCaptchaProperties.class) +@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true") public class BehaviorCaptchaAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaAutoConfiguration.class); @@ -64,7 +65,8 @@ public class BehaviorCaptchaAutoConfiguration { * 自定义缓存实现配置 */ @Configuration - @Import({BehaviorCaptchaCacheConfiguration.Redis.class, BehaviorCaptchaCacheConfiguration.Custom.class}) + @Import({BehaviorCaptchaCacheConfiguration.Default.class, BehaviorCaptchaCacheConfiguration.Redis.class, + BehaviorCaptchaCacheConfiguration.Custom.class}) protected static class BehaviorCaptchaCacheAutoConfiguration {} /** @@ -140,6 +142,6 @@ public class BehaviorCaptchaAutoConfiguration { @PostConstruct public void postConstruct() { - log.debug("[ContiNew Starter] - Auto Configuration 'Behavior Captcha' completed initialization."); + log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior' completed initialization."); } } diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java index 4763aee3..655a0865 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java @@ -17,7 +17,6 @@ package top.charles7c.continew.starter.captcha.behavior.autoconfigure; import com.anji.captcha.model.common.CaptchaTypeEnum; -import com.anji.captcha.service.CaptchaCacheService; import org.springframework.boot.context.properties.ConfigurationProperties; import top.charles7c.continew.starter.captcha.behavior.enums.StorageType; import top.charles7c.continew.starter.core.constant.PropertiesConstants; @@ -49,14 +48,9 @@ public class BehaviorCaptchaProperties { private CaptchaTypeEnum type = CaptchaTypeEnum.BLOCKPUZZLE; /** - * 缓存类型(默认:LOCAL 内存) + * 缓存类型 */ - private StorageType cacheType = StorageType.LOCAL; - - /** - * 自定义缓存类型(当 cacheType 为 CUSTOM 时必填) - */ - private Class cacheImpl; + private StorageType cacheType = StorageType.DEFAULT; /** * 滑动拼图底图路径(为空则使用默认底图)(路径下需要有两个文件夹,分别为 original(存放底图)slidingBlock(存放滑块)) @@ -180,14 +174,6 @@ public class BehaviorCaptchaProperties { this.cacheType = cacheType; } - public Class getCacheImpl() { - return cacheImpl; - } - - public void setCacheImpl(Class cacheImpl) { - this.cacheImpl = cacheImpl; - } - public String getJigsawBaseMapPath() { return jigsawBaseMapPath; } @@ -334,6 +320,6 @@ public class BehaviorCaptchaProperties { @Override public String toString() { - return "BehaviorCaptchaProperties{" + "enabled=" + enabled + ", enableAes=" + enableAes + ", type=" + type + ", cacheType=" + cacheType + ", cacheImpl=" + cacheImpl + ", jigsawBaseMapPath='" + jigsawBaseMapPath + '\'' + ", slipOffset='" + slipOffset + '\'' + ", picClickBaseMapPath='" + picClickBaseMapPath + '\'' + ", fontType='" + fontType + '\'' + ", historyDataClearEnable=" + historyDataClearEnable + ", reqFrequencyLimitEnable=" + reqFrequencyLimitEnable + ", reqGetLockLimit=" + reqGetLockLimit + ", reqGetLockSeconds=" + reqGetLockSeconds + ", reqGetMinuteLimit=" + reqGetMinuteLimit + ", reqCheckMinuteLimit=" + reqCheckMinuteLimit + ", reqVerifyMinuteLimit=" + reqVerifyMinuteLimit + ", cacheNumber='" + cacheNumber + '\'' + ", timingClear='" + timingClear + '\'' + ", waterMark='" + waterMark + '\'' + ", waterFont='" + waterFont + '\'' + ", interferenceOptions='" + interferenceOptions + '\'' + ", fontStyle=" + fontStyle + ", fontSize=" + fontSize + '}'; + return "BehaviorCaptchaProperties{" + "enabled=" + enabled + ", enableAes=" + enableAes + ", type=" + type + ", cacheType=" + cacheType + ", jigsawBaseMapPath='" + jigsawBaseMapPath + '\'' + ", slipOffset='" + slipOffset + '\'' + ", picClickBaseMapPath='" + picClickBaseMapPath + '\'' + ", fontType='" + fontType + '\'' + ", historyDataClearEnable=" + historyDataClearEnable + ", reqFrequencyLimitEnable=" + reqFrequencyLimitEnable + ", reqGetLockLimit=" + reqGetLockLimit + ", reqGetLockSeconds=" + reqGetLockSeconds + ", reqGetMinuteLimit=" + reqGetMinuteLimit + ", reqCheckMinuteLimit=" + reqCheckMinuteLimit + ", reqVerifyMinuteLimit=" + reqVerifyMinuteLimit + ", cacheNumber='" + cacheNumber + '\'' + ", timingClear='" + timingClear + '\'' + ", waterMark='" + waterMark + '\'' + ", waterFont='" + waterFont + '\'' + ", interferenceOptions='" + interferenceOptions + '\'' + ", fontStyle=" + fontStyle + ", fontSize=" + fontSize + '}'; } } diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaCacheConfiguration.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java similarity index 64% rename from continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaCacheConfiguration.java rename to continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java index c12b2cca..8d6e4a6c 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaCacheConfiguration.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java @@ -14,33 +14,35 @@ * limitations under the License. */ -package top.charles7c.continew.starter.captcha.behavior.autoconfigure; +package top.charles7c.continew.starter.captcha.behavior.autoconfigure.cache; -import cn.hutool.core.util.ReflectUtil; import cn.hutool.extra.spring.SpringUtil; import com.anji.captcha.service.CaptchaCacheService; +import com.anji.captcha.service.impl.CaptchaCacheServiceMemImpl; import com.anji.captcha.service.impl.CaptchaServiceFactory; import jakarta.annotation.PostConstruct; import org.redisson.client.RedisClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; +import org.springframework.core.ResolvableType; import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration; import top.charles7c.continew.starter.captcha.behavior.enums.StorageType; -import top.charles7c.continew.starter.captcha.behavior.core.BehaviorCaptchaCacheServiceImpl; import top.charles7c.continew.starter.core.constant.PropertiesConstants; /** * 行为验证码缓存配置 * * @author Bull-BCLS + * @author Charles7c * @since 1.1.0 */ -abstract class BehaviorCaptchaCacheConfiguration { +public class BehaviorCaptchaCacheConfiguration { private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaCacheConfiguration.class); @@ -48,36 +50,55 @@ abstract class BehaviorCaptchaCacheConfiguration { } /** - * 自定义缓存实现类-Redis + * 自定义缓存实现-默认(内存) */ - @ConditionalOnClass(RedisClient.class) - @AutoConfigureBefore(RedissonAutoConfiguration.class) - @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis") - static class Redis { + @ConditionalOnMissingBean(CaptchaCacheService.class) + @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "default", matchIfMissing = true) + public static class Default { @PostConstruct public void postConstruct() { - CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name() - .toLowerCase(), new BehaviorCaptchaCacheServiceImpl()); - log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Redis' completed initialization."); + CaptchaServiceFactory.cacheService.put(StorageType.DEFAULT.name() + .toLowerCase(), new CaptchaCacheServiceMemImpl()); + log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior-Cache-Default' completed initialization."); } } /** - * 自定义缓存实现类-自定义 + * 自定义缓存实现-Redis + */ + @ConditionalOnMissingBean(CaptchaCacheService.class) + @ConditionalOnClass(RedisClient.class) + @AutoConfigureBefore(RedissonAutoConfiguration.class) + @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis") + public static class Redis { + @PostConstruct + public void postConstruct() { + CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name() + .toLowerCase(), new BehaviorCaptchaCacheServiceImpl()); + log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior-Cache-Redis' completed initialization."); + } + } + + /** + * 自定义缓存实现 */ @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "custom") - static class Custom { + public static class Custom { @Bean @ConditionalOnMissingBean - public CaptchaCacheService captchaCacheService(BehaviorCaptchaProperties properties) { - return ReflectUtil.newInstance(properties.getCacheImpl()); + public CaptchaCacheService captchaCacheService() { + if (log.isErrorEnabled()) { + log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType + .forClass(CaptchaCacheService.class)); + } + throw new NoSuchBeanDefinitionException(CaptchaCacheService.class); } @PostConstruct public void postConstruct() { CaptchaServiceFactory.cacheService.put(StorageType.CUSTOM.name().toLowerCase(), SpringUtil .getBean(CaptchaCacheService.class)); - log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Custom' completed initialization."); + log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior-Cache-Custom' completed initialization."); } } } diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/core/BehaviorCaptchaCacheServiceImpl.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java similarity index 95% rename from continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/core/BehaviorCaptchaCacheServiceImpl.java rename to continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java index 4814c839..dfd48971 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/core/BehaviorCaptchaCacheServiceImpl.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.charles7c.continew.starter.captcha.behavior.core; +package top.charles7c.continew.starter.captcha.behavior.autoconfigure.cache; import com.anji.captcha.service.CaptchaCacheService; import top.charles7c.continew.starter.cache.redisson.util.RedisUtils; diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/enums/StorageType.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/enums/StorageType.java index 09c8f460..a85c88d3 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/enums/StorageType.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/charles7c/continew/starter/captcha/behavior/enums/StorageType.java @@ -25,9 +25,9 @@ package top.charles7c.continew.starter.captcha.behavior.enums; public enum StorageType { /** - * 内存 + * 默认(内存) */ - LOCAL, + DEFAULT, /** * Redis diff --git a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java index 6f3b29ba..e2df7733 100644 --- a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/autoconfigure/exception/GlobalExceptionHandlerAutoConfiguration.java @@ -38,8 +38,8 @@ import org.springframework.validation.beanvalidation.SpringConstraintValidatorFa * @since 1.0.0 */ @Configuration(proxyBeanMethods = false) -@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class}) @ConditionalOnMissingBean(BasicErrorController.class) +@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class}) public class GlobalExceptionHandlerAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class);