复制代码代码如下:
包 com.g.core.common.JCaptcha;
导入java.awt.Color;
导入java.awt.Font;
导入 com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
导入 com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
导入 com.octo.captcha.component.image.color.RandomListColorGenerator;
导入 com.octo.captcha.component.image.fontgenerator.FontGenerator;
导入 com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
导入 com.octo.captcha.component.image.textpaster.DecolatedRandomTextPaster;
导入 com.octo.captcha.component.image.textpaster.TextPaster;
导入 com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
导入 com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
导入 com.octo.captcha.component.image.wordtoimage.WordToImage;
导入 com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
导入 com.octo.captcha.component.word.wordgenerator.WordGenerator;
导入 com.octo.captcha.engine.image.ListImageCaptchaEngine;
导入 com.octo.captcha.image.gimpy.GimpyFactory;
/**
* 生成验证码图片
*/
公共类 JCaptchaEngine 扩展 ListImageCaptchaEngine {
public static Final String IMAGE_CAPTCHA_KEY = "imageCaptcha";// ImageCaptcha对象存放在Session中的key
public static Final String CAPTCHA_INPUT_NAME = "j_captcha";//验证码输入表单名称
public static Final String CAPTCHA_IMAGE_URL = "/captcha.jpg";//验证码图片URL
private static Final Integer MIN_WORD_LENGTH = 4;//验证码最小长度
private static Final Integer MAX_WORD_LENGTH = 4;//验证码最大长度
private static Final Integer IMAGE_HEIGHT = 28;//验证码图片高度
private static Final Integer IMAGE_WIDTH = 80;//验证码图片宽度
private static Final Integer MIN_FONT_SIZE = 16;//验证码最小字体
private static Final Integer MAX_FONT_SIZE = 16;//验证码最大字体
private static Final String RANDOM_WORD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 随机字符
private static Final String IMAGE_PATH = "./captcha/";//随机背景图片路径
// 验证码随机字体
私有静态最终字体[] RANDOM_FONT = 新字体[] {
新字体(“nyala”,Font.BOLD,MIN_FONT_SIZE),
新字体(“Arial”,Font.BOLD,MIN_FONT_SIZE),
新字体(“贝尔MT”,Font.BOLD,MIN_FONT_SIZE),
new Font("信用谷", Font.BOLD, MIN_FONT_SIZE),
新字体(“影响力”,Font.BOLD,MIN_FONT_SIZE)
};
//验证码随机颜色
私有静态最终颜色[] RANDOM_COLOR = 新颜色[] {
新颜色(255, 255, 255),
新颜色(255, 220, 220),
新颜色(220, 255, 255),
新颜色(220, 220, 255),
新颜色(255, 255, 220),
新颜色(220, 255, 220)
};
// 生成验证码
@覆盖
受保护的无效buildInitialFactories(){
RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR);
背景生成器 背景生成器 = new FileReaderRandomBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_PATH);
WordGenerator wordGenerator = new RandomWordGenerator(RANDOM_WORD);
FontGenerator fontGenerator = new RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE, RANDOM_FONT);
TextDecorator[] textDecorator = new TextDecorator[] {};
TextPaster textPaster = new DecoratedRandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, randomListColorGenerator, textDecorator);
WordToImage wordToImage = new CompositWordToImage(fontGenerator, backgroundGenerator, textPaster);
addFactory(new GimpyFactory(wordGenerator, wordToImage));
}
}
复制代码代码如下:
包 com.g.core.common.JCaptcha;
导入 com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
导入 com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
导入 com.octo.captcha.service.image.ImageCaptchaService;
公共类 CaptchaServiceSingleton {
私有静态 ImageCaptchaService 实例 =null;
公共 CaptchaServiceSingleton() {
}
// 使用同步关键字解决线程不安全
公共同步静态 ImageCaptchaService getInstance() {
如果(实例==空){
实例=新的DefaultManageableImageCaptchaService(新的FastHashMapCaptchaStore(),新的JCaptchaEngine(),180,
100000、75000);
}
返回实例;
}
}
复制代码代码如下:
包 com.g.core.render;
导入java.awt.image.BufferedImage;
导入java.io.IOException;
导入 javax.imageio.ImageIO;
导入 javax.servlet.ServletOutputStream;
导入 com.g.core.common.JCaptcha.CaptchaServiceSingleton;
导入 com.jfinal.kit.StringKit;
导入 com.jfinal.render.Render;
公共类 JCaptchaRender 扩展 Render {
私有字符串随机代码密钥;
公共 JCaptchaRender(字符串 randomCodeKey) {
if (StringKit.isBlank(randomCodeKey))
throw new IllegalArgumentException("randomCodeKey 不能为空");
this.randomCodeKey = randomCodeKey;
}
@覆盖
公共无效渲染(){
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "无缓存");
response.setDateHeader("过期", 0);
响应.setContentType(“图像/jpeg”);
ServletOutputStream sos = null;
尝试 {
sos = response.getOutputStream();
//字符串 captchaId = request.getSession(true).getId();
BufferedImage 挑战 = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(randomCodeKey, request.getLocale());
ImageIO.write(挑战, "jpg", sos);
sos.flush();
} catch (异常 e) {
抛出新的运行时异常(e);
}
最后 {
if (sos!= null)
尝试 {sos.close();} catch (IOException e) {e.printStackTrace();}
}
}
}
复制代码代码如下:
公共无效随机代码(){
渲染(new JCaptchaRender(getSession().getId()));
}