spring mvc 使用kaptcha配置生成驗(yàn)證碼實(shí)例
使用Kaptcha 生成驗(yàn)證碼十分簡(jiǎn)單并且參數(shù)可以進(jìn)行自定義,以下簡(jiǎn)單記錄下使用步驟。
1.在pom.xml中添加maven依賴:
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency>
2.web.xml中配置kaptcha屬性:
<bean id="verifyCodeProducer" 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">yes</prop> <prop key="kaptcha.border.color">105,179,90</prop> <prop key="kaptcha.border.thickness">1</prop> <prop key="kaptcha.noise.color">blue</prop> <prop key="kaptcha.image.width">150</prop> <prop key="kaptcha.image.height">50</prop> <prop key="kaptcha.session.key">verifyCode</prop> <!-- <prop key="kaptcha.textproducer.char.string">0123456789abcdefghijklmnopqrst!@#$%^*</prop> --> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.char.space">4</prop> <prop key="kaptcha.textproducer.font.size">30</prop> <prop key="kaptcha.textproducer.font.color">blue</prop> </props> </constructor-arg> </bean> </property> </bean>
其中bean節(jié)點(diǎn)的id值 verifyCodeProducer 為在類中引用@Resource生成實(shí)例時(shí)的名稱;屬性配置中 kaptcha.session.key 的值為在session中存取名稱。
在servlet節(jié)點(diǎn)中配置
3.controller類中的相關(guān)方法:
@Controller public class CommonController { @Autowired private Producer verifyCodeProducer; @RequestMapping(path = "/getVerifyCodeImage", method = RequestMethod.GET) public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); ResponseUtils.noCache(response); response.setContentType("image/jpeg"); String capText = verifyCodeProducer.createText(); session.setAttribute(Constants.SESSION_KEY_VERIFY_CODE, capText); BufferedImage bi = verifyCodeProducer.createImage(capText); ServletOutputStream out = null; try { out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); out.flush(); } catch (Exception ex) { LOGGER.error("Failed to produce the verify code image: ", ex); throw new ServerInternalException("Cannot produce the verify code image."); } finally { IOUtils.closeQuietly(out); } } }
Constants.SESSION_KEY_VERIFY_CODE為屬性配置中 kaptcha.session.key 的值。
4.jsp:
<div class="form-group has-feedback"> <span class="glyphicon glyphicon-barcode form-control-feedback"></span> <input id="verifyCode" name="verifyCode" type="text" maxlength="4" class="form-control" placeholder="<spring:message code='login.label.code' />" /> <div style="height: 1px"></div> <img src="${pageContext.request.contextPath}/getVerifyCodeImage" id="verifyCodeImage" style="margin-bottom: -3px" /> <a href="#" rel="external nofollow" onclick="changeVerifyCode()"><spring:message code='login.code.tip' /></a> </div>
function changeVerifyCode() { $('#verifyCodeImage').hide().attr('src', '${pageContext.request.contextPath}/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); event.cancelBubble=true; }
5.kaptcha屬性說明:
- kaptcha.border.color 邊框顏色 默認(rèn)為Color.BLACK
- kaptcha.border.thickness 邊框粗細(xì)度 默認(rèn)為1
- kaptcha.producer.impl 驗(yàn)證碼生成器 默認(rèn)為DefaultKaptcha
- kaptcha.textproducer.impl 驗(yàn)證碼文本生成器 默認(rèn)為DefaultTextCreator
- kaptcha.textproducer.char.string 驗(yàn)證碼文本字符內(nèi)容范圍 默認(rèn)為abcde2345678gfynmnpwx
- kaptcha.textproducer.char.length 驗(yàn)證碼文本字符長(zhǎng)度 默認(rèn)為5
- kaptcha.textproducer.font.names 驗(yàn)證碼文本字體樣式 默認(rèn)為new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
- kaptcha.textproducer.font.size 驗(yàn)證碼文本字符大小 默認(rèn)為40
- kaptcha.textproducer.font.color 驗(yàn)證碼文本字符顏色 默認(rèn)為Color.BLACK
- kaptcha.textproducer.char.space 驗(yàn)證碼文本字符間距 默認(rèn)為2
- kaptcha.noise.impl 驗(yàn)證碼噪點(diǎn)生成對(duì)象 默認(rèn)為DefaultNoise
- kaptcha.noise.color 驗(yàn)證碼噪點(diǎn)顏色 默認(rèn)為Color.BLACK
- kaptcha.obscurificator.impl 驗(yàn)證碼樣式引擎 默認(rèn)為WaterRipple
- kaptcha.word.impl 驗(yàn)證碼文本字符渲染 默認(rèn)為DefaultWordRenderer
- kaptcha.background.impl 驗(yàn)證碼背景生成器 默認(rèn)為DefaultBackground
- kaptcha.background.clear.from 驗(yàn)證碼背景顏色漸進(jìn) 默認(rèn)為Color.LIGHT_GRAY
- kaptcha.background.clear.to 驗(yàn)證碼背景顏色漸進(jìn) 默認(rèn)為Color.WHITE
- kaptcha.image.width 驗(yàn)證碼圖片寬度 默認(rèn)為200
- kaptcha.image.height 驗(yàn)證碼圖片高度 默認(rèn)為50
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springMVC實(shí)現(xiàn)圖形驗(yàn)證碼(kaptcha)代碼實(shí)例
- Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
- SpringBoot集成kaptcha驗(yàn)證碼
- springboot整合kaptcha驗(yàn)證碼的示例代碼
- SpringMvc使用GoogleKaptcha生成驗(yàn)證碼
- SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解
- 登陸驗(yàn)證碼kaptcha結(jié)合spring boot的用法詳解
- Spring boot如何集成kaptcha并生成驗(yàn)證碼
相關(guān)文章
redisson.tryLock()參數(shù)的使用及理解
這篇文章主要介紹了redisson.tryLock()參數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04淺談java線程中生產(chǎn)者與消費(fèi)者的問題
下面小編就為大家?guī)硪黄獪\談java線程中生產(chǎn)者與消費(fèi)者的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07Java設(shè)置Access-Control-Allow-Origin允許多域名訪問的實(shí)現(xiàn)方法
這篇文章主要介紹了Java設(shè)置Access-Control-Allow-Origin允許多域名訪問的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10springboot下ueditor上傳功能的實(shí)現(xiàn)及遇到的問題
這篇文章主要介紹了springboot下ueditor上傳功能的實(shí)現(xiàn)及遇到的問題,本文分步驟通過實(shí)例截圖給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Java使用原型模式展現(xiàn)每日生活應(yīng)用案例詳解
這篇文章主要介紹了Java使用原型模式展現(xiàn)每日生活應(yīng)用案例,較為詳細(xì)的分析了原型模式的概念、原理及Java使用原型模式展現(xiàn)每日生活案例的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2018-05-05