Framework MVC ·

验证码生成神器之-google patchca

之前在做项目时都是使用自己写的JAVA类来生成验证码,感觉一是不方便配置,二是比较简单,无意中在google上发现了一个比较小巧的验证码生成框架,使用了下,感觉非常不错,特推荐之.

先看看他在google上的项目:
20141010165724

 

 

 

 

 

 

 

 

 

再看看我在项目中使用情况:

20141010165145
 

 

 

 

 

 

 

 

 

 

目前在大多数项目中使用了spring, 然后和我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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:c="http://www.springframework.org/schema/c" xmlns:u="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <bean id="captchaService" class="org.patchca.service.ConfigurableCaptchaService" p:fontFactory-ref="randomFontFactory"
        p:wordFactory-ref="randomWordFactory" p:colorFactory-ref="singleColorFactory" p:width="80" p:height="29"
        p:filterFactory-ref="curvesRippleFilterFactory" />

    <bean id="curvesRippleFilterFactory" class="org.patchca.filter.predefined.CurvesRippleFilterFactory"
        c:colorFactory-ref="singleColorFactory" />

    <bean id="randomFontFactory" class="org.patchca.font.RandomFontFactory" p:families-ref="familieList" p:minSize="20"
        p:maxSize="30" p:randomStyle="true" />

    <u:list id="familieList" list-class="java.util.ArrayList">
        <value>Arial</value>
        <value>Verdana</value>
        <value>Tahoma</value>
    </u:list>

    <bean id="randomWordFactory" class="org.patchca.word.RandomWordFactory" p:characters="abcdefhkmnpstvwxyz23456789"
        p:minLength="4" p:maxLength="5" />

    <bean id="singleColorFactory" class="org.patchca.color.SingleColorFactory" c:color-ref="color" />
    <bean id="color" class="java.awt.Color">
        <constructor-arg index="0" type="int" value="25" />
        <constructor-arg index="1" type="int" value="60" />
        <constructor-arg index="2" type="int" value="170" />
    </bean>

</beans>

如果调用呢? 很简单

package com.tojsp.module;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.patchca.service.CaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
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.tojsp.common.Constants;
import com.tojsp.module.BaseContextController;

@Controller
public class CaptchaController extends BaseContextController {

    private static final Logger LOG = Logger.getLogger(CaptchaController.class);

    @Autowired
    private CaptchaService      capService;

    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public void captcha(HttpSession session, HttpServletResponse response) {
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            String s = EncoderHelper.getChallangeAndWriteImage(capService, "png", os);
            session.setAttribute(Constants.System.CAPTCHA, md5.encode(s));
            os.flush();
        } catch (IOException e) {
            LOG.error("generate captcha io exception", e);
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

参与评论