jfinal添加jcaptcha验证码实现方法

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

复制代码 代码如下:

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()));
 }

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多