fix(captcha): 修复行为验证码 Redis、自定义缓存无法加载到缓存工厂问题

This commit is contained in:
Bull-BCLS
2023-12-27 22:17:47 +08:00
parent 4dae89e0f2
commit d790e6941f
3 changed files with 16 additions and 33 deletions

View File

@@ -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());

View File

@@ -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.");
}
}
}

View File

@@ -42,7 +42,7 @@ public class BehaviorCaptchaProperties {
/**
* 是否开启 AES 坐标加密默认true
*/
private Boolean enableAES = true;
private Boolean enableAes = true;
/**
* 验证码类型(默认:滑动验证码)