mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-07 06:04:34 +08:00
fix: TacBuilder中添加资源只能添加一条的bug
This commit is contained in:
@@ -17,9 +17,7 @@ import cloud.tianai.captcha.resource.impl.LocalMemoryResourceStore;
|
|||||||
import cloud.tianai.captcha.validator.ImageCaptchaValidator;
|
import cloud.tianai.captcha.validator.ImageCaptchaValidator;
|
||||||
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
|
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: 天爱有情
|
* @Author: 天爱有情
|
||||||
@@ -36,9 +34,10 @@ public class TACBuilder {
|
|||||||
private ResourceStore resourceStore;
|
private ResourceStore resourceStore;
|
||||||
private ImageTransform imageTransform;
|
private ImageTransform imageTransform;
|
||||||
// private List<FontWrapper> fontWrappers = new ArrayList<>();
|
// private List<FontWrapper> fontWrappers = new ArrayList<>();
|
||||||
private Map<String, Resource> resourceCache;
|
private Map<String, List<Resource>> resourceCache = new HashMap<>(8);
|
||||||
private Map<String, ResourceMap> templateCache;
|
private Map<String, List<ResourceMap>> templateCache = new HashMap<>(8);
|
||||||
|
|
||||||
|
private String defaultTemplatePrefix = null;
|
||||||
public static TACBuilder builder() {
|
public static TACBuilder builder() {
|
||||||
return new TACBuilder();
|
return new TACBuilder();
|
||||||
}
|
}
|
||||||
@@ -53,8 +52,7 @@ public class TACBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
||||||
DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultPathPrefix);
|
this.defaultTemplatePrefix = defaultPathPrefix;
|
||||||
defaultBuiltInResources.addDefaultTemplate(resourceStore);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,14 +153,23 @@ public class TACBuilder {
|
|||||||
if (resourceStore instanceof CrudResourceStore) {
|
if (resourceStore instanceof CrudResourceStore) {
|
||||||
CrudResourceStore crudResourceStore = (CrudResourceStore) resourceStore;
|
CrudResourceStore crudResourceStore = (CrudResourceStore) resourceStore;
|
||||||
if (!CollectionUtils.isEmpty(resourceCache)) {
|
if (!CollectionUtils.isEmpty(resourceCache)) {
|
||||||
resourceCache.forEach(crudResourceStore::addResource);
|
resourceCache.forEach((type,resources) -> {
|
||||||
resourceCache = null;
|
resources.forEach(resource -> crudResourceStore.addResource(type,resource));
|
||||||
|
});
|
||||||
|
resourceCache.clear();
|
||||||
}
|
}
|
||||||
if (!CollectionUtils.isEmpty(templateCache)) {
|
if (!CollectionUtils.isEmpty(templateCache)) {
|
||||||
templateCache.forEach(crudResourceStore::addTemplate);
|
templateCache.forEach((type, templates) -> {
|
||||||
templateCache = null;
|
templates.forEach(template -> crudResourceStore.addTemplate(type, template));
|
||||||
|
});
|
||||||
|
templateCache.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 添加默认模板
|
||||||
|
if (defaultTemplatePrefix != null) {
|
||||||
|
DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultTemplatePrefix);
|
||||||
|
defaultBuiltInResources.addDefaultTemplate(resourceStore);
|
||||||
|
}
|
||||||
if (generator == null) {
|
if (generator == null) {
|
||||||
ResourceProviders resourceProviders = new ResourceProviders();
|
ResourceProviders resourceProviders = new ResourceProviders();
|
||||||
DefaultImageCaptchaResourceManager resourceManager = new DefaultImageCaptchaResourceManager(resourceStore, resourceProviders);
|
DefaultImageCaptchaResourceManager resourceManager = new DefaultImageCaptchaResourceManager(resourceStore, resourceProviders);
|
||||||
@@ -183,16 +190,10 @@ public class TACBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void cacheResource(String captchaType, Resource imageResource) {
|
private void cacheResource(String captchaType, Resource imageResource) {
|
||||||
if (resourceCache == null) {
|
resourceCache.computeIfAbsent(captchaType, k -> new ArrayList<>(4)).add(imageResource);
|
||||||
resourceCache = new LinkedHashMap<>(8);
|
|
||||||
}
|
|
||||||
resourceCache.put(captchaType, imageResource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cacheTemplate(String captchaType, ResourceMap resourceMap) {
|
private void cacheTemplate(String captchaType, ResourceMap resourceMap) {
|
||||||
if (templateCache == null) {
|
templateCache.computeIfAbsent(captchaType, k -> new ArrayList<>(4)).add(resourceMap);
|
||||||
templateCache = new LinkedHashMap<>(8);
|
|
||||||
}
|
|
||||||
templateCache.put(captchaType, resourceMap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ public class TACBuilderTest {
|
|||||||
|
|
||||||
ImageCaptchaApplication application = TACBuilder.builder()
|
ImageCaptchaApplication application = TACBuilder.builder()
|
||||||
// 设置资源存储器,默认是 LocalMemoryResourceStore
|
// 设置资源存储器,默认是 LocalMemoryResourceStore
|
||||||
.setResourceStore(new LocalMemoryResourceStore())
|
// .setResourceStore(new LocalMemoryResourceStore())
|
||||||
// 加载系统自带的默认资源(系统内置了几个滑块验证码缺口模板图,调用此函数加载)
|
// 加载系统自带的默认资源(系统内置了几个滑块验证码缺口模板图,调用此函数加载)
|
||||||
.addDefaultTemplate()
|
.addDefaultTemplate()
|
||||||
// 设置验证码过期时间, 单位毫秒, default 是默认验证码过期时间,当前设置为10秒,
|
// 设置验证码过期时间, 单位毫秒, default 是默认验证码过期时间,当前设置为10秒,
|
||||||
@@ -71,6 +71,7 @@ public class TACBuilderTest {
|
|||||||
// arg1 验证码类型(SLIDER、WORD_IMAGE_CLICK、ROTATE、CONCAT),
|
// arg1 验证码类型(SLIDER、WORD_IMAGE_CLICK、ROTATE、CONCAT),
|
||||||
// arg2 验证码背景图片资源
|
// arg2 验证码背景图片资源
|
||||||
.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
|
.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.addResource(CaptchaTypeConstant.ROTATE, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource(CaptchaTypeConstant.ROTATE, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.addResource(CaptchaTypeConstant.CONCAT, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource(CaptchaTypeConstant.CONCAT, new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
|
|||||||
Reference in New Issue
Block a user