淺析Java驗證碼生成庫JCaptcha。本站提示廣大學習愛好者:(淺析Java驗證碼生成庫JCaptcha)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析Java驗證碼生成庫JCaptcha正文
JCaptcha長短常壯大的,不但是可以生成圖片式的驗證碼,還可以生成聲響式的(新浪就應用了兩重驗證碼)。本文簡略的引見了JCaptcha庫和應用實例,上面一路來看看。
下載JCaptcha庫
maven依附如斯添加:
<dependency>
<groupId>com.octo.captcha</groupId>
<artifactId>jcaptcha</artifactId>
<version>1.0</version>
</dependency>
封裝了一個簡略的類
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.CaptchaEngine;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import java.awt.*;
/**
* 發生驗證碼圖片的類
*/
public class CapchaHelper {
private static final Integer MIN_WORD_LENGTH = 4;// 驗證碼最小長度
private static final Integer MAX_WORD_LENGTH = 4;// 驗證碼最年夜長度
private static final Integer IMAGE_HEIGHT = 30;// 驗證碼圖片高度
private static final Integer IMAGE_WIDTH = 130;// 驗證碼圖片寬度
private static final Integer MIN_FONT_SIZE = 15;// 驗證碼最小字體
private static final Integer MAX_FONT_SIZE = 15;// 驗證碼最年夜字體
private static final String RANDOM_WORD = "0123456789";// 隨機字符
// 驗證碼隨機字體
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)
};
private static ListImageCaptchaEngine captchaEngine;
public static CaptchaEngine getCaptchaEngine(final String imgPath) {
if (captchaEngine == null) {
synchronized (CapchaHelper.class) {
if (captchaEngine == null && imgPath != null) {
captchaEngine = new ListImageCaptchaEngine() {
@Override
protected void buildInitialFactories() {
RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR);
BackgroundGenerator backgroundGenerator = new FileReaderRandomBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, imgPath);
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));
}
};
}
}
}
return captchaEngine;
}
}
呼應網頁中對驗正碼圖象的要求
可以界說一個servlet呼應這個要求,假如用springMVC,也能夠用某個Controller中的某個辦法呼應這個要求,不論如何,都須要指定一個途徑對應到servlet或controller的辦法,好比途徑是:”/aaa/captcha”
那末在呼應對這個途徑的要求的Servlet中可以如許寫:
//獲得驗證碼配景圖片的途徑,這途徑放了許多作為配景的圖象
String captcha_backgrounds = session.getServletContext().getRealPath("/WEB-INF/img/captcha");
CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds);
//須要admin網頁頂用js准時從辦事端獲得以後的驗證碼
Captcha captcha = ce.getNextCaptcha();
//為了驗證,把captcha對象放到session中,以在客戶端提交驗證碼時停止驗證
req.getSession().setAttribute("captcha", captcha);
//獲得驗證碼圖片,這是未緊縮的位圖
BufferedImage image = (BufferedImage) captcha.getChallenge();
resp.setContentType("image/jpeg");
ImageIO.write(image, "jpg", resp.getOutputStream());
假如用springMVC,就如許寫:
//獲得驗證碼配景圖片的途徑,這途徑放了許多作為配景的圖象
String captcha_backgrounds = session.getServletContext().getRealPath("/WEB-INF/img/captcha");
CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds);
//須要admin網頁頂用js准時從辦事端獲得以後的驗證碼
Captcha captcha = ce.getNextCaptcha();
//為了驗證,把captcha對象放到session中,以在客戶端提交驗證碼時停止驗證
session.setAttribute("captcha", captcha);
//獲得驗證碼圖片,這是未緊縮的位圖
BufferedImage image = (BufferedImage) captcha.getChallenge();
ByteArrayOutputStream bao=new ByteArrayOutputStream();
//應縮成jpg並寫到輸入流中
ImageIO.write(image, "jpg", bao);
return bao.toByteArray();
這兩種方法,向客戶端前往的都是二進制數據。
String captcha_backgrounds = session.getServletContext().getRealPath(“/WEB-INF/img/captcha”);
表現途徑/WEB-INF/img/captcha上面放的是作為驗證碼圖象的配景的多張圖片,必需是jpeg,年夜小能夠沒限制,你可以本身試一下。
網頁頂用 <IMG> 指向這個地址
<img id="captcha" src="/captcha_img" onclick="refreshCaptchaImg()" />
js函數refreshCaptchaImg()呼應圖片的點擊,每點擊一次,就從新獲得一張新的驗證碼圖片。若何從新獲得驗正碼圖片呢?
只需轉變img的src屬性,但這裡是每次都是用統一個地址來設置這個屬性,如許不會惹起真實的刷新,所以辦法refreshCaptchaImg()是如許完成的:
function refreshCaptchaImg() {
//從辦事端從新下載驗證碼圖片
//給這個地加參數純為了強迫刷新,不然因為src指向的url地址沒變,閱讀器不會真正生刷新圖片
var now = new Date()
$("#captcha").attr("src","/captcha_img?"+now.getTime());
以上就是Java中驗證碼生成庫JCaptcha的引見及應用,願望對年夜家進修java有所贊助。