复制代码代码如下:
paket com.g.core.common.JCaptcha;
impor java.awt.Color;
impor java.awt.Font;
impor com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
impor com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
impor com.octo.captcha.component.image.color.RandomListColorGenerator;
impor com.octo.captcha.component.image.fontgenerator.FontGenerator;
impor com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
impor com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
impor com.octo.captcha.component.image.textpaster.TextPaster;
impor com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
impor com.octo.captcha.component.image.wordtoimage.ComposeWordToImage;
impor com.octo.captcha.component.image.wordtoimage.WordToImage;
impor com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
impor com.octo.captcha.component.word.wordgenerator.WordGenerator;
impor com.octo.captcha.engine.image.ListImageCaptchaEngine;
impor com.octo.captcha.image.gimpy.GimpyFactory;
/**
* kata sandi foto
*/
kelas publik JCaptchaEngine memperluas ListImageCaptchaEngine {
String final statis publik IMAGE_CAPTCHA_KEY = "imageCaptcha";// ImageCaptcha对象存放在Session中的key
String akhir statis publik CAPTCHA_INPUT_NAME = "j_captcha";// 验证码输入表单名称
String akhir statis publik CAPTCHA_IMAGE_URL = "/captcha.jpg";// 验证码图片URL
Integer akhir statis pribadi MIN_WORD_LENGTH = 4;// 验证码最小长度
Integer akhir statis pribadi MAX_WORD_LENGTH = 4;// 验证码最大长度
Private static final Integer IMAGE_HEIGHT = 28;// 验证码图片高度
Integer akhir statis pribadi IMAGE_WIDTH = 80;// 验证码图片宽度
Integer akhir statis pribadi MIN_FONT_SIZE = 16;// 验证码最小字体
Integer akhir statis pribadi MAX_FONT_SIZE = 16;// 验证码最大字体
String final statis pribadi RANDOM_WORD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 随机字符
String final statis pribadi IMAGE_PATH = "./captcha/";// 随机背景图片路径
// 验证码随机字体
font akhir statis pribadi[] RANDOM_FONT = Font baru[] {
Font baru("nyala", Font.BOLD, MIN_FONT_SIZE),
Font baru("Arial", Font.BOLD, MIN_FONT_SIZE),
Font baru("Bell MT", Font.BOLD, MIN_FONT_SIZE),
Font baru("Lembah Kredit", Font.BOLD, MIN_FONT_SIZE),
Font baru("Dampak", Font.BOLD, MIN_FONT_SIZE)
};
// 验证码随机颜色
warna akhir statis pribadi[] RANDOM_COLOR = Warna baru[] {
Warna baru (255, 255, 255),
Warna baru (255, 220, 220),
Warna baru (220, 255, 255),
Warna baru (220, 220, 255),
Warna baru (255, 255, 220),
Warna baru (220, 255, 220)
};
// 生成验证码
@Mengesampingkan
dilindungi kekosongan buildInitialFactories() {
RandomListColorGenerator randomListColorGenerator = RandomListColorGenerator baru(RANDOM_COLOR);
BackgroundGenerator backgroundGenerator = FileReaderRandomBackgroundGenerator baru(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_PATH);
WordGenerator wordGenerator = RandomWordGenerator baru(RANDOM_WORD);
FontGenerator fontGenerator = RandomFontGenerator baru(MIN_FONT_SIZE, MAX_FONT_SIZE, RANDOM_FONT);
TextDecorator[] textDecorator = TextDecorator[] {} baru;
TextPaster textPaster = new DecoratedRandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, randomListColorGenerator, textDecorator);
WordToImage wordToImage = baru ComposeWordToImage(fontGenerator, backgroundGenerator, textPaster);
addFactory(GimpyFactory baru(wordGenerator, wordToImage));
}
}
复制代码代码如下:
paket com.g.core.common.JCaptcha;
impor com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
impor com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
impor com.octo.captcha.service.image.ImageCaptchaService;
kelas publik CaptchaServiceSingleton {
instance ImageCaptchaService statis pribadi =null;
publik CaptchaServiceSingleton() {
}
// 使用 disinkronkan关键字解决线程不安全
ImageCaptchaService getInstance() statis tersinkronisasi publik {
jika (contoh == nol) {
contoh = baru DefaultManageableImageCaptchaService(FastHashMapCaptchaStore() baru, JCaptchaEngine() baru, 180,
100.000, 75.000);
}
contoh kembali;
}
}
复制代码代码如下:
paket com.g.core.render;
impor java.awt.image.BufferedImage;
impor java.io.IOException;
impor javax.imageio.ImageIO;
impor javax.servlet.ServletOutputStream;
import com.g.core.common.JCaptcha.CaptchaServiceSingleton;
impor com.jfinal.kit.StringKit;
import com.jfinal.render.Render;
kelas publik JCaptchaRender memperluas Render {
String pribadi RandomCodeKey;
publik JCaptchaRender(String randomCodeKey) {
jika (StringKit.isBlank(randomCodeKey))
throw new IllegalArgumentException("randomCodeKey tidak boleh kosong");
this.randomCodeKey = randomCodeKey;
}
@Mengesampingkan
kekosongan publik render() {
respon.setHeader("Kontrol Cache", "tidak ada penyimpanan");
respon.setHeader("Pragma", "tanpa cache");
respon.setDateHeader("Kedaluwarsa", 0);
respon.setContentType("gambar/jpeg");
ServletOutputStream sos = null;
mencoba {
sos = respon.getOutputStream();
//String captchaId = permintaan.getSession(true).getId();
Tantangan BufferedImage = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(randomCodeKey, request.getLocale());
ImageIO.write(tantangan, "jpg", sos);
sos.flush();
} tangkapan (Pengecualian e) {
melempar RuntimeException(e);
}
Akhirnya {
jika (sos != nol)
coba {sos.close();} catch (IOException e) {e.printStackTrace();}
}
}
}
复制代码代码如下:
kekosongan publik kode_acak() {
render(JCaptchaRender baru(getSession().getId()));
}