mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-12 00:44:06 +08:00
1.5.0.beta
This commit is contained in:
@@ -26,23 +26,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class ApplicationTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 验证码资源管理器
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
// 验证码生成器; 注意: 生成器必须调用init(...)初始化方法 true为加载默认资源,false为不加载,
|
||||
ImageCaptchaGenerator generator = new MultiImageCaptchaGenerator(imageCaptchaResourceManager).init(true);
|
||||
// 验证码校验器
|
||||
ImageCaptchaValidator imageCaptchaValidator = new SimpleImageCaptchaValidator();
|
||||
// 缓存, 用于存放校验数据
|
||||
CacheStore cacheStore = new LocalCacheStore();
|
||||
// 验证码拦截器, 可以是单个,也可以是一组拦截器,可以嵌套, 这里演示加载参数校验拦截,和 滑动轨迹拦截
|
||||
CaptchaInterceptorGroup group = new CaptchaInterceptorGroup();
|
||||
group.addInterceptor(new ParamCheckCaptchaInterceptor());
|
||||
group.addInterceptor(new BasicTrackCaptchaInterceptor());
|
||||
|
||||
ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
||||
// application 验证码封装, prop为所需的一些扩展参数
|
||||
ImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, imageCaptchaValidator, cacheStore, prop, group);
|
||||
|
||||
ImageCaptchaApplication application = createImageCaptchaApplication();
|
||||
// 生成验证码数据, 可以将该数据直接返回给前端 , 可配合 tianai-captcha-web-sdk 使用
|
||||
CaptchaResponse<ImageCaptchaVO> res = application.generateCaptcha("SLIDER");
|
||||
System.out.println(res);
|
||||
@@ -54,7 +38,9 @@ public class ApplicationTest {
|
||||
ApiResponse<?> valid = application.matching(id, imageCaptchaTrack);
|
||||
System.out.println(valid.isSuccess());
|
||||
|
||||
|
||||
// 扩展: 一个简单的二次验证
|
||||
CacheStore cacheStore = new LocalCacheStore();
|
||||
if (valid.isSuccess()) {
|
||||
// 如果验证成功,生成一个token并存储, 将该token返回给客户端,客户端下次请求数据时携带该token, 后台判断是否有效
|
||||
String token = UUID.randomUUID().toString();
|
||||
@@ -62,4 +48,24 @@ public class ApplicationTest {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static ImageCaptchaApplication createImageCaptchaApplication() {
|
||||
// 验证码资源管理器 该类负责管理验证码背景图和模板图等数据
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
// 验证码生成器; 注意: 生成器必须调用init(...)初始化方法 true为加载默认资源,false为不加载,
|
||||
ImageCaptchaGenerator generator = new MultiImageCaptchaGenerator(imageCaptchaResourceManager).init(true);
|
||||
// 验证码校验器
|
||||
ImageCaptchaValidator imageCaptchaValidator = new SimpleImageCaptchaValidator();
|
||||
// 缓存, 用于存放校验数据
|
||||
CacheStore cacheStore = new LocalCacheStore();
|
||||
// 验证码拦截器, 可以是单个,也可以是一组拦截器,可以嵌套, 这里演示加载参数校验拦截,和 滑动轨迹拦截
|
||||
CaptchaInterceptorGroup group = new CaptchaInterceptorGroup();
|
||||
group.addInterceptor(new ParamCheckCaptchaInterceptor());
|
||||
group.addInterceptor(new BasicTrackCaptchaInterceptor());
|
||||
|
||||
ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
||||
// application 验证码封装, prop为所需的一些扩展参数
|
||||
ImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, imageCaptchaValidator, cacheStore, prop, group);
|
||||
return application;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user