驗證碼定義:
驗證碼是一種區分用戶是計算機還是人的公共自動程序。
作用:
可以防止惡意破解密碼,刷票、論壇灌水,有效防止某個黑客對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登錄嘗試。
沒有驗證碼會帶來的一些問題:
- 對特定用戶不斷登錄破解密碼
- 對某個網站創建賬戶
- 對某個網站提交垃圾數據
- 對某個網站刷票
1、使用Servlet實現
開源組件實現驗證碼
- 使用Jcaptcha組件實現驗證碼
jcaptcha的使用比較簡單:
1:下載并將其JAR放到類路徑下
2:配置web.xml
3:寫一個驗證驗證碼的請求方法
web配置代碼
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>
com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet
</servlet-class>
</servlet>
- 使用Kaptcha組件使用驗證碼
下載kaptcha-2.3。jar
將jar包導入項目
編寫頁面
配置web.xml
啟動項目
web.xml配置
<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>
com.google.code.kaptchaServlet
</servlet-class>
</servlet>
插件KaptchaServlet獲取session中的驗證碼
String captcha = ((String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY));
邊框顏色:rgb值也可以
文本實現類:kaptcha.textproducer.impl 值為:com.google.code.kaptcha.text.impl.DefaultTextCreator
驗證碼值配置:kaptcha.textproducer.char.string 值:abcde2345678gfynmnpwx(隨意你自己寫)
驗證碼的長度:kaptcha.textproducer.char.length 值:
字體:kaptcha.textproducer.font.names 值:Arial,Courier
字體顏色:kaptcha.textproducer.font.color 值: r,g,b 或者 white,black,blue
文字間隔:kaptcha.textproducer.char.space 值:
干擾實現類: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
背景實現類:kaptcha.background.impl 值:com.google.code.kaptcha.impl.DefaultBackground
背景漸變色開始:kaptcha.background.clear.from 值:r,g,b 或者 white,black,blue
背景漸變色結束:kaptcha.background.clear.to 值:r,g,b 或者 white,black,blue
文字渲染器:kaptcha.word.impl 值:com.google.code.kaptcha.text.impl.DefaultWordRenderer
session中存放驗證碼的key鍵:kaptcha.session.key 值:KAPTCHA_SESSION_KEY