您的位置:首页 > 其它

kaptcha 验证码使用

2015-11-12 16:02 204 查看
本文的原文连接是: 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好用点,另外一个配置文件太多,配置起来比较复杂。没有使用。

代码不多,可以学习下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  验证码