grails验证码插件-JCaptcha

1、安装

grails install-plugin jcaptcha

 2、现在Config.groovy文件中定义验证码图片样式

Config文件结构:

log4j {
    /* log4j config */
}

jcaptchas {
 //captcha1 图片的id
 captcha1 = … 
 captcha2 = … 
}

 一个Example:

jcaptchas {
    Random random = new Random(new Date().getTime());
    imageCaptcha = new GenericManageableCaptchaService(
            new GenericCaptchaEngine(
                    new GimpyFactory(
                            //随机字符范围
                            new RandomWordGenerator(
                                    "加减乘除abcdefghjklmnopqOPQARSTS"
                            ),
                            new ComposedWordToImage(
                                    //字体
                                    new RandomFontGenerator(
                                            20, // min font size
                                            30, // max font size
                                            [new Font("宋体", 0, 10)] as Font[]
                                    ),
                                    //图片背景
                                    new GradientBackgroundGenerator(
                                            200, // width
                                            100, // height
                                            new SingleColorGenerator(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))),
                                            new SingleColorGenerator(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)))
                                    ),
                                    //字符颜色个数限制
                                    new NonLinearTextPaster(
                                            1, // minimal length of text
                                            4, // maximal length of text
                                            new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))
                                    )
                            )
                    )
            ),
            180, // minGuarantedStorageDelayInSeconds
            180000 // maxCaptchaStoreSize
    )
}

 3、标签引用:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>Simple GSP page</title></head>

<body>
<g:form controller="validate" action="valid">
    <!--name与config.groovy中定义的一致 -->
    <jcaptcha:jpeg name="imageCaptcha"/>
    <label>
        <input type="text" name="code" value=""/>
    </label>
    <input type="submit" value="提交"/>
</g:form>
</body>
</html>

 4、验证输入是否正确:

在Controller中定义

package demo

class ValidateController {

    //这个是注入的
    def jcaptchaService;

    def index = {
        redirect(action: "valid")
    }

    def valid = {
        println params
        println session.id
        if (params.size() == 2) {
            return render(view: 'code');
        }
        /*
        这里的try catch 是为了防止重复提交,重复提交会报错误
         */
        try {
            /*
            三个参数:name(标签中的name),session.id这个是固定的,用户输入的内容
             */
            if (!jcaptchaService.validateResponse("imageCaptcha", session.id, params.code)) {
                flash.message = "错误"
            } else {
                flash.message = "正确"
            }
        } catch (Exception e) {
            log.error(e.message);
            flash.message = "拒绝重复提交"
        }
        return render(view: "message");
    }
}
 

你可能感兴趣的:(插件,验证码,grails,JCAPTCHA)