本文将使用Jcaptcha实现校验码验证,并演示在Spring/SpringMVC环境下的使用方法。
<dependency>
<groupId>com.octo.captchagroupId>
<artifactId>jcaptcha-allartifactId>
<version>1.0-RC6version>
<exclusions>
<exclusion>
<groupId>quartzgroupId>
<artifactId>quartzartifactId>
exclusion>
<exclusion>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
exclusion>
<exclusion>
<groupId>commons-poolgroupId>
<artifactId>commons-poolartifactId>
exclusion>
<exclusion>
<groupId>hsqldbgroupId>
<artifactId>hsqldbartifactId>
exclusion>
<exclusion>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcacheartifactId>
exclusion>
<exclusion>
<groupId>concurrentgroupId>
<artifactId>concurrentartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
<exclusion>
<groupId>xercesgroupId>
<artifactId>xercesImplartifactId>
exclusion>
<exclusion>
<groupId>xercesgroupId>
<artifactId>xmlParserAPIsartifactId>
exclusion>
exclusions>
dependency>
注:请按需对需要排除的依赖进行增减
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
classpath*:applicationContext-captcha.xml
</param-value>
</context-param>
<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-3.0.xsd"
default-lazy-init="true">
<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" />
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>
0123456789abcdefghijklmnopqrstuvwxyz
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>20value>
constructor-arg>
<constructor-arg index="1">
<value>20value>
constructor-arg>
<constructor-arg index="2">
<list>
<bean class="java.awt.Font">
<constructor-arg index="0">
<value>Arialvalue>
constructor-arg>
<constructor-arg index="1">
<value>0value>
constructor-arg>
<constructor-arg index="2">
<value>20value>
constructor-arg>
bean>
list>
constructor-arg>
bean>
<bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator">
<constructor-arg index="0">
<value>80value>
constructor-arg>
<constructor-arg index="1">
<value>32value>
constructor-arg>
bean>
<bean id="decoratedPaster" class="com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster">
<constructor-arg type="java.lang.Integer" index="0">
<value>4value>
constructor-arg>
<constructor-arg type="java.lang.Integer" index="1">
<value>4value>
constructor-arg>
<constructor-arg index="2">
<ref bean="colorGen" />
constructor-arg>
<constructor-arg index="3">
<list>
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>1value>
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="colorDimGrey" />
constructor-arg>
bean>
<bean id="colorWrite" class="java.awt.Color">
<constructor-arg type="int" index="0">
<value>255value>
constructor-arg>
<constructor-arg type="int" index="1">
<value>255value>
constructor-arg>
<constructor-arg type="int" index="2">
<value>255value>
constructor-arg>
bean>
<bean id="colorDimGrey" class="java.awt.Color">
<constructor-arg type="int" index="0">
<value>105value>
constructor-arg>
<constructor-arg type="int" index="1">
<value>105value>
constructor-arg>
<constructor-arg type="int" index="2">
<value>105value>
constructor-arg>
bean>
beans>
package personal.hutao.captcha;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.octo.captcha.service.image.ImageCaptchaService;
@Controller
@RequestMapping("/captcha")
public class JcaptchaImageCreater {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private ImageCaptchaService imageCaptchaService;
@RequestMapping
public void handleRequest(HttpServletRequest request, HttpServletResponse response) {
try {
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
String captchaId = request.getSession().getId();
BufferedImage challenge = imageCaptchaService.getImageChallengeForID(captchaId, request.getLocale());
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0L);
response.setContentType("image/jpeg");
ImageIO.write(challenge, "jpeg", jpegOutputStream);
byte[] captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
ServletOutputStream respOs = response.getOutputStream();
respOs.write(captchaChallengeAsJpeg);
respOs.flush();
respOs.close();
} catch (IOException e) {
logger.error("generate captcha image error: {}", e.getMessage());
}
}
}
如图:
package personal.hutao.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.octo.captcha.service.image.ImageCaptchaService;
@RequestMapping("/login")
@Controller
public class LoginController {
@Autowired
private ImageCaptchaService imageCaptchaService;
@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
@ResponseBody
public Boolean login(String username, String password, String captcha, HttpServletRequest request) {
Boolean result = Boolean.FALSE;
Boolean isResponseCorrect = imageCaptchaService.validateResponseForID(request.getSession().getId(), captcha);
if (isResponseCorrect) {
// 继续校验用户名密码等..
}
return result;
}
}