您好,登錄后才能下訂單哦!
使用Kaptcha 生成驗(yàn)證碼十分簡單并且參數(shù)可以進(jì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 ></div> <img src="${pageContext.request.contextPath}/getVerifyCodeImage" id="verifyCodeImage" /> <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屬性說明:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。