kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。
1.在applicationContext.xml中配置,切记启动时别忘了加载。
<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">80</prop> <prop key="kaptcha.textproducer.font.size">30</prop> <prop key="kaptcha.image.height">30</prop> <prop key="kaptcha.session.key">code</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop> <prop key="kaptcha.GimpyEngine">WaterRipple</prop> <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop> </props> </constructor-arg> </bean> </property> </bean>
2.新建图片控制类CaptchaController.java
package com.ly.controller;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
@Controller
@RequestMapping("/captchaController")
public class CaptchaController extends BaseController {
@Autowired
private Producer captchaProducer = null;
@RequestMapping("/image")
public String getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String code = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************验证码是: " + code);
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
3.前台登录页面.jsp
<div id="ck">
验证码 <input type="text" id="kaptcha" name="kaptcha" style="width: 60px;" class="easyui-validatebox" maxlength="4" data-options="required:true"/>
<img id="kaptchaImage" src="captchaController/image" style="margin-bottom: -10px"/>
</div>
<script type="text/javascript">
$('#kaptchaImage').click(function () {//生成验证码
$(this).hide().attr('src', 'captchaController/image?' + Math.floor(Math.random()*100) ).fadeIn();
event.cancelBubble=true;
});
</script>
4.验证登录什么的功能,自己发挥吧!
以下另送配置属性参考值:
<servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <init-param> <description> Border around kaptcha. Legal values are yes or no. </description> <param-name>kaptcha.border</param-name> <param-value>no</param-value> </init-param> <init-param> <description>Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. </description> <param-name>kaptcha.border.color</param-name> <param-value>red</param-value> </init-param> <init-param> <description>Thickness of the border around kaptcha. Legal values are > 0. </description> <param-name>kaptcha.border.thickness</param-name> <param-value>5</param-value> </init-param> <init-param> <description>Width in pixels of the kaptcha image. </description> <param-name>kaptcha.image.width</param-name> <param-value>80</param-value> </init-param> <init-param> <description>Height in pixels of the kaptcha image. </description> <param-name>kaptcha.image.height</param-name> <param-value>40</param-value> </init-param> <init-param> <description>The image producer. </description> <param-name>kaptcha.producer.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </param-value> </init-param> <init-param> <description>The text producer. </description> <param-name>kaptcha.textproducer.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value> </init-param> <init-param> <description>The characters that will create the kaptcha. </description> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>abcde2345678gfynmnpwx </param-value> </init-param> <init-param> <description>The number of characters to display. </description> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>5</param-value> </init-param> <init-param> <description>A list of comma separated font names.</description> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>Arial, Courier</param-value> </init-param> <init-param> <description>The size of the font to use. </description> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>23</param-value> </init-param> <init-param> <description>The color to use for the font. Legal values are r,g,b. </description> <param-name>kaptcha.textproducer.font.color</param-name> <param-value>black</param-value> </init-param> <init-param> <description>The noise producer. </description> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise </param-value> </init-param> <init-param> <description>The noise color. Legal values are r,g,b. </description> <param-name>kaptcha.noise.color</param-name> <param-value>black</param-value> </init-param> <init-param> <description>The obscurificator implementation. </description> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> <init-param> <description>The background implementation. </description> <param-name>kaptcha.background.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value> </init-param> <init-param> <description>Ending background color. Legal values are r,g,b. </description> <param-name>kaptcha.background.clear.to</param-name> <param-value>white</param-value> </init-param> <init-param> <description>The word renderer implementation. </description> <param-name>kaptcha.word.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value> </init-param> <init-param> <description>The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session. </description> <param-name>kaptcha.session.key</param-name> <param-value>KAPTCHA_SESSION_KEY</param-value> </init-param> <init-param> <description>The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session. </description> <param-name>kaptcha.session.date</param-name> <param-value>KAPTCHA_SESSION_DATE</param-value> </init-param> </servlet>