com.healthy.common.security.code.image.ImageCodeGenerator Maven / Gradle / Ivy
package com.healthy.common.security.code.image;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.healthy.common.security.code.ValidateCodeGenerator;
import com.healthy.common.security.properties.ImageCodeProperties;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import java.awt.image.BufferedImage;
import java.util.Properties;
import static com.google.code.kaptcha.Constants.*;
/**
* ImageCodeGenerator
*
* @author xiaomingzhang
*/
public class ImageCodeGenerator implements ValidateCodeGenerator {
private ImageCodeProperties imageCodeProperties;
@Override
public ImageCode generate(ServletWebRequest request) {
DefaultKaptcha defaultKaptcha = defaultKaptcha(request);
String capText = defaultKaptcha.createText();
BufferedImage image = defaultKaptcha.createImage(capText);
return new ImageCode(image, capText, imageCodeProperties.getExpireIn());
}
private DefaultKaptcha defaultKaptcha(ServletWebRequest request) {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 是否有边框 默认为true 我们可以自己设置yes,no
properties.setProperty(KAPTCHA_BORDER, "yes");
// 边框颜色 默认为Color.BLACK
properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
// 验证码文本字符颜色 默认为Color.BLACK
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
// 验证码图片宽度 默认为200
properties.setProperty(KAPTCHA_IMAGE_WIDTH, String.valueOf(ServletRequestUtils.getIntParameter(request.getRequest(), "width", imageCodeProperties.getWidth())));
// 验证码图片高度 默认为50
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, String.valueOf(ServletRequestUtils.getIntParameter(request.getRequest(), "height", imageCodeProperties.getWidth())));
// 验证码文本字符大小 默认为40
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, String.valueOf(ServletRequestUtils.getIntParameter(request.getRequest(), "font_size", imageCodeProperties.getFontSize())));
// 验证码文本字符间距 默认为2
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
// 验证码文本字符长度 默认为5
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, String.valueOf(imageCodeProperties.getLength()));
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
// 验证码噪点颜色 默认为Color.BLACK
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
// 干扰实现类
properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
public void setImageCodeProperties(ImageCodeProperties imageCodeProperties) {
this.imageCodeProperties = imageCodeProperties;
}
}