复制代码代码如下:
Paket com.g.core.common.JCaptcha;
import java.awt.Color;
java.awt.Font importieren;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomListColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
/**
* 生成验证码图片
*/
öffentliche Klasse JCaptchaEngine erweitert 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/";// 随机背景图片路径
// 验证码随机字体
private static final Font[] RANDOM_FONT = new Font[] {
neue Schriftart("nyala", Font.BOLD, MIN_FONT_SIZE),
neue Schriftart("Arial", Font.BOLD, MIN_FONT_SIZE),
neue Schriftart("Bell MT", Font.BOLD, MIN_FONT_SIZE),
neue Schriftart("Credit Valley", Font.BOLD, MIN_FONT_SIZE),
neue Schriftart("Impact", Font.BOLD, MIN_FONT_SIZE)
};
// 验证码随机颜色
private statische endgültige Farbe[] RANDOM_COLOR = neue Farbe[] {
neue Farbe(255, 255, 255),
neue Farbe(255, 220, 220),
neue Farbe(220, 255, 255),
neue Farbe(220, 220, 255),
neue Farbe(255, 255, 220),
neue Farbe (220, 255, 220)
};
// 生成验证码
@Override
protected void buildInitialFactories() {
RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR);
BackgroundGenerator backgroundGenerator = 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 ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
addFactory(new GimpyFactory(wordGenerator, wordToImage));
}
}
复制代码代码如下:
Paket com.g.core.common.JCaptcha;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
öffentliche Klasse CaptchaServiceSingleton {
private statische ImageCaptchaService-Instanz =null;
public CaptchaServiceSingleton() {
}
// 使用synchronized关键字解决线程不安全
öffentlicher synchronisierter statischer ImageCaptchaService getInstance() {
if (instance == null) {
Instanz = new DefaultManageableImageCaptchaService(new FastHashMapCaptchaStore(), new JCaptchaEngine(), 180,
100000, 75000);
}
Rückgabeinstanz;
}
}
复制代码代码如下:
Paket com.g.core.render;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import com.g.core.common.JCaptcha.CaptchaServiceSingleton;
import com.jfinal.kit.StringKit;
import com.jfinal.render.Render;
öffentliche Klasse JCaptchaRender erweitert Render {
privater String randomCodeKey;
public JCaptchaRender(String randomCodeKey) {
if (StringKit.isBlank(randomCodeKey))
throw new IllegalArgumentException("randomCodeKey darf nicht leer sein");
this.randomCodeKey = randomCodeKey;
}
@Override
public void render() {
Response.setHeader("Cache-Control", "no-store");
Response.setHeader("Pragma", "no-cache");
Response.setDateHeader("Expires", 0);
Response.setContentType("image/jpeg");
ServletOutputStream sos = null;
versuchen {
sos = Response.getOutputStream();
//String captchaId = request.getSession(true).getId();
BufferedImage Challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(randomCodeKey, request.getLocale());
ImageIO.write(challenge, "jpg", sos);
sos.flush();
} Catch (Ausnahme e) {
throw new RuntimeException(e);
}
Endlich {
if (sos != null)
Versuchen Sie es mit {sos.close();} Catch (IOException e) {e.printStackTrace();}
}
}
}
复制代码代码如下:
public void random_code() {
render(new JCaptchaRender(getSession().getId()));
}