kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。
使用kaptcha可以方便的配置:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)
验证码的样式(鱼眼样式、3D、普通模糊……
下面介绍一下用法:
一. 依赖
<!-- 验证码 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二.配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <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.image.width">110</prop> <!-- 验证码高度 --> <prop key="kaptcha.image.height">50</prop> <!-- 生成验证码内容范围 --> <prop key="kaptcha.textproducer.char.string">123456789abcDeFGHjkLmnoQqrsTUVWXYZ</prop> <!-- 验证码个数 --> <prop key="kaptcha.textproducer.char.length">4</prop> <!-- 是否有边框 --> <prop key="kaptcha.border">no</prop> <!-- 边框颜色 --> <prop key="kaptcha.border.color">105,179,90</prop> <!-- 边框厚度 --> <prop key="kaptcha.border.thickness">1</prop> <!-- 验证码字体颜色 --> <prop key="kaptcha.textproducer.font.color">black</prop> <!-- 验证码字体大小 --> <prop key="kaptcha.textproducer.font.size">30</prop> <!-- 验证码所属字体样式 --> <prop key="kaptcha.textproducer.font.names">微软雅黑</prop> <!-- 干扰线颜色 --> <prop key="kaptcha.noise.color">black</prop> <!-- 验证码文本字符间距 --> <prop key="kaptcha.textproducer.char.space">3</prop> <!-- 图片样式 :水纹 --> <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop> </props> </constructor-arg> </bean> </property> </bean> </beans>
三.样式
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 |
四.项目运用
<div class="form-group"> <input type="text" placeholder="请输入验证码" id="yzm"> <img id="captcha_img" alt="点击更换" title="点击更换" onclick="refresh()" src="kaptcha.jpg" /> </div>如果想设置点击图片更换验证码,可以加上如下js,需要jquery
<script type="text/javascript"> function refresh() { document.getElementById('captcha_img').src="kaptcha.jpg?"+Math.random(); } </script>
/** * @author Ray * @date 2018/6/19 0019 * 随机验证码控制层 * 使用kaptcha-2.3.2.jar支持 */ @Controller public class CodeController { @Autowired private Producer producer = null; @RequestMapping("/kaptcha") public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); // 禁止服务器缓存 response.setDateHeader("Expires",0); // 设置标准的 HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // 设置IE扩展 HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // 设置标准 HTTP/1.0 不缓存图片 response.setHeader("Pragma", "no-cache"); // 返回一个 jpeg图片, 默认是text/html response.setContentType("image/jpeg"); // 生成验证码 String capText = producer.createText(); // 为图片创建文本 session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); // 将文本保存在session中 // 向客户端写出 BufferedImage bufferedImage = producer.createImage(capText); // 创建带有文本的图片 ServletOutputStream outputStream = response.getOutputStream(); // 图片数据输出 ImageIO.write(bufferedImage, "jpg", outputStream); try{ outputStream.flush(); }finally { outputStream.close(); } } }
/** * @author Ray * @date 2018/6/19 0019 * 比对验证码 */ public class CodeUtil { /** * 将获取到的前端参数转为String类型 * @param request * @param key * @return */ public static String getString(HttpServletRequest request, String key){ try{ String result = request.getParameter(key); if (result != null) { result = result.trim(); } if("".equals(result)){ result = null; } return result; }catch (Exception e){ return null; } } public static boolean checkVerifyCode(HttpServletRequest request){ // 获取生成的验证码 String sessionyzm = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY); System.out.println("sessionyzm: " + sessionyzm); // 获取用户输入的验证码 String yzm = CodeUtil.getString(request, "yzm"); System.out.println("yzm: " + yzm); if(yzm == null || !yzm.equals(sessionyzm)){ return false; } return true; } }
五.运行效果