1.5.0正式版
This commit is contained in:
@@ -12,6 +12,7 @@ import cloud.tianai.captcha.common.util.CollectionUtils;
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
||||
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
||||
import cloud.tianai.captcha.generator.impl.CacheImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
|
||||
import cloud.tianai.captcha.interceptor.EmptyCaptchaInterceptor;
|
||||
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||
@@ -51,7 +52,7 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
|
||||
ImageCaptchaProperties prop,
|
||||
CaptchaInterceptor captchaInterceptor) {
|
||||
this.prop = prop;
|
||||
setImageCaptchaGenerator(captchaGenerator);
|
||||
|
||||
setImageCaptchaValidator(imageCaptchaValidator);
|
||||
setCacheStore(cacheStore);
|
||||
// 默认过期时间
|
||||
@@ -65,6 +66,14 @@ public class DefaultImageCaptchaApplication implements ImageCaptchaApplication {
|
||||
this.captchaInterceptor = captchaInterceptor;
|
||||
}
|
||||
captchaGenerator.setInterceptor(this.captchaInterceptor);
|
||||
if (prop.isLocalCacheEnabled()) {
|
||||
captchaGenerator = new CacheImageCaptchaGenerator(captchaGenerator,
|
||||
prop.getLocalCacheSize(), prop.getLocalCacheWaitTime(),
|
||||
prop.getLocalCachePeriod(), prop.getLocalCacheExpireTime());
|
||||
}
|
||||
// 初始化生成器
|
||||
captchaGenerator.init();
|
||||
setImageCaptchaGenerator(captchaGenerator);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,7 @@ package cloud.tianai.captcha.application;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -15,5 +15,12 @@ public class ImageCaptchaProperties {
|
||||
/** 过期key prefix. */
|
||||
private String prefix = "captcha";
|
||||
/** 过期时间. */
|
||||
private Map<String, Long> expire = Collections.emptyMap();
|
||||
private Map<String, Long> expire = new HashMap<>();
|
||||
|
||||
// 本地提前缓存
|
||||
private boolean localCacheEnabled = false;
|
||||
private int localCacheSize = 10;
|
||||
private int localCacheWaitTime = 1000;
|
||||
private int localCachePeriod = 5000;
|
||||
private Long localCacheExpireTime;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package cloud.tianai.captcha.application;
|
||||
|
||||
import cloud.tianai.captcha.cache.CacheStore;
|
||||
import cloud.tianai.captcha.cache.impl.LocalCacheStore;
|
||||
import cloud.tianai.captcha.common.util.CollectionUtils;
|
||||
import cloud.tianai.captcha.generator.ImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.generator.ImageTransform;
|
||||
import cloud.tianai.captcha.generator.common.FontWrapper;
|
||||
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||
import cloud.tianai.captcha.interceptor.CaptchaInterceptor;
|
||||
import cloud.tianai.captcha.interceptor.EmptyCaptchaInterceptor;
|
||||
import cloud.tianai.captcha.resource.DefaultBuiltInResources;
|
||||
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.LocalMemoryResourceStore;
|
||||
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
|
||||
import cloud.tianai.captcha.validator.ImageCaptchaValidator;
|
||||
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: 天爱有情
|
||||
* @date 2024/7/14 16:41
|
||||
* @Description 一个构建ImageCaptchaApplication的工具, 免去一些繁琐的配置,方便新手用户一键使用
|
||||
*/
|
||||
public class TACBuilder {
|
||||
|
||||
private CacheStore cacheStore;
|
||||
private ImageCaptchaGenerator generator;
|
||||
private ImageCaptchaValidator validator;
|
||||
private CaptchaInterceptor interceptor = EmptyCaptchaInterceptor.INSTANCE;
|
||||
private ImageCaptchaProperties prop = new ImageCaptchaProperties();
|
||||
private ResourceStore resourceStore;
|
||||
private ImageTransform imageTransform;
|
||||
private List<FontWrapper> fontWrappers = new ArrayList<>();
|
||||
|
||||
public static TACBuilder builder() {
|
||||
TACBuilder builder = new TACBuilder();
|
||||
// 默认设置本地的
|
||||
LocalMemoryResourceStore resourceStore = new LocalMemoryResourceStore();
|
||||
builder.resourceStore = resourceStore;
|
||||
builder.prop = new ImageCaptchaProperties();
|
||||
return builder;
|
||||
}
|
||||
|
||||
private TACBuilder() {
|
||||
}
|
||||
|
||||
public TACBuilder addDefaultTemplate(String defaultPathPrefix) {
|
||||
DefaultBuiltInResources defaultBuiltInResources = new DefaultBuiltInResources(defaultPathPrefix);
|
||||
defaultBuiltInResources.addDefaultTemplate(resourceStore);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addDefaultTemplate() {
|
||||
return addDefaultTemplate(DefaultBuiltInResources.PATH_PREFIX);
|
||||
}
|
||||
|
||||
public TACBuilder setCacheStore(CacheStore cacheStore) {
|
||||
this.cacheStore = cacheStore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setGenerator(ImageCaptchaGenerator generator) {
|
||||
this.generator = generator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setValidator(ImageCaptchaValidator validator) {
|
||||
this.validator = validator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setInterceptor(CaptchaInterceptor interceptor) {
|
||||
this.interceptor = interceptor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addFont(FontWrapper fontWrapper) {
|
||||
this.fontWrappers.add(fontWrapper);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addFont(Font font) {
|
||||
return addFont(new FontWrapper(font));
|
||||
}
|
||||
|
||||
|
||||
public TACBuilder cached(int size, int waitTime, int period, Long expireTime) {
|
||||
prop.setLocalCacheEnabled(true);
|
||||
prop.setLocalCacheSize(size);
|
||||
prop.setLocalCacheWaitTime(waitTime);
|
||||
prop.setLocalCachePeriod(period);
|
||||
prop.setLocalCacheExpireTime(expireTime);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder prefix(String prefix) {
|
||||
this.prop.setPrefix(prefix);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder expire(String captchaType, Long expireTime) {
|
||||
prop.getExpire().put(captchaType, expireTime);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setProp(ImageCaptchaProperties prop) {
|
||||
this.prop = prop;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setResourceStore(ResourceStore resourceStore) {
|
||||
this.resourceStore = resourceStore;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public TACBuilder addResource(String captchaType, Resource imageResource) {
|
||||
this.resourceStore.addResource(captchaType, imageResource);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder addTemplate(String captchaType, ResourceMap resourceMap) {
|
||||
this.resourceStore.addTemplate(captchaType, resourceMap);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TACBuilder setTransform(ImageTransform imageTransform) {
|
||||
this.imageTransform = imageTransform;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ImageCaptchaApplication build() {
|
||||
if (cacheStore == null) {
|
||||
cacheStore = new LocalCacheStore();
|
||||
}
|
||||
if (generator == null) {
|
||||
DefaultImageCaptchaResourceManager resourceManager = new DefaultImageCaptchaResourceManager(resourceStore);
|
||||
generator = new MultiImageCaptchaGenerator(resourceManager, imageTransform);
|
||||
}
|
||||
if (generator instanceof MultiImageCaptchaGenerator) {
|
||||
if (CollectionUtils.isEmpty(fontWrappers)) {
|
||||
// 添加默认字体
|
||||
try {
|
||||
ClassPathResourceProvider resourceProvider = new ClassPathResourceProvider();
|
||||
InputStream stream = resourceProvider.getResourceInputStream(new Resource("classpath", "META-INF/fonts/SIMSUN.TTC"));
|
||||
Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
|
||||
stream.close();
|
||||
fontWrappers.add(new FontWrapper(font));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("读取默认字体包报错",e);
|
||||
}
|
||||
}
|
||||
((MultiImageCaptchaGenerator) generator).setFontWrappers(fontWrappers);
|
||||
}
|
||||
if (validator == null) {
|
||||
validator = new SimpleImageCaptchaValidator();
|
||||
}
|
||||
if (interceptor == null) {
|
||||
interceptor = EmptyCaptchaInterceptor.INSTANCE;
|
||||
}
|
||||
|
||||
DefaultImageCaptchaApplication application = new DefaultImageCaptchaApplication(generator, validator, cacheStore, prop, interceptor);
|
||||
return application;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user