refactor(captcha/behavior): 优化行为验证码缓存配置

This commit is contained in:
2024-02-02 20:44:10 +08:00
parent e6f8ac8afa
commit 8598e6d109
8 changed files with 52 additions and 43 deletions

View File

@@ -44,7 +44,7 @@ public class SaTokenDaoConfiguration {
} }
/** /**
* 自定义持久层实现-默认 * 自定义持久层实现-默认(内存)
*/ */
@ConditionalOnMissingBean(SaTokenDao.class) @ConditionalOnMissingBean(SaTokenDao.class)
@ConditionalOnClass(RedisClient.class) @ConditionalOnClass(RedisClient.class)

View File

@@ -25,7 +25,7 @@ package top.charles7c.continew.starter.auth.satoken.enums;
public enum SaTokenDaoType { public enum SaTokenDaoType {
/** /**
* 默认 * 默认(内存)
*/ */
DEFAULT, DEFAULT,

View File

@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; 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 top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.util.HashMap; import java.util.HashMap;
@@ -49,8 +50,8 @@ import java.util.Properties;
* @since 1.1.0 * @since 1.1.0
*/ */
@AutoConfiguration @AutoConfiguration
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true")
@EnableConfigurationProperties(BehaviorCaptchaProperties.class) @EnableConfigurationProperties(BehaviorCaptchaProperties.class)
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true")
public class BehaviorCaptchaAutoConfiguration { public class BehaviorCaptchaAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaAutoConfiguration.class); private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaAutoConfiguration.class);
@@ -64,7 +65,8 @@ public class BehaviorCaptchaAutoConfiguration {
* 自定义缓存实现配置 * 自定义缓存实现配置
*/ */
@Configuration @Configuration
@Import({BehaviorCaptchaCacheConfiguration.Redis.class, BehaviorCaptchaCacheConfiguration.Custom.class}) @Import({BehaviorCaptchaCacheConfiguration.Default.class, BehaviorCaptchaCacheConfiguration.Redis.class,
BehaviorCaptchaCacheConfiguration.Custom.class})
protected static class BehaviorCaptchaCacheAutoConfiguration {} protected static class BehaviorCaptchaCacheAutoConfiguration {}
/** /**
@@ -140,6 +142,6 @@ public class BehaviorCaptchaAutoConfiguration {
@PostConstruct @PostConstruct
public void postConstruct() { public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Behavior Captcha' completed initialization."); log.debug("[ContiNew Starter] - Auto Configuration 'Captcha-Behavior' completed initialization.");
} }
} }

View File

@@ -17,7 +17,6 @@
package top.charles7c.continew.starter.captcha.behavior.autoconfigure; package top.charles7c.continew.starter.captcha.behavior.autoconfigure;
import com.anji.captcha.model.common.CaptchaTypeEnum; import com.anji.captcha.model.common.CaptchaTypeEnum;
import com.anji.captcha.service.CaptchaCacheService;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.captcha.behavior.enums.StorageType; import top.charles7c.continew.starter.captcha.behavior.enums.StorageType;
import top.charles7c.continew.starter.core.constant.PropertiesConstants; import top.charles7c.continew.starter.core.constant.PropertiesConstants;
@@ -49,14 +48,9 @@ public class BehaviorCaptchaProperties {
private CaptchaTypeEnum type = CaptchaTypeEnum.BLOCKPUZZLE; private CaptchaTypeEnum type = CaptchaTypeEnum.BLOCKPUZZLE;
/** /**
* 缓存类型默认LOCAL 内存) * 缓存类型
*/ */
private StorageType cacheType = StorageType.LOCAL; private StorageType cacheType = StorageType.DEFAULT;
/**
* 自定义缓存类型(当 cacheType 为 CUSTOM 时必填)
*/
private Class<? extends CaptchaCacheService> cacheImpl;
/** /**
* 滑动拼图底图路径(为空则使用默认底图)(路径下需要有两个文件夹,分别为 original存放底图slidingBlock存放滑块 * 滑动拼图底图路径(为空则使用默认底图)(路径下需要有两个文件夹,分别为 original存放底图slidingBlock存放滑块
@@ -180,14 +174,6 @@ public class BehaviorCaptchaProperties {
this.cacheType = cacheType; this.cacheType = cacheType;
} }
public Class<? extends CaptchaCacheService> getCacheImpl() {
return cacheImpl;
}
public void setCacheImpl(Class<? extends CaptchaCacheService> cacheImpl) {
this.cacheImpl = cacheImpl;
}
public String getJigsawBaseMapPath() { public String getJigsawBaseMapPath() {
return jigsawBaseMapPath; return jigsawBaseMapPath;
} }
@@ -334,6 +320,6 @@ public class BehaviorCaptchaProperties {
@Override @Override
public String toString() { 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 + '}';
} }
} }

View File

@@ -14,33 +14,35 @@
* limitations under the License. * 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 cn.hutool.extra.spring.SpringUtil;
import com.anji.captcha.service.CaptchaCacheService; import com.anji.captcha.service.CaptchaCacheService;
import com.anji.captcha.service.impl.CaptchaCacheServiceMemImpl;
import com.anji.captcha.service.impl.CaptchaServiceFactory; import com.anji.captcha.service.impl.CaptchaServiceFactory;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.redisson.client.RedisClient; import org.redisson.client.RedisClient;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; 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.cache.redisson.autoconfigure.RedissonAutoConfiguration;
import top.charles7c.continew.starter.captcha.behavior.enums.StorageType; 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; import top.charles7c.continew.starter.core.constant.PropertiesConstants;
/** /**
* 行为验证码缓存配置 * 行为验证码缓存配置
* *
* @author Bull-BCLS * @author Bull-BCLS
* @author Charles7c
* @since 1.1.0 * @since 1.1.0
*/ */
abstract class BehaviorCaptchaCacheConfiguration { public class BehaviorCaptchaCacheConfiguration {
private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaCacheConfiguration.class); private static final Logger log = LoggerFactory.getLogger(BehaviorCaptchaCacheConfiguration.class);
@@ -48,36 +50,55 @@ abstract class BehaviorCaptchaCacheConfiguration {
} }
/** /**
* 自定义缓存实现-Redis * 自定义缓存实现-默认内存
*/ */
@ConditionalOnClass(RedisClient.class) @ConditionalOnMissingBean(CaptchaCacheService.class)
@AutoConfigureBefore(RedissonAutoConfiguration.class) @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "default", matchIfMissing = true)
@ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis") public static class Default {
static class Redis {
@PostConstruct @PostConstruct
public void postConstruct() { public void postConstruct() {
CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name() CaptchaServiceFactory.cacheService.put(StorageType.DEFAULT.name()
.toLowerCase(), new BehaviorCaptchaCacheServiceImpl()); .toLowerCase(), new CaptchaCacheServiceMemImpl());
log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Redis' completed initialization."); 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") @ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "custom")
static class Custom { public static class Custom {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CaptchaCacheService captchaCacheService(BehaviorCaptchaProperties properties) { public CaptchaCacheService captchaCacheService() {
return ReflectUtil.newInstance(properties.getCacheImpl()); if (log.isErrorEnabled()) {
log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType
.forClass(CaptchaCacheService.class));
}
throw new NoSuchBeanDefinitionException(CaptchaCacheService.class);
} }
@PostConstruct @PostConstruct
public void postConstruct() { public void postConstruct() {
CaptchaServiceFactory.cacheService.put(StorageType.CUSTOM.name().toLowerCase(), SpringUtil CaptchaServiceFactory.cacheService.put(StorageType.CUSTOM.name().toLowerCase(), SpringUtil
.getBean(CaptchaCacheService.class)); .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.");
} }
} }
} }

View File

@@ -14,7 +14,7 @@
* limitations under the License. * 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 com.anji.captcha.service.CaptchaCacheService;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils; import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;

View File

@@ -25,9 +25,9 @@ package top.charles7c.continew.starter.captcha.behavior.enums;
public enum StorageType { public enum StorageType {
/** /**
* 内存 * 默认(内存
*/ */
LOCAL, DEFAULT,
/** /**
* Redis * Redis

View File

@@ -38,8 +38,8 @@ import org.springframework.validation.beanvalidation.SpringConstraintValidatorFa
* @since 1.0.0 * @since 1.0.0
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class})
@ConditionalOnMissingBean(BasicErrorController.class) @ConditionalOnMissingBean(BasicErrorController.class)
@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class})
public class GlobalExceptionHandlerAutoConfiguration { public class GlobalExceptionHandlerAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class); private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAutoConfiguration.class);