From 62121b78ec653991cf6bf3710a13cf5aa467b55b Mon Sep 17 00:00:00 2001 From: Yoofff Date: Mon, 20 May 2024 20:53:38 +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=E6=8E=A5=E5=8F=A3=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=AC=A1=E6=95=B0=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BehaviorCaptchaAutoConfiguration.java | 13 +---- .../BehaviorCaptchaProperties.java | 55 ++++++++++--------- ...ehaviorCaptchaCacheAutoConfiguration.java} | 33 +++++++---- .../BehaviorCaptchaCacheServiceImpl.java | 15 ++++- ...ot.autoconfigure.AutoConfiguration.imports | 3 +- 5 files changed, 67 insertions(+), 52 deletions(-) rename continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/{BehaviorCaptchaCacheConfiguration.java => BehaviorCaptchaCacheAutoConfiguration.java} (83%) diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java index 04cfc995..37a733a4 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaAutoConfiguration.java @@ -31,12 +31,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.DependsOn; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; -import top.continew.starter.captcha.behavior.autoconfigure.cache.BehaviorCaptchaCacheConfiguration; import top.continew.starter.core.constant.PropertiesConstants; import java.util.HashMap; @@ -61,18 +59,11 @@ public class BehaviorCaptchaAutoConfiguration { this.properties = properties; } - /** - * 自定义缓存实现配置 - */ - @Configuration - @Import({BehaviorCaptchaCacheConfiguration.Default.class, BehaviorCaptchaCacheConfiguration.Redis.class, - BehaviorCaptchaCacheConfiguration.Custom.class}) - protected static class BehaviorCaptchaCacheAutoConfiguration {} - /** * 行为验证码服务接口 */ @Bean + @DependsOn("captchaCacheService") @ConditionalOnMissingBean public CaptchaService captchaService() { Properties config = new Properties(); diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java index cbac1c19..87fa1182 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/BehaviorCaptchaProperties.java @@ -16,6 +16,7 @@ package top.continew.starter.captcha.behavior.autoconfigure; +import cn.hutool.core.convert.Convert; import com.anji.captcha.model.common.CaptchaTypeEnum; import org.springframework.boot.context.properties.ConfigurationProperties; import top.continew.starter.captcha.behavior.enums.StorageType; @@ -75,37 +76,37 @@ public class BehaviorCaptchaProperties { /** * 历史数据清除开关(0:关闭;1:开启) */ - private Integer historyDataClearEnable = 0; + private String historyDataClearEnable = "0"; /** * 一分钟内接口请求次数限制开关(0:关闭;1:开启) */ - private Integer reqFrequencyLimitEnable = 0; + private String reqFrequencyLimitEnable = "0"; /** * 一分钟内验证码最多失败次数限制(默认:5次) */ - private int reqGetLockLimit = 5; + private String reqGetLockLimit = "5"; /** * 一分钟内验证码最多失败次数限制达标后锁定时间(默认:300秒) */ - private int reqGetLockSeconds = 300; + private String reqGetLockSeconds = "300"; /** * 获取验证码接口一分钟内请求次数限制(默认:100次) */ - private int reqGetMinuteLimit = 100; + private String reqGetMinuteLimit = "100"; /** * 校验检验码接口一分内请求次数限制(默认:100次) */ - private int reqCheckMinuteLimit = 100; + private String reqCheckMinuteLimit = "100"; /** * 二次校验检验码接口一分钟内请求次数限制(默认:100次) */ - private int reqVerifyMinuteLimit = 100; + private String reqVerifyMinuteLimit = "100"; /** * local缓存的阈值(默认:1000个) @@ -135,12 +136,12 @@ public class BehaviorCaptchaProperties { /** * 点选字体样式(默认:BOLD) */ - private int fontStyle = Font.BOLD; + private String fontStyle = Convert.toStr(Font.BOLD); /** * 点选字体大小(默认:25) */ - private int fontSize = 25; + private String fontSize = "25"; public boolean isEnabled() { return enabled; @@ -206,59 +207,59 @@ public class BehaviorCaptchaProperties { this.fontType = fontType; } - public Integer getHistoryDataClearEnable() { + public String getHistoryDataClearEnable() { return historyDataClearEnable; } - public void setHistoryDataClearEnable(Integer historyDataClearEnable) { + public void setHistoryDataClearEnable(String historyDataClearEnable) { this.historyDataClearEnable = historyDataClearEnable; } - public Integer getReqFrequencyLimitEnable() { + public String getReqFrequencyLimitEnable() { return reqFrequencyLimitEnable; } - public void setReqFrequencyLimitEnable(Integer reqFrequencyLimitEnable) { + public void setReqFrequencyLimitEnable(String reqFrequencyLimitEnable) { this.reqFrequencyLimitEnable = reqFrequencyLimitEnable; } - public int getReqGetLockLimit() { + public String getReqGetLockLimit() { return reqGetLockLimit; } - public void setReqGetLockLimit(int reqGetLockLimit) { + public void setReqGetLockLimit(String reqGetLockLimit) { this.reqGetLockLimit = reqGetLockLimit; } - public int getReqGetLockSeconds() { + public String getReqGetLockSeconds() { return reqGetLockSeconds; } - public void setReqGetLockSeconds(int reqGetLockSeconds) { + public void setReqGetLockSeconds(String reqGetLockSeconds) { this.reqGetLockSeconds = reqGetLockSeconds; } - public int getReqGetMinuteLimit() { + public String getReqGetMinuteLimit() { return reqGetMinuteLimit; } - public void setReqGetMinuteLimit(int reqGetMinuteLimit) { + public void setReqGetMinuteLimit(String reqGetMinuteLimit) { this.reqGetMinuteLimit = reqGetMinuteLimit; } - public int getReqCheckMinuteLimit() { + public String getReqCheckMinuteLimit() { return reqCheckMinuteLimit; } - public void setReqCheckMinuteLimit(int reqCheckMinuteLimit) { + public void setReqCheckMinuteLimit(String reqCheckMinuteLimit) { this.reqCheckMinuteLimit = reqCheckMinuteLimit; } - public int getReqVerifyMinuteLimit() { + public String getReqVerifyMinuteLimit() { return reqVerifyMinuteLimit; } - public void setReqVerifyMinuteLimit(int reqVerifyMinuteLimit) { + public void setReqVerifyMinuteLimit(String reqVerifyMinuteLimit) { this.reqVerifyMinuteLimit = reqVerifyMinuteLimit; } @@ -302,19 +303,19 @@ public class BehaviorCaptchaProperties { this.interferenceOptions = interferenceOptions; } - public int getFontStyle() { + public String getFontStyle() { return fontStyle; } - public void setFontStyle(int fontStyle) { + public void setFontStyle(String fontStyle) { this.fontStyle = fontStyle; } - public int getFontSize() { + public String getFontSize() { return fontSize; } - public void setFontSize(int fontSize) { + public void setFontSize(String fontSize) { this.fontSize = fontSize; } } diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheAutoConfiguration.java similarity index 83% rename from continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java rename to continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheAutoConfiguration.java index 2843c849..c56d5ec0 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheConfiguration.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheAutoConfiguration.java @@ -25,7 +25,7 @@ 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.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -36,29 +36,35 @@ import top.continew.starter.captcha.behavior.enums.StorageType; import top.continew.starter.core.constant.PropertiesConstants; /** - * 行为验证码缓存配置 + * 行为验证码缓存自动配置 * * @author Bull-BCLS * @author Charles7c * @since 1.1.0 */ -public class BehaviorCaptchaCacheConfiguration { +@AutoConfiguration() +public class BehaviorCaptchaCacheAutoConfiguration { - private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaCacheConfiguration.class); + private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaCacheAutoConfiguration.class); - private BehaviorCaptchaCacheConfiguration() { + private BehaviorCaptchaCacheAutoConfiguration() { } /** * 自定义缓存实现-默认(内存) */ + @AutoConfiguration @ConditionalOnMissingBean(CaptchaCacheService.class) @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "default", matchIfMissing = true) public static class Default { + @Bean + public CaptchaCacheService captchaCacheService() { + return new CaptchaCacheServiceMemImpl(); + } + @PostConstruct public void postConstruct() { - CaptchaServiceFactory.cacheService.put(StorageType.DEFAULT.name() - .toLowerCase(), new CaptchaCacheServiceMemImpl()); + CaptchaServiceFactory.cacheService.put(StorageType.DEFAULT.name().toLowerCase(), captchaCacheService()); log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior-Cache-Default' completed initialization."); } } @@ -66,15 +72,19 @@ public class BehaviorCaptchaCacheConfiguration { /** * 自定义缓存实现-Redis */ - @ConditionalOnMissingBean(CaptchaCacheService.class) + @AutoConfiguration(before = RedissonAutoConfiguration.class) @ConditionalOnClass(RedisClient.class) - @AutoConfigureBefore(RedissonAutoConfiguration.class) + @ConditionalOnMissingBean(CaptchaCacheService.class) @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis") public static class Redis { + @Bean + public CaptchaCacheService captchaCacheService() { + return new BehaviorCaptchaCacheServiceImpl(); + } + @PostConstruct public void postConstruct() { - CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name() - .toLowerCase(), new BehaviorCaptchaCacheServiceImpl()); + CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name().toLowerCase(), captchaCacheService()); log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior-Cache-Redis' completed initialization."); } } @@ -82,6 +92,7 @@ public class BehaviorCaptchaCacheConfiguration { /** * 自定义缓存实现 */ + @AutoConfiguration @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "custom") public static class Custom { @Bean diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java index f61107c1..a3f219ac 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/java/top/continew/starter/captcha/behavior/autoconfigure/cache/BehaviorCaptchaCacheServiceImpl.java @@ -16,6 +16,8 @@ package top.continew.starter.captcha.behavior.autoconfigure.cache; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.NumberUtil; import com.anji.captcha.service.CaptchaCacheService; import top.continew.starter.cache.redisson.util.RedisUtils; import top.continew.starter.captcha.behavior.enums.StorageType; @@ -31,7 +33,11 @@ import java.time.Duration; public class BehaviorCaptchaCacheServiceImpl implements CaptchaCacheService { @Override public void set(String key, String value, long expiresInSeconds) { - RedisUtils.set(key, value, Duration.ofSeconds(expiresInSeconds)); + if (NumberUtil.isNumber(value)) { + RedisUtils.set(key, Convert.toInt(value), Duration.ofSeconds(expiresInSeconds)); + } else { + RedisUtils.set(key, value, Duration.ofSeconds(expiresInSeconds)); + } } @Override @@ -46,11 +52,16 @@ public class BehaviorCaptchaCacheServiceImpl implements CaptchaCacheService { @Override public String get(String key) { - return RedisUtils.get(key); + return Convert.toStr(RedisUtils.get(key)); } @Override public String type() { return StorageType.REDIS.name().toLowerCase(); } + + @Override + public Long increment(String key, long val) { + return RedisUtils.incr(key); + } } diff --git a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index f629ac78..2b89bf00 100644 --- a/continew-starter-captcha/continew-starter-captcha-behavior/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-captcha/continew-starter-captcha-behavior/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,2 @@ -top.continew.starter.captcha.behavior.autoconfigure.BehaviorCaptchaAutoConfiguration \ No newline at end of file +top.continew.starter.captcha.behavior.autoconfigure.BehaviorCaptchaAutoConfiguration +top.continew.starter.captcha.behavior.autoconfigure.cache.BehaviorCaptchaCacheAutoConfiguration \ No newline at end of file