This article mainly introduces spring mvc using kaptcha to generate verification code examples, and introduces in detail the steps of using Kaptcha to generate verification codes. Those who are interested can learn more
Using Kaptcha to generate verification codes is very simple and The parameters can be customized, and the usage steps are simply recorded below.
1. Add maven dependency in pom.xml:
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency>
2. Configure kaptcha attribute in web.xml:
<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>
The id value of the bean node verifyCodeProducer is the name when referenced in the class@Resource; kaptcha.session.key in the attribute configuration The value is the access name in the session.
Configure in the servlet node
##3. Related methods in the controller class:
@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); } } }
4.jsp:
<p 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' />" /> <p style="height: 1px"></p> <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> </p>
function changeVerifyCode() { $('#verifyCodeImage').hide().attr('src', '${pageContext.request.contextPath}/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); event.cancelBubble=true; }
5.kaptcha attribute description:
Generator The default is DefaultKaptcha
##kaptcha.noise.impl Verification code noise generation
Objectkaptcha.noise.color Verification code noise color Default is Color.BLACK
kaptcha.obscurificator.impl Verification code style engine Default is WaterRipple
kaptcha.word.impl Verification code text character rendering Default is DefaultWordRenderer
kaptcha.background.impl Verification code background generator Default is DefaultBackground
kaptcha.background.clear.from Verification code background color gradient Default is Color.LIGHT_GRAY
kaptcha.background.clear.to Verification code background color gradient Default is Color.WHITE
Image
Width Default It is 200kaptcha.image.height Verification code image height The default is 50
Java Video Tutorial on Implementing Equal-proportional Thumbnails for Images
The above is the detailed content of Spring code example generated through kaptcha configuration verification code. For more information, please follow other related articles on the PHP Chinese website!