mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-07 06:04:34 +08:00
!22 1、缓存前缀处理逻辑修改为提供专用的缓存前缀处理接口 StoreCacheKeyPrefix 类,方便用户个性缓存前缀处理逻辑扩展需求。
Merge pull request !22 from Alay/master
This commit is contained in:
+8
-3
@@ -2,6 +2,7 @@ package cloud.tianai.captcha.application;
|
|||||||
|
|
||||||
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
||||||
import cloud.tianai.captcha.cache.CacheStore;
|
import cloud.tianai.captcha.cache.CacheStore;
|
||||||
|
import cloud.tianai.captcha.cache.StoreCacheKeyPrefix;
|
||||||
import cloud.tianai.captcha.common.AnyMap;
|
import cloud.tianai.captcha.common.AnyMap;
|
||||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||||
import cloud.tianai.captcha.common.exception.ImageCaptchaException;
|
import cloud.tianai.captcha.common.exception.ImageCaptchaException;
|
||||||
@@ -41,6 +42,8 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
|
|||||||
private CacheStore cacheStore;
|
private CacheStore cacheStore;
|
||||||
/** 验证码配置属性. */
|
/** 验证码配置属性. */
|
||||||
private final ImageCaptchaProperties prop;
|
private final ImageCaptchaProperties prop;
|
||||||
|
/** 缓存key 前缀处理器. */
|
||||||
|
private StoreCacheKeyPrefix storeCacheKeyPrefix;
|
||||||
/** 默认的过期时间. */
|
/** 默认的过期时间. */
|
||||||
private long defaultExpire = 20000L;
|
private long defaultExpire = 20000L;
|
||||||
|
|
||||||
@@ -50,9 +53,10 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
|
|||||||
ImageCaptchaValidator imageCaptchaValidator,
|
ImageCaptchaValidator imageCaptchaValidator,
|
||||||
CacheStore cacheStore,
|
CacheStore cacheStore,
|
||||||
ImageCaptchaProperties prop,
|
ImageCaptchaProperties prop,
|
||||||
CaptchaInterceptor captchaInterceptor) {
|
CaptchaInterceptor captchaInterceptor,
|
||||||
|
StoreCacheKeyPrefix storeCacheKeyPrefix) {
|
||||||
this.prop = prop;
|
this.prop = prop;
|
||||||
|
this.storeCacheKeyPrefix = null != storeCacheKeyPrefix? storeCacheKeyPrefix: StoreCacheKeyPrefix.prefixed(prop.getPrefix());
|
||||||
setImageCaptchaValidator(imageCaptchaValidator);
|
setImageCaptchaValidator(imageCaptchaValidator);
|
||||||
setCacheStore(cacheStore);
|
setCacheStore(cacheStore);
|
||||||
// 默认过期时间
|
// 默认过期时间
|
||||||
@@ -235,7 +239,8 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String getKey(String id) {
|
protected String getKey(String id) {
|
||||||
return prop.getPrefix().concat(":").concat(id);
|
// 改为通过接口扩展
|
||||||
|
return storeCacheKeyPrefix.compute(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cloud.tianai.captcha.application;
|
package cloud.tianai.captcha.application;
|
||||||
|
|
||||||
import cloud.tianai.captcha.cache.CacheStore;
|
import cloud.tianai.captcha.cache.CacheStore;
|
||||||
|
import cloud.tianai.captcha.cache.StoreCacheKeyPrefix;
|
||||||
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
||||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||||
import cloud.tianai.captcha.generator.ImageTransform;
|
import cloud.tianai.captcha.generator.ImageTransform;
|
||||||
@@ -29,6 +30,7 @@ public class TACBuilder {
|
|||||||
private ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
private ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
||||||
private ResourceStore resourceStore;
|
private ResourceStore resourceStore;
|
||||||
private ImageTransform imageTransform;
|
private ImageTransform imageTransform;
|
||||||
|
private StoreCacheKeyPrefix cacheKeyPrefix;
|
||||||
// private List<FontWrapper> fontWrappers = new ArrayList<>();
|
// private List<FontWrapper> fontWrappers = new ArrayList<>();
|
||||||
|
|
||||||
public static TACBuilder builder() {
|
public static TACBuilder builder() {
|
||||||
@@ -75,6 +77,10 @@ public class TACBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TACBuilder setCacheKeyPrefix(StoreCacheKeyPrefix cacheKeyPrefix) {
|
||||||
|
this.cacheKeyPrefix = cacheKeyPrefix;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
public TACBuilder addFont(Resource resource) {
|
public TACBuilder addFont(Resource resource) {
|
||||||
this.addResource(FontCache.FONT_TYPE, resource);
|
this.addResource(FontCache.FONT_TYPE, resource);
|
||||||
return this;
|
return this;
|
||||||
@@ -148,7 +154,8 @@ public class TACBuilder {
|
|||||||
if (interceptor == null) {
|
if (interceptor == null) {
|
||||||
interceptor = EmptyCaptchaInterceptor.INSTANCE;
|
interceptor = EmptyCaptchaInterceptor.INSTANCE;
|
||||||
}
|
}
|
||||||
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor);
|
// 增加前缀处理接口
|
||||||
|
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor, cacheKeyPrefix);
|
||||||
return application;
|
return application;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package cloud.tianai.captcha.cache;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码缓存Key前缀处理
|
||||||
|
*
|
||||||
|
* @author Alay
|
||||||
|
* @since 2025-11-11 13:44
|
||||||
|
*/
|
||||||
|
public interface StoreCacheKeyPrefix {
|
||||||
|
/**
|
||||||
|
* 缓存Key 计算处理
|
||||||
|
*
|
||||||
|
* @param captchaId 原始验证码Id
|
||||||
|
* @return 处理后的验证码缓存Key
|
||||||
|
*/
|
||||||
|
String compute(String captchaId);
|
||||||
|
|
||||||
|
static StoreCacheKeyPrefix prefixed(String prefix) {
|
||||||
|
if (prefix == null || prefix.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("prefix must not be null or empty");
|
||||||
|
}
|
||||||
|
return captchaId -> prefix.concat(":").concat(captchaId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user