From d790e6941fa3a9b138aa3c4a569cfda49782928a Mon Sep 17 00:00:00 2001 From: Bull-BCLS Date: Wed, 27 Dec 2023 22:17:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(captcha):=20=E4=BF=AE=E5=A4=8D=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA=E9=AA=8C=E8=AF=81=E7=A0=81=20=20Redis=E3=80=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BC=93=E5=AD=98=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=88=B0=E7=BC=93=E5=AD=98=E5=B7=A5=E5=8E=82?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BehaviorCaptchaAutoConfiguration.java | 4 +- .../BehaviorCaptchaCacheConfiguration.java | 43 ++++++------------- .../BehaviorCaptchaProperties.java | 2 +- 3 files changed, 16 insertions(+), 33 deletions(-) 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 823b28a0..d08112f5 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 @@ -60,7 +60,7 @@ public class BehaviorCaptchaAutoConfiguration { * 自定义缓存实现配置 */ @Configuration - @Import({BehaviorCaptchaCacheConfiguration.Redis.class, BehaviorCaptchaCacheConfiguration.Local.class, BehaviorCaptchaCacheConfiguration.Custom.class}) + @Import({BehaviorCaptchaCacheConfiguration.Redis.class,BehaviorCaptchaCacheConfiguration.Custom.class}) protected static class BehaviorCaptchaCacheAutoConfiguration { } @@ -79,7 +79,7 @@ public class BehaviorCaptchaAutoConfiguration { config.put(Const.ORIGINAL_PATH_JIGSAW, StrUtil.emptyIfNull(properties.getJigsawBaseMapPath())); config.put(Const.ORIGINAL_PATH_PIC_CLICK, StrUtil.emptyIfNull(properties.getPicClickBaseMapPath())); config.put(Const.CAPTCHA_SLIP_OFFSET, properties.getSlipOffset()); - config.put(Const.CAPTCHA_AES_STATUS, String.valueOf(properties.getEnableAES())); + config.put(Const.CAPTCHA_AES_STATUS, String.valueOf(properties.getEnableAes())); config.put(Const.CAPTCHA_WATER_FONT, properties.getWaterFont()); config.put(Const.CAPTCHA_CACAHE_MAX_NUMBER, properties.getCacheNumber()); config.put(Const.CAPTCHA_TIMING_CLEAR_SECOND, properties.getTimingClear()); 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/BehaviorCaptchaCacheConfiguration.java index 6d646f26..9ed1e49b 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/BehaviorCaptchaCacheConfiguration.java @@ -18,7 +18,8 @@ package top.charles7c.continew.starter.captcha.behavior.autoconfigure; import cn.hutool.core.util.ReflectUtil; 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 lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -27,8 +28,8 @@ 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 top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration; +import top.charles7c.continew.starter.captcha.behavior.enums.StorageType; import top.charles7c.continew.starter.captcha.behavior.impl.BehaviorCaptchaCacheServiceImpl; /** @@ -48,31 +49,14 @@ abstract class BehaviorCaptchaCacheConfiguration { @ConditionalOnMissingBean(CaptchaCacheService.class) @AutoConfigureBefore(RedissonAutoConfiguration.class) @ConditionalOnProperty(name = "continew-starter.captcha.behavior.cache-type", havingValue = "redis") + @NoArgsConstructor(access = AccessLevel.PRIVATE) static class Redis { - static { + + @PostConstruct + public void captchaCacheService() { + CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name().toLowerCase(), new BehaviorCaptchaCacheServiceImpl()); log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Redis' completed initialization."); } - - @Bean - public CaptchaCacheService captchaCacheService() { - return new BehaviorCaptchaCacheServiceImpl(); - } - } - - /** - * 自定义缓存实现类-内存 - */ - @ConditionalOnMissingBean(CaptchaCacheService.class) - @ConditionalOnProperty(name = "continew-starter.captcha.behavior.cache-type", havingValue = "local") - static class Local { - static { - log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Local' completed initialization."); - } - - @Bean - public CaptchaCacheService captchaCacheService() { - return new CaptchaCacheServiceMemImpl(); - } } /** @@ -80,14 +64,13 @@ abstract class BehaviorCaptchaCacheConfiguration { */ @ConditionalOnMissingBean(CaptchaCacheService.class) @ConditionalOnProperty(name = "continew-starter.captcha.behavior.cache-type", havingValue = "custom") + @NoArgsConstructor(access = AccessLevel.PRIVATE) static class Custom { - static { - log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Custom' completed initialization."); - } - @Bean - public CaptchaCacheService captchaCacheService(BehaviorCaptchaProperties properties) { - return ReflectUtil.newInstance(properties.getCacheImpl()); + @PostConstruct + public void captchaCacheService(BehaviorCaptchaProperties properties) { + CaptchaServiceFactory.cacheService.put(StorageType.CUSTOM.name().toLowerCase(), ReflectUtil.newInstance(properties.getCacheImpl())); + log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Custom' 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 eb9239b3..69e9a42b 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 @@ -42,7 +42,7 @@ public class BehaviorCaptchaProperties { /** * 是否开启 AES 坐标加密(默认:true) */ - private Boolean enableAES = true; + private Boolean enableAes = true; /** * 验证码类型(默认:滑动验证码)