feat(resource): 重构资源存储和加载机制

- 新增 AbstractResourceStore 类,实现 ResourceStore 接口的通用逻辑
- 创建 FontCache 类,用于缓存和管理字体资源
- 重构 DefaultImageCaptchaResourceManager 类,支持资源提供者和监听器
- 更新 Resource 和 ResourceMap 类,增加唯一 ID 字段
- 新增 ResourceListener 接口,用于扩展资源存储功能
- 创建 ResourceProviders 类,统一管理资源提供者
- 更新 TACBuilder 类,支持新的资源存储和加载机制
This commit is contained in:
天爱有情
2024-11-22 11:54:21 +08:00
parent 2be22591bf
commit 600878f6bd
20 changed files with 661 additions and 107 deletions
@@ -26,7 +26,7 @@ public class TACBuilderTest {
template1.put(StandardSliderImageCaptchaGenerator.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/active.png"));
template1.put(StandardSliderImageCaptchaGenerator.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/fixed.png"));
ImageCaptchaApplication application = TACBuilder.builder()
ImageCaptchaApplication application = TACBuilder.builder(new LocalMemoryResourceStore())
// 加载系统自带的默认资源
.addDefaultTemplate()
// 设置验证码过期时间
@@ -43,11 +43,11 @@ public class TACBuilderTest {
// 设置缓冲器,可提前生成验证码,用于增加并发性
.cached(10, 1000, 5000, 10000L)
// 添加字体包,用于给文字点选验证码提供字体
.addFont(font)
.addFont(new Resource("file", "C:\\Users\\Thinkpad\\Desktop\\captcha\\手写字体\\ttf\\千图小兔体.ttf"))
// 设置缓存存储器,如果要支持分布式,需要把这里改成分布式缓存,比如通过redis实现的 CacheStore 缓存
.setCacheStore(new LocalCacheStore())
// 设置资源存储器,如果想在分布式环境或者想统一管理以及扩展 实现 ResourceStore 接口,自定义
.setResourceStore(new LocalMemoryResourceStore())
// .setResourceStore(new LocalMemoryResourceStore())
// 图片转换器,默认是将图片转换成base64格式, 背景图为jpg, 模板图为png, 如果想要扩展,可替换成自己实现的
.setTransform(new Base64ImageTransform())
.build();
@@ -34,7 +34,7 @@ public class TACBuilderTest2 {
return CaptchaInterceptor.super.beforeGenerateCaptcha(context, type, param);
}
})
.addFont(font)
.addFont(new Resource("file", "C:\\Users\\Thinkpad\\Desktop\\captcha\\手写字体\\ttf\\千图小兔体.ttf"))
.build();
CaptchaResponse<ImageCaptchaVO> response = application.generateCaptcha("WORD_IMAGE_CLICK");
System.out.println(response);
+1 -1
View File
@@ -20,7 +20,7 @@ public class Test7 {
}
@Override
public boolean supported(String type) {
public boolean supported(Resource type) {
return false;
}