Spring MVC中使用Google kaptcha验证码的方法详解

2025-05-29 0 79

前言

众所周知验证码是抵抗批量操作和恶意登录最有效的方式之一,我们在每天或许都会遇到,验证码从产生到现在已经衍生出了很多分支、方式。google kaptcha 是一个非常实用的验证码生成类库。

通过灵活的配置生成各种样式的验证码,并将生成的验证码字符串放到 HttpSession 中,方便获取进行比较。

本文描述在 spring mvc 下快速的将 google kaptcha 集成到项目中(单独使用的话在 web.xml 中配置 KaptchaServlet)。下面话不多说了,来一起看看详细的介绍吧。

1.maven 依赖

官方提供的 pom 无法正常使用,使用阿里云仓库对应 kaptcha

?

1

2

3

4

5

6
<!-- google 验证码 -->

<dependency>

<groupId>com.github.penggle</groupId>

<artifactId>kaptcha</artifactId>

<version>${kaptcha.version}</version>

</dependency>

2.前端

?

1
<img id="kaptchaImage" src="${pageContext.request.contextPath}/captcha-image" width="116" height="36">

?

1

2

3

4

5

6
$(function(){

$('#kaptchaImage').click(function () {

$(this).hide().attr('src', '${ctx}/captcha-image?' + Math.floor(Math.random()*100) ).fadeIn();

event.cancelBubble=true;

});

});

3.mvc-context 配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
<!--goole captcha 验证码配置-->

<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>

更多参数:

Constant 描述 默认值
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 图片实现类 com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl 文本实现类 com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string 文本集合,验证码值从此集合中获取 abcde2345678gfynmnpwx
kaptcha.textproducer.char.length 验证码长度 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 干扰实现类 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 背景实现类 com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from 背景颜色渐变,开始颜色 light grey
kaptcha.background.clear.to 背景颜色渐变,结束颜色 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.服务端

?

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

31

32
@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 中获取验证码

?

1
request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);

总结

以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。

原文链接:http://www.cnblogs.com/java-class/p/7597591.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring MVC中使用Google kaptcha验证码的方法详解 https://www.kuaiidc.com/114349.html

相关文章

发表评论
暂无评论