kaptcha 验证码使用
来源:互联网 发布:淘宝壹心表行全是好评 编辑:程序博客网 时间:2024/06/05 10:18
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49800115 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
1,关于
验证码是做互联网必须做的。
目前都是用的第三方库。Kaptcha已经迁移到github项目了。
参考博客:http://www.v5cn.cn/?p=157
2,项目中使用Kaptcha
引入pom
<dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version></dependency><!-- kaptcha依赖--><dependency> <groupId>com.jhlabs</groupId> <artifactId>imaging</artifactId> <version>01012005</version></dependency>
默认Kaptcha使用的是servlet,找到servlet迁移到 srping mvc。
controller代码:
import com.google.code.kaptcha.Producer;import com.google.code.kaptcha.util.Config;import com.octo.captcha.service.CaptchaServiceException;import com.octo.captcha.service.image.ImageCaptchaService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Date;import java.util.Properties;import com.google.code.kaptcha.Constants;@Controller@RequestMapping(value = "/demo", method = {RequestMethod.GET})public class KcaptchaController { public static final String CAPTCHA_IMAGE_FORMAT = "jpeg"; //--kapcha验证码。 private Properties props = new Properties(); private Producer kaptchaProducer = null; private String sessionKeyValue = null; private String sessionKeyDateValue = null; public KcaptchaController() { ImageIO.setUseCache(false); //设置宽和高。 this.props.put(Constants.KAPTCHA_IMAGE_WIDTH, "200"); this.props.put(Constants.KAPTCHA_IMAGE_HEIGHT, "60"); //kaptcha.border:是否显示边框。 this.props.put(Constants.KAPTCHA_BORDER, "no"); //kaptcha.textproducer.font.color:字体颜色 this.props.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"); //kaptcha.textproducer.char.space:字符间距 this.props.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "5"); //设置字体。 this.props.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40"); //this.props.put(Constants.KAPTCHA_NOISE_COLOR, ""); //更多的属性设置可以在com.google.code.kaptcha.Constants类中找到。 Config config1 = new Config(this.props); this.kaptchaProducer = config1.getProducerImpl(); this.sessionKeyValue = config1.getSessionKey(); this.sessionKeyDateValue = config1.getSessionDate(); } @RequestMapping(value = "/kcaptcha.jpg", method = {RequestMethod.GET}) @ResponseBody public void kaptcha(@RequestParam(value = "code", defaultValue = "0") String code, HttpServletResponse response, HttpServletRequest request, Model view) throws IOException { // flush it in the response response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT); String capText = this.kaptchaProducer.createText(); BufferedImage bi = this.kaptchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); try { request.getSession().setAttribute(this.sessionKeyValue, capText); request.getSession().setAttribute(this.sessionKeyDateValue, new Date()); } catch (Exception e) { e.printStackTrace(); } ImageIO.write(bi, CAPTCHA_IMAGE_FORMAT, out); }}
其中还有一个问题,必须在ImageIO关闭之前将数据写到session里面。否则报错。
参数配置都在 com.google.code.kaptcha.Constants 类里面。
只要设定下就可以了。而且可以对规则进行自己的实现。
显示效果如下:
3,总结
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49800115 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
Kaptcha感觉上臂Japtcha好用点,另外一个配置文件太多,配置起来比较复杂。没有使用。
代码不多,可以学习下。
0 0
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha 验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha 验证码使用
- kaptcha验证码使用
- kaptcha验证码使用
- kaptcha验证码使用配置
- spring kaptcha 验证码使用
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- 打包过程vs2010(包含卸载功能、生成一个exe文件)
- 批处理查找方件中的关键词
- C++primer第五版第九章学习笔记
- extView设置跑马灯形式不动
- mysql提高查询速度
- kaptcha 验证码使用
- 【小白自学笔记】【机器学习实战】【Python代码逐行理解】CH02
- 从”JAVA“而终 20:深入理解Java的内省与反射
- break语句和continue语句
- sicily 1318. Magic Square
- Objective-C 之 消息机制详解
- PyQt发布成应用程序中遇到的若干问题
- 设计模式—单例
- wro4j:Java Web资源压缩