Kaptcha 是Google的一款可高度配置的实用验证码生成工具,验证码生成的各种样式可通过配置来控制
使用环境主要为Spring + SpringMVC
Kaptcha版本:kaptcha-2.3.2.jar
maven 中央库 artifact
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
对于Spring及SpringMVC当前网上应该有很多,这里就不再一一介绍了,这里就只介绍Kaptcha的在applicationContext.xml
中的配置了
在applicationContext.xml中添加如下配置其中"${}"中的字段类型为properties文件中的配置字段,这种设置方式更自由灵活。
<!-- kaptcha验证码配置 -->
<bean id="kaptchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<!-- 这里的颜色只支持标准色和rgb颜色,不可使用十六进制的颜色 -->
<!-- 是否有边框 -->
<prop key="kaptcha.border">no</prop>
<!-- 验证码文本字符颜色 -->
<prop key="kaptcha.textproducer.font.color">blue</prop>
<!-- 验证码图片宽度 默认为200-->
<prop key="kaptcha.image.width">${imageWidth}</prop>
<!-- 验证码图片高度 默认为50-->
<prop key="kaptcha.image.height">${imageHeight}</prop>
<!-- 验证码文本字符大小 -->
<prop key="kaptcha.textproducer.font.size">${fontSize}</prop>
<!-- session中存放验证码的key键 -->
<prop key="kaptcha.session.key">validateCode</prop>
<!-- 验证码干扰类 -->
<!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</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>
<!-- 验证码文本字符长度 -->
<prop key="kaptcha.textproducer.char.length">${validateLength}</prop>
<!-- 验证码文本字体样式 -->
<prop key="kaptcha.textproducer.font.names">彩云,宋体,楷体,微软雅黑</prop>
<!-- 背景实现样式 -->
<prop key="kaptcha.background.impl">com.google.code.kaptcha.impl.DefaultBackground</prop>
<!-- 文字渲染器 -->
<prop key="kaptcha.word.impl">com.google.code.kaptcha.text.impl.DefaultWordRenderer</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
这里写入了大部分的Kaptcha的配置,还有更多的配置如下:
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 |
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* @Description : Kaptcha验证码生成及校验
* @Author Difficult_Name on 2020-5-14
* @ClassName ValidateController
* @Date 2020-5-14 10:44
* @Version 1.0
*/
@Controller
public class ValidateController {
private static Logger log = LoggerFactory.getLogger(ValidateController.class);
/* 这里的变量就是ApplicationContext.xml中的bean */
private Producer kaptchaProducer = null;
@Autowired
public void setCaptchaProducer(Producer kaptchaProducer) {
this.kaptchaProducer = kaptchaProducer;
}
@RequestMapping(value = "validateImage")
public void createKaptchaImage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
HttpSession httpSession = httpServletRequest.getSession();
// 清除浏览器的缓存
httpServletResponse.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
httpServletResponse.setHeader("Cache-Control","no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
httpServletResponse.setHeader("Pragma", "no-cache");
// return a jpeg
httpServletResponse.setContentType("image/jpeg");
// 浏览器记忆功能-----当前过浏览器和服务器交互成功以后下载的图片和资源会进行缓存一次。下次刷新的时候就不会在到服务器去下载。
// 获取KAPTCHA验证的随机文本
String kapText = kaptchaProducer.createText();
// 将生成好的图片放入会话中
httpSession.setAttribute(Constants.KAPTCHA_SESSION_KEY, kapText);
// create the image with the text
BufferedImage bufferedImage = kaptchaProducer.createImage(kapText);
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
// write the data out
ImageIO.write(bufferedImage, "jpg", servletOutputStream);
try {
servletOutputStream.flush();
} finally {
servletOutputStream.close();//关闭
}
}
}
<input name="validateCode" type="text" id="validateCode" />
<img src="<%=basePath%>validateImage" id="img" width="80" onclick="changeImage($(this))"
alt = "点击图片刷新" height="40" />
更新图片方法
function changeImage(dobj){
dobj.attr("src", "<%=basePath%>validateImage?d=" + new Date().getTime());
}
验证校验代码如下
/* 获取Kaptcha生成的随机数 */
String validateCode = subjectSession.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString().toLowerCase();
String paramValidateCode = request.getParameter("validateCode");
return validateCode.equals(paramValidateCode.toLowerCase());