kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。
1:前期工作:准备kaptcha的jar包
<!--kaptcha--> <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency>
2:在spring配置文件中配置图片生成器的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">no</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">red</prop>
<prop key="kaptcha.image.width">250</prop>
<prop key="kaptcha.textproducer.font.size">80</prop>
<prop key="kaptcha.image.height">90</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
里面配置了很多图片生成器的属性,比如字体颜色,字体大小等,根据英文意思就能理解大概配置的是什么内容了
3:生成图片的控制器
@Controller
public class CaptchaImageCreateController {
@Autowired
private Producer captchaProducer;//验证码生成器
@RequestMapping("/captcha-image")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
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();
// store the text in the session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);//将生成的验证码保存在session中
// 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.验证码验证控制器
@Controller
public class VerifyController {
@RequestMapping(value = "/checkVerificationCode")
@ResponseBody
public boolean checkVerificationCode(@RequestParam("verifyCode")String verifyCode,
HttpServletRequest request){
//验证码的值
String kaptchaExpected = (String)request.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//用户输入的验证码的值
String kaptchaReceived = verifyCode;
System.out.println("实际的验证码为:"+kaptchaExpected);
System.out.println("输入的验证码为:"+kaptchaReceived);
if(kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) {
return false;
}
return true;
}
}
期待输入的验证码从session中取出,和实际输入的验证码进行比较,如果相同,则返回true,否则返回false
5:前端页面
<div class="title"> 用户登录 </div>
<div class="loginbox">
<form id="loginForm" action="/checkVerificationCode" method="post">
<div style="height:40px;">
<label class="tip">登 录 名: </label>
<input name="name" type="text" id="name" class="user-text" value="" />
</div>
<div style="height:40px;">
<label class="tip">密 码: </label>
<input type="password" id="password" name="password" class="user-text" value="" />
</div>
<div style="height:60px;">
<label class="tip">验 证 码: </label>
<input type="text" name="verifyCode" id="verifyCode" class="usertext" value="" />
οnchange="changeVerifyCode();"/>
<img src="captcha-image.jpg" width="110" height="30" id="kaptchaImage"
style="margin-bottom: -13px"/>
</div>
<div style="margin-left:15px">
<input type="submit" class="login-btn" value="登录" />
<input type="reset" class="login-btn" style="margin-left:10px;" value="重置" />
</div>
</form>
</div>
src="captcha-image.jpg",将触发生成图片的控制器,返回一张图片,action="/checkVerificationCode",将调用验证验证码的控制器,返回相应的结果。
附完整代码地址:https://github.com/TiantianUpup/validate