使用Kaptcha 生成验证码十分简单并且参数可以进行自定义,以下简单记录下使用步骤。
1.在pom.xml中添加maven依赖:
1
2
3
4
5
6
|
< dependency > < artifactId >kaptcha</ artifactId > < version >2.3</ version > < classifier >jdk15</ classifier > </ dependency > |
2.web.xml中配置kaptcha属性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
< 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节点的id值 verifyCodeProducer 为在类中引用@Resource生成实例时的名称;属性配置中 kaptcha.session.key 的值为在session中存取名称。
在servlet节点中配置
3.controller类中的相关方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@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:
1
2
3
4
5
6
7
|
< 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 > |
1
2
3
4
|
function changeVerifyCode() { $( '#verifyCodeImage' ).hide().attr( 'src' , '${pageContext.request.contextPath}/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); event.cancelBubble= true ; } |
5.kaptcha属性说明:
- kaptcha.border.color 边框颜色 默认为Color.BLACK
- kaptcha.border.thickness 边框粗细度 默认为1
- kaptcha.producer.impl 验证码生成器 默认为DefaultKaptcha
- kaptcha.textproducer.impl 验证码文本生成器 默认为DefaultTextCreator
- kaptcha.textproducer.char.string 验证码文本字符内容范围 默认为abcde2345678gfynmnpwx
- kaptcha.textproducer.char.length 验证码文本字符长度 默认为5
- kaptcha.textproducer.font.names 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
- kaptcha.textproducer.font.size 验证码文本字符大小 默认为40
- kaptcha.textproducer.font.color 验证码文本字符颜色 默认为Color.BLACK
- kaptcha.textproducer.char.space 验证码文本字符间距 默认为2
- kaptcha.noise.impl 验证码噪点生成对象 默认为DefaultNoise
- kaptcha.noise.color 验证码噪点颜色 默认为Color.BLACK
- kaptcha.obscurificator.impl 验证码样式引擎 默认为WaterRipple
- kaptcha.word.impl 验证码文本字符渲染 默认为DefaultWordRenderer
- kaptcha.background.impl 验证码背景生成器 默认为DefaultBackground
- kaptcha.background.clear.from 验证码背景颜色渐进 默认为Color.LIGHT_GRAY
- kaptcha.background.clear.to 验证码背景颜色渐进 默认为Color.WHITE
- kaptcha.image.width 验证码图片宽度 默认为200
- kaptcha.image.height 验证码图片高度 默认为50
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/aleefang/article/details/70231335