Spring中使用Jcaptcha实现校验码验证

本文将使用Jcaptcha实现校验码验证,并演示在Spring/SpringMVC环境下的使用方法。

1. maven依赖

<dependency>
  <groupId>com.octo.captcha</groupId>
  <artifactId>jcaptcha-all</artifactId>
  <version>1.0-RC6</version>
  <exclusions>
    <exclusion>
      <groupId>quartz</groupId>
      <artifactId>quartz</artifactId>
    </exclusion>
    <exclusion>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
    </exclusion>
    <exclusion>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      </exclusion>
    <exclusion>
      <groupId>hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
    </exclusion>
    <exclusion>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </exclusion>
    <exclusion>
      <groupId>concurrent</groupId>
      <artifactId>concurrent</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
    </exclusion>
    <exclusion>
      <groupId>xerces</groupId>
      <artifactId>xercesImpl</artifactId>
    </exclusion>
    <exclusion>
      <groupId>xerces</groupId>
      <artifactId>xmlParserAPIs</artifactId>
    </exclusion>
  </exclusions>
</dependency>

注:请按需对需要排除的依赖进行增减

2. 需要将Jcaptcha的Bean加入Spring管理,故新增Spring-Jcaptcha的配置文件。web.xml中

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:applicationContext.xml
        classpath*:applicationContext-captcha.xml
    </param-value>
</context-param>

3. applicationContext-captcha.xml的内容

<?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-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>20</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>20</value>
          </constructor-arg>
        </bean>
      </list>
    </constructor-arg>
  </bean>

  <bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator">
    <!--背景宽度-->
    <constructor-arg index="0">
      <value>80</value>
    </constructor-arg>
    <!--背景高度-->
    <constructor-arg index="1">
      <value>32</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="colorDimGrey" />
</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="colorDimGrey" class="java.awt.Color">
<constructor-arg type="int" index="0">
<value>105</value>
</constructor-arg>
<constructor-arg type="int" index="1">
<value>105</value>
</constructor-arg>
<constructor-arg type="int" index="2">
<value>105</value>
</constructor-arg>
</bean>
</beans>

4. 服务端生成校验码

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());
    }
  }

}

5. 页面调用

<input type="text" id="captcha" name="captcha" maxlength="4" />
<img src="captcha" onclick="this.src='captcha?d='+new Date().getTime()"/>

如图:

6. 提交登录表单后,校验

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;
  }

}
posted @ 2016-06-27 17:48  Study_Work  阅读(283)  评论(0编辑  收藏  举报