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有所幫助。
新聞熱點
疑難解答