mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(captcha/behavior): 优化行为验证码缓存配置
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
@@ -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<? extends CaptchaCacheService> cacheImpl;
|
||||
private StorageType cacheType = StorageType.DEFAULT;
|
||||
|
||||
/**
|
||||
* 滑动拼图底图路径(为空则使用默认底图)(路径下需要有两个文件夹,分别为 original(存放底图)slidingBlock(存放滑块))
|
||||
@@ -180,14 +174,6 @@ public class BehaviorCaptchaProperties {
|
||||
this.cacheType = cacheType;
|
||||
}
|
||||
|
||||
public Class<? extends CaptchaCacheService> getCacheImpl() {
|
||||
return cacheImpl;
|
||||
}
|
||||
|
||||
public void setCacheImpl(Class<? extends CaptchaCacheService> 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 + '}';
|
||||
}
|
||||
}
|
||||
|
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -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;
|
@@ -25,9 +25,9 @@ package top.charles7c.continew.starter.captcha.behavior.enums;
|
||||
public enum StorageType {
|
||||
|
||||
/**
|
||||
* 内存
|
||||
* 默认(内存)
|
||||
*/
|
||||
LOCAL,
|
||||
DEFAULT,
|
||||
|
||||
/**
|
||||
* Redis
|
||||
|
Reference in New Issue
Block a user