您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        前言
众所周知验证码是抵抗批量操作和恶意登录最有效的方式之一,我们在每天或许都会遇到,验证码从产生到现在已经衍生出了很多分支、方式。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);
总结
以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。