[验证码] kaptcha 验证码生成

在spring配置文件中配置kaptcha。

生成验证码的action:

package com.wa.xwolf.freemarker.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 com.google.code.kaptcha.impl.DefaultKaptcha;

@Controller
public class CheckCodeController {
    
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    /**
     * kaptcha 生成验证码
     * @param request
     * @param response
     */
    @RequestMapping(value="/checkCode",method=RequestMethod.GET)
    public void getCheckCode(HttpServletRequest request,HttpServletResponse response){
        
        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 = defaultKaptcha.createText();
        request.getSession().setAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = defaultKaptcha.createImage(capText);
        ServletOutputStream out;
        try {
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

kaptcha 在spring中的配置:

    <!-- 登陆时验证码的配置 -->
    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <!--通过构造函数注入属性值 -->
                <constructor-arg type="java.util.Properties">
                    <props>
                        <!-- 验证码宽度 -->
                        <prop key="kaptcha.image.width">150</prop>
                        <!-- 验证码高度 -->
                        <prop key="kaptcha.image.height">60</prop>
                        <!-- 生成验证码内容范围 -->
                        <prop key="kaptcha.textproducer.char.string">abcdefghijklmnopqrstuvwxyz012345678ABCDEFGHIJKLMNOPQRSTUVWXYZ</prop>
                        <!-- 验证码个数 -->
                        <prop key="kaptcha.textproducer.char.length">5</prop>
                        <!-- 是否有边框 -->
                        <prop key="kaptcha.border">no</prop>
                        <!-- 验证码字体颜色 -->
                        <prop key="kaptcha.textproducer.font.color">black</prop>
                        <!-- 验证码字体大小 -->
                        <prop key="kaptcha.textproducer.font.size">35</prop>
                        <!-- 验证码所属字体样式 -->
                        <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop>
                        <prop key="kaptcha.background.clear.from">white</prop>
                        <prop key="kaptcha.background.clear.to">white</prop>
                        <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
                        <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
                        <!-- 干扰线颜色 -->
                        <prop key="kaptcha.noise.color">red</prop>
                        <!-- 验证码文本字符间距 -->
                        <prop key="kaptcha.textproducer.char.space">4</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>

 

posted @ 2015-03-18 08:58  snow__wolf  阅读(266)  评论(0)    收藏  举报