1,导入jcaptcha.jar包,这里用的是1.0版本
2,编写captcha-context.xml配置文件(非必须,可在spring配置文件中直接添加):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="captchaService" class="com.octo.captcha.service.multitype.GenericManageableCaptchaService"> <constructor-arg index="0" ref="imageEngine"/> <constructor-arg type="int" index="1" value="180"/> <constructor-arg type="int" index="2" value="100000"/> <constructor-arg type="int" index="3" value="75000"/> </bean> <bean id="imageEngine" class="com.octo.captcha.engine.GenericCaptchaEngine"> <constructor-arg index="0"> <list> <ref bean="captchaFactory"/> </list> </constructor-arg> </bean> <bean id="captchaFactory" class="com.octo.captcha.image.gimpy.GimpyFactory"> <constructor-arg> <ref bean="wordgen"/> </constructor-arg> <constructor-arg> <ref bean="wordtoimage"/> </constructor-arg> </bean> <bean id="wordgen" class= "com.octo.captcha.component.word.wordgenerator.RandomWordGenerator"> <!--可选字符--> <constructor-arg> <value>0123456789</value> <!-- <value>abcdefghijklmnopquvwxyz</value> --> </constructor-arg> </bean> <bean id="wordtoimage" class="com.octo.captcha.component.image.wordtoimage.ComposedWordToImage"> <constructor-arg index="0"> <ref bean="fontGenRandom"/> </constructor-arg> <constructor-arg index="1"> <ref bean="backGenUni"/> </constructor-arg> <constructor-arg index="2"> <ref bean="decoratedPaster"/> </constructor-arg> </bean> <bean id="fontGenRandom" class="com.octo.captcha.component.image.fontgenerator.RandomFontGenerator"> <!--最小字体--> <constructor-arg index="0"> <value>16</value> </constructor-arg> <!--最大字体--> <constructor-arg index="1"> <value>20</value> </constructor-arg> <constructor-arg index="2"> <list> <bean class="java.awt.Font"> <constructor-arg index="0"><value>Arial</value></constructor-arg> <constructor-arg index="1"><value>0</value></constructor-arg> <constructor-arg index="2"><value>12</value></constructor-arg> </bean> </list> </constructor-arg> </bean> <bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator"> <!--背景宽度--> <constructor-arg index="0"> <value>90</value> </constructor-arg> <!--背景高度--> <constructor-arg index="1"> <value>30</value> </constructor-arg> </bean> <bean id="decoratedPaster" class="com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster"> <!--最大字符长度--> <constructor-arg type="java.lang.Integer" index="0"> <value>4</value> </constructor-arg> <!--最小字符长度--> <constructor-arg type="java.lang.Integer" index="1"> <value>4</value> </constructor-arg> <!--文本颜色--> <constructor-arg index="2"> <ref bean="colorGen"/> </constructor-arg> <!--文本混淆--> <constructor-arg index="3"> <list> <ref bean="baffleDecorator"/> </list> </constructor-arg> </bean> <bean id="baffleDecorator" class="com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator"> <constructor-arg type="java.lang.Integer" index="0"><value>1</value></constructor-arg> <constructor-arg type="java.awt.Color" index="1"><ref bean="colorWrite"/></constructor-arg> </bean> <bean id="colorGen" class="com.octo.captcha.component.image.color.SingleColorGenerator"> <constructor-arg type="java.awt.Color" index="0"> <ref bean="colorBlack"/> </constructor-arg> </bean> <bean id="colorWrite" class="java.awt.Color"> <constructor-arg type="int" index="0"> <value>255</value> </constructor-arg> <constructor-arg type="int" index="1"> <value>255</value> </constructor-arg> <constructor-arg type="int" index="2"> <value>255</value> </constructor-arg> </bean> <bean id="colorBlack" class="java.awt.Color"> <constructor-arg type="int" index="0"> <value>50</value> </constructor-arg> <constructor-arg type="int" index="1"> <value>50</value> </constructor-arg> <constructor-arg type="int" index="2"> <value>50</value> </constructor-arg> </bean> </beans>
3,编写JcaptchaServlet响应请求提供验证码输出:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] captchaChallengeAsJpeg = null; // 输出jpg的字节流 ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { // get the session id that will identify the generated captcha. // the same id must be used to validate the response, the session id // is a good candidate! String captchaId = session.getSessionId(request); BufferedImage challenge = captchaService.getImageChallengeForID( captchaId, request.getLocale()); // a jpeg encoder JPEGImageEncoder jpegEncoder = JPEGCodec .createJPEGEncoder(jpegOutputStream); jpegEncoder.encode(challenge); } catch (IllegalArgumentException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch (CaptchaServiceException e) { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); // flush it in the response response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT); ServletOutputStream responseOutputStream = response.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); }
4,在web.xml中添加该servlet:
<!-- Jcaptcha --> <servlet> <servlet-name>Jcaptcha</servlet-name> <servlet-class>cn.xxx.JcaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Jcaptcha</servlet-name> <url-pattern>/captcha.jpg</url-pattern> </servlet-mapping>
5,在html中显示验证码只需要以下代码:
<div><img src="${base }/captcha.jpg" onclick="this.src='${base}/captcha.jpg?'+Math.random()"/></div>
<div class="input_capt">验证码: <input type="text"id="captcha" name="captcha" />
6,后台验证的话,只需要调用ImageCaptchaService的一个方法即可:
imageCaptchaService.validateResponseForID(session.getSessionId(getRequest()), captcha);