mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-06 21:53:10 +08:00
1.5.0.beta
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
## 可能是开源界最好用的行为验证码工具
|
||||
|
||||
> 重大更新,1.5.0版本, 与1.4.x的版本不兼容,请谨慎升级
|
||||
-----
|
||||
|
||||
## pc版在线体验 [在线体验](http://captcha.tianai.cloud)
|
||||
@@ -126,186 +125,6 @@ public class ApplicationTest {
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## 扩展
|
||||
|
||||
### 生成带有混淆滑块的图片
|
||||
|
||||
```java
|
||||
package example.readme;
|
||||
|
||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.ImageTransform;
|
||||
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
||||
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
||||
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.impl.transform.Base64ImageTransform;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||
|
||||
public class Test3 {
|
||||
public static void main(String[] args) {
|
||||
// application 为 ImageCaptchaApplication对象
|
||||
// 生成 具有混淆的 滑块验证码 (目前只有滑块验证码支持混淆滑块, 旋转验证,滑动还原,点选验证 均不支持混淆功能)
|
||||
ImageCaptchaInfo imageCaptchaInfo = application.generateCaptcha(GenerateParam.builder()
|
||||
.type(CaptchaTypeConstant.SLIDER)
|
||||
// 是否添加混淆滑块
|
||||
.obfuscate(true)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 添加自定义图片资源
|
||||
|
||||
- 自定义图片资源大小为 600*360 格式为jpg
|
||||
|
||||
```java
|
||||
package example.readme;
|
||||
|
||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.ResourceStore;
|
||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
|
||||
import cloud.tianai.captcha.resource.impl.provider.URLResourceProvider;
|
||||
|
||||
public class Test5 {
|
||||
public static void main(String[] args) {
|
||||
// 在资源管理器中设置自定义图片资源
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
// 通过资源管理器或者资源存储器
|
||||
ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
|
||||
// 添加classpath目录下的 aa.jpg 图片
|
||||
resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(ClassPathResourceProvider.NAME, "/aa.jpg"));
|
||||
// 添加远程url图片资源
|
||||
resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(URLResourceProvider.NAME, "http://www.xx.com/aa.jpg"));
|
||||
// 内置了通过url 和 classpath读取图片资源,如果想扩展可实现 ResourceProvider 接口,进行自定义扩展
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 添加自定义模板资源
|
||||
|
||||
- 系统内置了2套模板,可以到QQ群:1021884609 文件中获取更多模板或者自己制作模板
|
||||
- 模板图片格式
|
||||
- 滑块验证码
|
||||
- 滑块大小为 110*110 格式为png
|
||||
- 凹槽大小为 110*110 格式为png
|
||||
- 旋转验证码
|
||||
- 滑块大小为 200*200 格式为png
|
||||
- 凹槽大小为 200*200 格式为png
|
||||
|
||||
```java
|
||||
package example.readme;
|
||||
|
||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||
import cloud.tianai.captcha.generator.common.constant.SliderCaptchaConstant;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.ResourceStore;
|
||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||
import cloud.tianai.captcha.resource.common.model.dto.ResourceMap;
|
||||
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
|
||||
|
||||
public class Test6 {
|
||||
public static void main(String[] args) {
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
// 通过资源管理器或者资源存储器
|
||||
ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
|
||||
// 添加滑块验证码模板.模板图片由三张图片组成
|
||||
ResourceMap template1 = new ResourceMap("default", 4);
|
||||
template1.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/active.png"));
|
||||
template1.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, "/fixed.png"));
|
||||
resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, template1);
|
||||
// 模板与两张图片组成 滑块、凹槽
|
||||
// 同样默认支持 classpath 和 url 两种获取图片资源, 如果想扩展可实现 ResourceProvider 接口,进行自定义扩展
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 自定义 `ResourceProvider` 实现自定义文件读取策略, 比如 oss之类的
|
||||
|
||||
```java
|
||||
package example.readme;
|
||||
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.ResourceProvider;
|
||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class Test7 {
|
||||
public static void main(String[] args) {
|
||||
// 自定义 ResourceProvider
|
||||
ResourceProvider resourceProvider = new ResourceProvider() {
|
||||
@Override
|
||||
public InputStream getResourceInputStream(Resource data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supported(String type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
ImageTransform imageTransform = new Base64ImageTransform();
|
||||
ImageCaptchaGenerator imageCaptchaGenerator = new MultiImageCaptchaGenerator(imageCaptchaResourceManager, imageTransform).init(false);
|
||||
// 注册
|
||||
imageCaptchaResourceManager.registerResourceProvider(resourceProvider);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 扩展,对`StandardImageCaptchaGenerator`增加了缓存模块
|
||||
|
||||
> 由于实时生成滑块图片可能会有一点性能影响,内部基于`StandardSliderCaptchaGenerator`进行了提前缓存生成好的图片,
|
||||
> `CacheSliderCaptchaGenerator` 是一个装饰类,这只是基本的缓存逻辑,比较简单,用户可以定义一些更加有意思的扩展,用于突破性能瓶颈
|
||||
|
||||
```java
|
||||
package example.readme;
|
||||
|
||||
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
||||
import cloud.tianai.captcha.generator.impl.CacheImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||
|
||||
public class Test8 {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
// 使用 CacheSliderCaptchaGenerator 对滑块验证码进行缓存,使其提前生成滑块图片
|
||||
// 参数一: 真正实现 滑块的 SliderCaptchaGenerator
|
||||
// 参数二: 默认提前缓存多少个
|
||||
// 参数三: 出错后 等待xx时间再进行生成
|
||||
// 参数四: 检查时间间隔
|
||||
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||
ImageTransform imageTransform = new Base64ImageTransform();
|
||||
ImageCaptchaGenerator imageCaptchaGenerator = new CacheImageCaptchaGenerator(new MultiImageCaptchaGenerator(imageCaptchaResourceManager, imageTransform), 10, 1000, 100);
|
||||
imageCaptchaGenerator.init(true);
|
||||
}
|
||||
}
|
||||
```
|
||||
### 验证码拦截器
|
||||
|
||||
> 如有需要对验证码进行增强或拦截之类的功能,可以通过实现`ImageCaptchaInterceptor`接口,然后注册到`ImageCaptchaApplication`中
|
||||
> 拦截器内具体执行顺序和功能请查看代码注释
|
||||
|
||||
# qq群: 305532064
|
||||
|
||||
# 微信群:
|
||||
|
||||
Reference in New Issue
Block a user