mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-07 06:04:34 +08:00
refactor(resource): 重构资源存储和管理逻辑
- 移除了 AbstractResourceStore 类 - 新增了 CrudResourceStore 接口,定义了 CRUD操作 - 修改了 DefaultImageCaptchaResourceManager,支持批量获取资源和模板 - 重构了 FontCache 类,改为实现 ResourceStore 接口 - 更新了相关应用类,使用新的资源管理逻辑
This commit is contained in:
@@ -46,8 +46,8 @@ public class TACBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
||||||
// DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultPathPrefix);
|
DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultPathPrefix);
|
||||||
// defaultBuiltInResources.addDefaultTemplate(resourceStore);
|
defaultBuiltInResources.addDefaultTemplate(resourceStore);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,12 +29,12 @@ public class ApplicationTest {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ImageCaptchaApplication application = createImageCaptchaApplication();
|
ImageCaptchaApplication application = createImageCaptchaApplication();
|
||||||
// 生成验证码数据, 可以将该数据直接返回给前端 , 可配合 tianai-captcha-web-sdk 使用
|
// 生成验证码数据, 可以将该数据直接返回给前端 , 可配合 tianai-captcha-web-sdk 使用
|
||||||
CaptchaResponse<ImageCaptchaVO> res = application.generateCaptcha("SLIDER");
|
ApiResponse<ImageCaptchaVO> res = application.generateCaptcha("SLIDER");
|
||||||
System.out.println(res);
|
System.out.println(res);
|
||||||
|
|
||||||
// 校验验证码, ImageCaptchaTrack 和 id 均为前端传开的参数, 可将 valid数据直接返回给 前端
|
// 校验验证码, ImageCaptchaTrack 和 id 均为前端传开的参数, 可将 valid数据直接返回给 前端
|
||||||
// 注意: 该项目只负责生成和校验验证码数据, 至于二次验证等需要自行扩展
|
// 注意: 该项目只负责生成和校验验证码数据, 至于二次验证等需要自行扩展
|
||||||
String id =res.getId();
|
String id =res.getData().getId();
|
||||||
ImageCaptchaTrack imageCaptchaTrack = null;
|
ImageCaptchaTrack imageCaptchaTrack = null;
|
||||||
ApiResponse<?> valid = application.matching(id, imageCaptchaTrack);
|
ApiResponse<?> valid = application.matching(id, imageCaptchaTrack);
|
||||||
System.out.println(valid.isSuccess());
|
System.out.println(valid.isSuccess());
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import cloud.tianai.captcha.application.vo.CaptchaResponse;
|
|||||||
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
||||||
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
||||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||||
|
import cloud.tianai.captcha.common.response.ApiResponse;
|
||||||
import cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator;
|
import cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator;
|
||||||
import cloud.tianai.captcha.generator.impl.transform.Base64ImageTransform;
|
import cloud.tianai.captcha.generator.impl.transform.Base64ImageTransform;
|
||||||
import cloud.tianai.captcha.interceptor.EmptyCaptchaInterceptor;
|
import cloud.tianai.captcha.interceptor.EmptyCaptchaInterceptor;
|
||||||
@@ -54,7 +55,7 @@ public class TACBuilderTest {
|
|||||||
|
|
||||||
while (true){
|
while (true){
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
CaptchaResponse<ImageCaptchaVO> response = application.generateCaptcha("SLIDER");
|
ApiResponse<ImageCaptchaVO> response = application.generateCaptcha("SLIDER");
|
||||||
System.out.println("耗时:" + (System.currentTimeMillis() - start));
|
System.out.println("耗时:" + (System.currentTimeMillis() - start));
|
||||||
// System.out.println(response);
|
// System.out.println(response);
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import cloud.tianai.captcha.application.ImageCaptchaApplication;
|
|||||||
import cloud.tianai.captcha.application.TACBuilder;
|
import cloud.tianai.captcha.application.TACBuilder;
|
||||||
import cloud.tianai.captcha.application.vo.CaptchaResponse;
|
import cloud.tianai.captcha.application.vo.CaptchaResponse;
|
||||||
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
|
||||||
|
import cloud.tianai.captcha.common.response.ApiResponse;
|
||||||
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
||||||
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
|
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
|
||||||
import cloud.tianai.captcha.interceptor.Context;
|
import cloud.tianai.captcha.interceptor.Context;
|
||||||
@@ -17,9 +18,6 @@ import java.io.IOException;
|
|||||||
public class TACBuilderTest2 {
|
public class TACBuilderTest2 {
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, FontFormatException {
|
public static void main(String[] args) throws IOException, FontFormatException {
|
||||||
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Thinkpad\\Desktop\\captcha\\手写字体\\ttf\\千图小兔体.ttf");
|
|
||||||
Font font = Font.createFont(Font.TRUETYPE_FONT, fileInputStream);
|
|
||||||
fileInputStream.close();
|
|
||||||
ImageCaptchaApplication application = TACBuilder.builder()
|
ImageCaptchaApplication application = TACBuilder.builder()
|
||||||
.addDefaultTemplate()
|
.addDefaultTemplate()
|
||||||
.expire("default", 10000L)
|
.expire("default", 10000L)
|
||||||
@@ -27,16 +25,8 @@ public class TACBuilderTest2 {
|
|||||||
.addResource("SLIDER", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource("SLIDER", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.addResource("WORD_IMAGE_CLICK", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource("WORD_IMAGE_CLICK", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.addResource("ROTATE", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
.addResource("ROTATE", new Resource("classpath", "META-INF/cut-image/resource/1.jpg"))
|
||||||
.setInterceptor(new CaptchaInterceptor() {
|
|
||||||
@Override
|
|
||||||
public CaptchaResponse<ImageCaptchaVO> beforeGenerateCaptcha(Context context, String type, GenerateParam param) {
|
|
||||||
System.out.println("before generator");
|
|
||||||
return CaptchaInterceptor.super.beforeGenerateCaptcha(context, type, param);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.addFont(new Resource("file", "C:\\Users\\Thinkpad\\Desktop\\captcha\\手写字体\\ttf\\千图小兔体.ttf"))
|
|
||||||
.build();
|
.build();
|
||||||
CaptchaResponse<ImageCaptchaVO> response = application.generateCaptcha("WORD_IMAGE_CLICK");
|
ApiResponse<ImageCaptchaVO> response = application.generateCaptcha("WORD_IMAGE_CLICK");
|
||||||
System.out.println(response);
|
System.out.println(response);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package example.readme;
|
|||||||
|
|
||||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||||
import cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator;
|
import cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator;
|
||||||
|
import cloud.tianai.captcha.resource.CrudResourceStore;
|
||||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||||
import cloud.tianai.captcha.resource.ResourceStore;
|
import cloud.tianai.captcha.resource.ResourceStore;
|
||||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||||
@@ -13,7 +14,7 @@ public class Test6 {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||||
// 通过资源管理器或者资源存储器
|
// 通过资源管理器或者资源存储器
|
||||||
ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
|
CrudResourceStore resourceStore = (CrudResourceStore) imageCaptchaResourceManager.getResourceStore();
|
||||||
// 添加滑块验证码模板.模板图片由三张图片组成
|
// 添加滑块验证码模板.模板图片由三张图片组成
|
||||||
ResourceMap template1 = new ResourceMap("default", 4);
|
ResourceMap template1 = new ResourceMap("default", 4);
|
||||||
template1.put(StandardSliderImageCaptchaGenerator.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/active.png"));
|
template1.put(StandardSliderImageCaptchaGenerator.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/active.png"));
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package example.readme;
|
package example.readme;
|
||||||
|
|
||||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||||
|
import cloud.tianai.captcha.resource.CrudResourceStore;
|
||||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||||
import cloud.tianai.captcha.resource.ResourceStore;
|
import cloud.tianai.captcha.resource.ResourceStore;
|
||||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||||
@@ -15,7 +16,7 @@ public class TestImageCaptcha {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||||
// 通过资源管理器或者资源存储器
|
// 通过资源管理器或者资源存储器
|
||||||
ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
|
CrudResourceStore resourceStore = (CrudResourceStore) imageCaptchaResourceManager.getResourceStore();
|
||||||
// 添加classpath目录下的 aa.jpg 图片
|
// 添加classpath目录下的 aa.jpg 图片
|
||||||
resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(ClassPathResourceProvider.NAME, "/aa.jpg"));
|
resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(ClassPathResourceProvider.NAME, "/aa.jpg"));
|
||||||
// 添加远程url图片资源
|
// 添加远程url图片资源
|
||||||
|
|||||||
Reference in New Issue
Block a user