jfinal添加jcaptcha验证码实现方法
package com.g.core.common.jcaptcha;
import java.awt.color;
import java.awt.font;
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;
/**
* 生成验证码图片
*/
public class jcaptchaengine extends 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[] {
new font("nyala", font.bold, min_font_size),
new font("arial", font.bold, min_font_size),
new font("bell mt", font.bold, min_font_size),
new font("credit valley", font.bold, min_font_size),
new font("impact", font.bold, min_font_size)
};
// 验证码随机颜色
private static final color[] random_color = new color[] {
new color(255, 255, 255),
new color(255, 220, 220),
new color(220, 255, 255),
new color(220, 220, 255),
new color(255, 255, 220),
new color(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));
}
}
package 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;
public class captchaservicesingleton {
private static imagecaptchaservice instance =null;
public captchaservicesingleton() {
}
// 使用synchronized关键字解决线程不安全
public synchronized static imagecaptchaservice getinstance() {
if (instance == null) {
instance = new defaultmanageableimagecaptchaservice(new fasthashmapcaptchastore(), new jcaptchaengine(), 180,
100000 , 75000);
}
return instance;
}
}
package 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;
public class jcaptcharender extends render {
private string randomcodekey;
public jcaptcharender(string randomcodekey) {
if (stringkit.isblank(randomcodekey))
throw new illegalargumentexception("randomcodekey can not be blank");
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;
try {
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 (exception e) {
throw new runtimeexception(e);
}
finally {
if (sos != null)
try {sos.close();} catch (ioexception e) {e.printstacktrace();}
}
}
}
public void random_code() {
render(new jcaptcharender(getsession().getid()));
}