复制代码代码如下:
pacote com.g.core.common.JCaptcha;
importar java.awt.Color;
importar java.awt.Font;
importar com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
importar com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
importar com.octo.captcha.component.image.color.RandomListColorGenerator;
importar com.octo.captcha.component.image.fontgenerator.FontGenerator;
importar com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
importar com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
importar com.octo.captcha.component.image.textpaster.TextPaster;
importar com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
importar com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
importar com.octo.captcha.component.image.wordtoimage.WordToImage;
importar com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
importar com.octo.captcha.component.word.wordgenerator.WordGenerator;
importar com.octo.captcha.engine.image.ListImageCaptchaEngine;
importar com.octo.captcha.image.gimpy.GimpyFactory;
/**
* 生成验证码图 foto
*/
classe pública JCaptchaEngine estende 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/";// 随机背景图片路径
// 验证码随机字体
fonte final estática privada[] RANDOM_FONT = nova fonte[] {
nova fonte("nyala", Font.BOLD, MIN_FONT_SIZE),
nova fonte("Arial", Font.BOLD, MIN_FONT_SIZE),
nova fonte("Bell MT", Font.BOLD, MIN_FONT_SIZE),
new Font("Vale de crédito", Font.BOLD, MIN_FONT_SIZE),
new Font("Impacto", Font.BOLD, MIN_FONT_SIZE)
};
// 验证码随机颜色
cor final estática privada[] RANDOM_COLOR = nova cor[] {
nova cor (255, 255, 255),
nova cor (255, 220, 220),
nova cor (220, 255, 255),
nova cor (220, 220, 255),
nova cor (255, 255, 220),
nova cor (220, 255, 220)
};
// 生成验证码
@Substituir
void protegido buildInitialFactories() {
RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR);
BackgroundGenerator backgroundGenerator = novo FileReaderRandomBackgroundGenerator (IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_PATH);
WordGenerator wordGenerator = new RandomWordGenerator(RANDOM_WORD);
FontGenerator fontGenerator = novo RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE, RANDOM_FONT);
TextDecorator[] textDecorator = new TextDecorator[] {};
TextPaster textPaster = novo DecoratedRandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, randomListColorGenerator, textDecorator);
WordToImage wordToImage = novo ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
addFactory(new GimpyFactory(wordGenerator, wordToImage));
}
}
复制代码代码如下:
pacote com.g.core.common.JCaptcha;
importar com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
importar com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
importar com.octo.captcha.service.image.ImageCaptchaService;
classe pública CaptchaServiceSingleton {
instância estática privada do ImageCaptchaService =null;
public CaptchaServiceSingleton() {
}
// 使用synchronized关键字解决线程不安全
público sincronizado estático ImageCaptchaService getInstance() {
if (instância == nulo) {
instância = novo DefaultManageableImageCaptchaService(novo FastHashMapCaptchaStore(), novo JCaptchaEngine(), 180,
100.000, 75.000);
}
instância de retorno;
}
}
复制代码代码如下:
pacote com.g.core.render;
importar java.awt.image.BufferedImage;
importar java.io.IOException;
importar javax.imageio.ImageIO;
importar javax.servlet.ServletOutputStream;
importar com.g.core.common.JCaptcha.CaptchaServiceSingleton;
importar com.jfinal.kit.StringKit;
importar com.jfinal.render.Render;
classe pública JCaptchaRender estende Render {
string privada randomCodeKey;
public JCaptchaRender(String randomCodeKey) {
if (StringKit.isBlank(randomCodeKey))
throw new IllegalArgumentException("randomCodeKey não pode ficar em branco");
this.randomCodeKey = randomCodeKey;
}
@Substituir
renderização pública void() {
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "sem cache");
response.setDateHeader("Expira", 0);
resposta.setContentType("imagem/jpeg");
ServletOutputStream sos = null;
tentar {
sos = resposta.getOutputStream();
//String captchaId = request.getSession(true).getId();
Desafio BufferedImage = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(randomCodeKey, request.getLocale());
ImageIO.write(desafio, "jpg", sos);
sos.flush();
} catch (Exceção e) {
lançar nova RuntimeException(e);
}
finalmente {
if (sos! = nulo)
tente {sos.close();} catch (IOException e) {e.printStackTrace();}
}
}
}
复制代码代码如下:
public void código_aleatório() {
render(new JCaptchaRender(getSession().getId()));
}