Spring MVC中使用Google kaptcha驗(yàn)證碼的方法詳解
前言
眾所周知驗(yàn)證碼是抵抗批量操作和惡意登錄最有效的方式之一,我們?cè)诿刻旎蛟S都會(huì)遇到,驗(yàn)證碼從產(chǎn)生到現(xiàn)在已經(jīng)衍生出了很多分支、方式。google kaptcha 是一個(gè)非常實(shí)用的驗(yàn)證碼生成類庫。
通過靈活的配置生成各種樣式的驗(yàn)證碼,并將生成的驗(yàn)證碼字符串放到 HttpSession 中,方便獲取進(jìn)行比較。
本文描述在 spring mvc 下快速的將 google kaptcha 集成到項(xiàng)目中(單獨(dú)使用的話在 web.xml 中配置 KaptchaServlet)。下面話不多說了,來一起看看詳細(xì)的介紹吧。
1.maven 依賴
官方提供的 pom 無法正常使用,使用阿里云倉庫對(duì)應(yīng) kaptcha。
<!-- google 驗(yàn)證碼 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>
2.前端
<img id="kaptchaImage" src="${pageContext.request.contextPath}/captcha-image" width="116" height="36">
$(function(){
$('#kaptchaImage').click(function () {
$(this).hide().attr('src', '${ctx}/captcha-image?' + Math.floor(Math.random()*100) ).fadeIn();
event.cancelBubble=true;
});
});
3.mvc-context 配置
<!--goole captcha 驗(yàn)證碼配置--> <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <prop key="kaptcha.border">no</prop> <prop key="kaptcha.textproducer.font.size">45</prop> <prop key="kaptcha.textproducer.font.color">blue</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.session.key">code</prop> </props> </constructor-arg> </bean> </property> </bean>
更多參數(shù):
| Constant | 描述 | 默認(rèn)值 |
| kaptcha.border | 圖片邊框,合法值:yes , no | yes |
| kaptcha.border.color | 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
| kaptcha.border.thickness | 邊框厚度,合法值:>0 | 1 |
| kaptcha.image.width | 圖片寬 | 200 |
| kaptcha.image.height | 圖片高 | 50 |
| kaptcha.producer.impl | 圖片實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultKaptcha |
| kaptcha.textproducer.impl | 文本實(shí)現(xiàn)類 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
| kaptcha.textproducer.char.string | 文本集合,驗(yàn)證碼值從此集合中獲取 | abcde2345678gfynmnpwx |
| kaptcha.textproducer.char.length | 驗(yàn)證碼長度 | 5 |
| kaptcha.textproducer.font.names | 字體 | Arial, Courier |
| kaptcha.textproducer.font.size | 字體大小 | 40px |
| kaptcha.textproducer.font.color | 字體顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.textproducer.char.space | 文字間隔 | 2 |
| kaptcha.noise.impl | 干擾實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultNoise |
| kaptcha.noise.color | 干擾顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.obscurificator.impl | 圖片樣式: 水紋com.google.code.kaptcha.impl.WaterRipple 魚眼com.google.code.kaptcha.impl.FishEyeGimpy 陰影com.google.code.kaptcha.impl.ShadowGimpy |
com.google.code.kaptcha.impl.WaterRipple |
| kaptcha.background.impl | 背景實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultBackground |
| kaptcha.background.clear.from | 背景顏色漸變,開始顏色 | light grey |
| kaptcha.background.clear.to | 背景顏色漸變,結(jié)束顏色 | white |
| kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
| kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
| kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
4.服務(wù)端
@Controller
public class CaptchaController {
private final Producer captchaProducer;
@Autowired
public CaptchaController(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
@RequestMapping(value = "captcha-image")
public String getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
5.session 中獲取驗(yàn)證碼
request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
總結(jié)
以上就是這篇文章的全部內(nèi)容了,本文還有許多不足,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java生產(chǎn)1-100的隨機(jī)數(shù)簡單實(shí)例(分享)
下面小編就為大家?guī)硪黄狫ava生產(chǎn)1-100的隨機(jī)數(shù)簡單實(shí)例(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
SpringBoot項(xiàng)目打成war和jar的區(qū)別說明
這篇文章主要介紹了SpringBoot項(xiàng)目打成war和jar的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式
這篇文章主要介紹了Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Javaweb實(shí)現(xiàn)上傳下載文件的多種方法
本篇文章主要介紹了Javaweb實(shí)現(xiàn)上傳下載文件,有多種實(shí)現(xiàn)方式,需要的朋友可以參考下。2016-10-10
關(guān)于java數(shù)組與字符串相互轉(zhuǎn)換的問題
這篇文章主要介紹了java數(shù)組與字符串相互轉(zhuǎn)換的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
Java類和對(duì)象習(xí)題及詳細(xì)答案解析
這篇文章主要介紹了Java類和對(duì)象的相關(guān)知識(shí),包括局部變量初始化、靜態(tài)方法、靜態(tài)導(dǎo)入、構(gòu)造方法、代碼塊執(zhí)行順序、toString方法重寫、類變量和靜態(tài)成員變量的訪問等,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
SpringMVC 單文件,多文件上傳實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringMVC 單文件,多文件上傳實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

