本文所写是在 Spring-mvc框架下集成 kaptcha
1.下载jar包
下载地址:kaptcha-2.3.2.jar
2.在applicationContext.xml 下配置 bean
<!-- 验证码配置 -->
<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">yes</prop>
<!-- 边框颜色 -->
<prop key="kaptcha.border.color">black</prop>
<!-- 验证码图片宽度 -->
<prop key="kaptcha.image.width">140</prop>
<!-- 验证码图片高度 -->
<prop key="kaptcha.image.height">34</prop>
<prop key="kaptcha.session.key">code</prop>
<!-- 验证码 颜色 -->
<prop key="kaptcha.textproducer.font.color">black</prop>
<!-- 验证码 大小 -->
<prop key="kaptcha.textproducer.font.size">30</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
<!-- 验证码 字数 -->
<prop key="kaptcha.textproducer.char.length">5</prop>
<!-- 验证码图片 背景渐变色 开始 -->
<prop key="kaptcha.background.clear.from">gray</prop>
<!-- 验证码图片 背景渐变色 结束 -->
<prop key="kaptcha.background.clear.to">white</prop>
<!-- 验证码图片样式 我使用的是阴影-->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
<!--配置中文-->
<!--<prop key="kaptcha.textproducer.impl">com.google.code.kaptcha.text.impl.ChineseTextProducer-->
<!--</prop>-->
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
<!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>-->
<prop key="kaptcha.textproducer.char.string">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
3.生成验证码的 controller
/**
* 生成验证码Controller
* Created by Administrator on 2018-01-10.
*/
@Controller
@RequestMapping("/captchaImage")
public class CaptchaImageCreateController {
private Producer captchaProducer = null;
@Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
@RequestMapping("/kaptcha.jpg")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Set to expire far in the past.
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
System.out.println("验证码===" + capText);
// store the text in the session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
4.html 页面
<input type="text" class="form-control" style="width:160px;float: left" maxlength="5" placeholder="验证码" id="verifyCode"/>
<img src="/captchaImage/kaptcha.jpg" style="margin-left: 20px" id="checkcode" onclick="onCheckCode()" title="看不清换一张">
5.点击图片刷新验证码 同时清空输入框
function onCheckCode() {
var date = new Date();
$("#checkcode").attr("src", "/captchaImage/kaptcha.jpg?d=" + date);
$("#verifyCode").val(null);
}
6.登录验证
//session中的验证码
String kaptchaExpected = (String) request.getSession().getAttribute(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//将 session 中的验证码 转换成小写
StringBuffer sb = new StringBuffer();
if (kaptchaExpected != null) {
for (int i = 0; i < kaptchaExpected.length(); i++) {
char ch;
if (kaptchaExpected.charAt(i) >= 'a' && kaptchaExpected.charAt(i) <= 'z') {//判断字符是否在a-z之间(小写)
ch = kaptchaExpected.charAt(i); //不变
} else if (kaptchaExpected.charAt(i) >= 'A' && kaptchaExpected.charAt(i) <= 'Z') { //判断字符是否在A-Z之间(大写)
ch = (char) (kaptchaExpected.charAt(i) + 32); //如果为小写则转换为相应大写,赋值给ch
} else if (kaptchaExpected.charAt(i) >= '0' && kaptchaExpected.charAt(i) <= '9') {//判断字符是否在0-9之间(数字)
ch = kaptchaExpected.charAt(i); //如果为数字,将原数字赋值给ch
} else {
ch = '*'; //如果为其他则转为*号
}
sb.append(ch);
}
}
kaptchaExpected = sb.toString();
System.out.println("kaptchaExpected===" + kaptchaExpected);
//将用户输入的验证码转换成小写
StringBuffer sb2 = new StringBuffer();
if (kaptchaReceived != null) {
for (int i = 0; i < kaptchaReceived.length(); i++) {
char ch;
if (kaptchaReceived.charAt(i) >= 'a' && kaptchaReceived.charAt(i) <= 'z') {//判断字符是否在a-z之间(小写)
ch = kaptchaReceived.charAt(i); //不变
} else if (kaptchaReceived.charAt(i) >= 'A' && kaptchaReceived.charAt(i) <= 'Z') { //判断字符是否在A-Z之间(大写)
ch = (char) (kaptchaReceived.charAt(i) + 32); //如果为小写则转换为相应大写,赋值给ch
} else if (kaptchaReceived.charAt(i) >= '0' && kaptchaReceived.charAt(i) <= '9') {//判断字符是否在0-9之间(数字)
ch = kaptchaReceived.charAt(i); //如果为数字,将原数字赋值给ch
} else {
ch = '*'; //如果为其他则转为*号
}
sb2.append(ch);
}
}
kaptchaReceived = sb2.toString();
System.out.println("kaptchaReceived===" + kaptchaReceived);
//校验验证码是否正确
if (kaptchaReceived == null || !kaptchaReceived.equals(kaptchaExpected)) {
return new Message(Message.Type.error, "验证码不正确!");
}
最后实现效果!