栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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

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

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

1.maven 依赖

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



 com.github.penggle
 kaptcha
 ${kaptcha.version}

2.前端




$(function(){
 $('#kaptchaImage').click(function () {
  $(this).hide().attr('src', '${ctx}/captcha-image?' + Math.floor(Math.random()*100) ).fadeIn();
  event.cancelBubble=true;
 });
 });

3.mvc-context 配置


 
 
  
  
   
   no
   45
   blue
   4
   code
   
  
  
 
 

更多参数:

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

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号