kaptcha验证码

Posted 起个名字好难

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kaptcha验证码相关的知识,希望对你有一定的参考价值。

 

@Action("/validimg")
public String validimg() throws Exception {
        genernateCaptchaImage();
        return null;
}

 

 

protected void genernateCaptchaImage() throws IOException {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream out = response.getOutputStream();
        try {
            String captchaId = request.getSession(true).getId();
            LOGGER.info("当前生产验证码的sessionId为: "+captchaId);
            BufferedImage challenge = (BufferedImage) CaptchaServiceSingleton.getInstance().getChallengeForID(
                    captchaId, request.getLocale());
            ImageIO.write(challenge, "jpg", out);
            out.flush();
        } catch (CaptchaServiceException e) {
        } finally {
            out.close();
        }
    }

 

 

 

/**
 * JCaptcha的单例
 * 
 * @author fuxingxign
 */
public class CaptchaServiceSingleton {
    private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService(
            new FastHashMapCaptchaStore(), new RdImageEngine(), 180, 100000, 75000);

    /*
     * private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService( new
     * FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000 , 75000);
     */
    public static ImageCaptchaService getInstance() {
        return instance;
    }
}

 

 

 

 

import java.awt.Color;
import java.awt.Font;

import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.RandomRangeColorGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
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 RdImageEngine extends ListImageCaptchaEngine {
    @Override
    protected void buildInitialFactories() {
        WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz");
        RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(new int[] { 0, 100 }, new int[] { 0, 100 },
                new int[] { 0, 100 });
        TextPaster textPaster = new RandomTextPaster(Integer.valueOf(4),Integer.valueOf(4), cgen, true);
        BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(Integer.valueOf(80), Integer.valueOf(37),
                Color.WHITE);
        Font[] fontsList = new Font[] { new Font("Arial", 0, 12), new Font("Tahoma", 0, 12),
                new Font("Verdana", 0, 12), };
        FontGenerator fontGenerator = new RandomFontGenerator(Integer.valueOf(26),Integer.valueOf(26), fontsList);

        WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
        this.addFactory(new GimpyFactory(wgen, wordToImage));
    }

}

 

以上是关于kaptcha验证码的主要内容,如果未能解决你的问题,请参考以下文章

火狐浏览器kaptcha验证码点击无法刷新问题解决方法

kaptcha验证码框架

linux下Kaptcha生成的验证码图片文字不显示

java 实现登录验证码 (kaptcha 验证码组件)

work_04_谷歌验证码工具Kaptcha

Kaptcha 变种验证码一键攻破