驗(yàn)證碼定義:
驗(yàn)證碼是一種區(qū)分用戶是計(jì)算機(jī)還是人的公共自動(dòng)程序闲昭。
作用:
可以防止惡意破解密碼康谆,刷票抗果、論壇灌水定页,有效防止某個(gè)黑客對(duì)某一個(gè)特定注冊(cè)用戶用特定程序暴力破解方式進(jìn)行不斷的登錄嘗試。
沒有驗(yàn)證碼會(huì)帶來的一些問題:
- 對(duì)特定用戶不斷登錄破解密碼
- 對(duì)某個(gè)網(wǎng)站創(chuàng)建賬戶
- 對(duì)某個(gè)網(wǎng)站提交垃圾數(shù)據(jù)
- 對(duì)某個(gè)網(wǎng)站刷票
1酗电、使用Servlet實(shí)現(xiàn)
開源組件實(shí)現(xiàn)驗(yàn)證碼
- 使用Jcaptcha組件實(shí)現(xiàn)驗(yàn)證碼
jcaptcha的使用比較簡(jiǎn)單:
1:下載并將其JAR放到類路徑下
2:配置web.xml
3:寫一個(gè)驗(yàn)證驗(yàn)證碼的請(qǐng)求方法
web配置代碼
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>
com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet
</servlet-class>
</servlet>
- 使用Kaptcha組件使用驗(yàn)證碼
下載kaptcha-2.3魄藕。jar
將jar包導(dǎo)入項(xiàng)目
編寫頁(yè)面
配置web.xml
啟動(dòng)項(xiàng)目
web.xml配置
<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>
com.google.code.kaptchaServlet
</servlet-class>
</servlet>
插件KaptchaServlet獲取session中的驗(yàn)證碼
String captcha = ((String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY));
邊框顏色:rgb值也可以
文本實(shí)現(xiàn)類:kaptcha.textproducer.impl 值為:com.google.code.kaptcha.text.impl.DefaultTextCreator
驗(yàn)證碼值配置:kaptcha.textproducer.char.string 值:abcde2345678gfynmnpwx(隨意你自己寫)
驗(yàn)證碼的長(zhǎng)度:kaptcha.textproducer.char.length 值:
字體:kaptcha.textproducer.font.names 值:Arial,Courier
字體顏色:kaptcha.textproducer.font.color 值: r,g,b 或者 white,black,blue
文字間隔:kaptcha.textproducer.char.space 值:
干擾實(shí)現(xiàn)類:kaptcha.noise.impl 值:com.google.code.kaptcha.impl.DefaultNoise/NoNoise
干擾顏色:kaptcha.noise.color 值: r,g,b 或者 white,black,blue
圖片樣式:kaptcha.obscurificator.impl 值:
水紋com.google.code.kaptcha.impl.WaterRipple;
魚眼com.google.code.kaptcha.impl.FishEyeGimpy撵术;
陰影com.google.code.kaptcha.impl.ShadowGimpy
背景實(shí)現(xiàn)類:kaptcha.background.impl 值:com.google.code.kaptcha.impl.DefaultBackground
背景漸變色開始:kaptcha.background.clear.from 值:r,g,b 或者 white,black,blue
背景漸變色結(jié)束:kaptcha.background.clear.to 值:r,g,b 或者 white,black,blue
文字渲染器:kaptcha.word.impl 值:com.google.code.kaptcha.text.impl.DefaultWordRenderer
session中存放驗(yàn)證碼的key鍵:kaptcha.session.key 值:KAPTCHA_SESSION_KEY