当前位置: 移动技术网 > IT编程>开发语言>Java > Spring MVC中使用Google kaptcha验证码的方法详解

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

前言

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

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

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

1.maven 依赖

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

<!-- google 验证码 -->
<dependency>
 <groupid>com.github.penggle</groupid>
 <artifactid>kaptcha</artifactid>
 <version>${kaptcha.version}</version>
</dependency>

2.前端

<img id="kaptchaimage" src="${pagecontext.request.contextpath}/captcha-image" width="116" height="36">
$(function(){
 $('#kaptchaimage').click(function () {
  $(this).hide().attr('src', '${ctx}/captcha-image?' + math.floor(math.random()*100) ).fadein();
  event.cancelbubble=true;
 });
 });

3.mvc-context 配置

<!--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.服务端

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

request.getsession().getattribute(constants.kaptcha_session_key);

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网