给Grails添加验证码插件之jcaptcha-1.0

给Grails添加验证码插件之jcaptcha-1.0
JCaptcha Plugin :基于 JCaptcha 开源图片和音频验证码项目的Grails插件
而jcaptcha-1.0终于出来了. 比起0.2轻量了很多, 而且0.2里面有不少无用的东西...
现在grails-app下面主要的文件就剩下JcaptchaController.groovy, JcaptchaService.groovy, JcaptchaTagLib.groovy了.
JcaptchaTagLib.groovy里面其实也就定义了两个标签, 一个用户图片验证(jpeg), 一个用于语音验证(wav).
JcaptchaController.groovy里面则定义了主要两个action(jpeg和wav)用于JcaptchaTagLib.groovy的两个标签调用.
JcaptchaService.groovy则定义了四个常用方法, 主要是boolean validateResponse(captchaName, id, response)用于验证输入验证码是否正确

下面看看使用验证码代码:
首先需要在Config.groovy, 添加:
import Java.awt.Font
import Java.awt.Color
import com.octo.captcha.service.multitype.GenericManageableCaptchaService
import com.octo.captcha.engine.GenericCaptchaEngine
import com.octo.captcha.image.gimpy.GimpyFactory
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator
import com.octo.captcha.component.image.color.SingleColorGenerator
import com.octo.captcha.component.image.textpaster.NonLinearTextPaster

jcaptchas { 
        imageCaptcha = new GenericManageableCaptchaService( 
            new GenericCaptchaEngine( 
                new GimpyFactory( 
                    new RandomWordGenerator( 
                        "abcdefghijklmnopqrstuvwxyz1234567890" 
                    ), 
                    new ComposedWordToImage( 
                        new RandomFontGenerator(                  // 生成图片文字的字体以及字号大小,可以是多种字体,会随机出现。
                            20, // min font size 
                            30, // max font size 
                            [new Font("Arial", 0, 10)] as Font[] 
                        ), 
                        new GradientBackgroundGenerator(       // 配置验证图片的大小和背景色以及过渡色
                            140, // width 
                            35, // height 
                            new SingleColorGenerator(new Color(255, 255, 255)), 
                            new SingleColorGenerator(new Color(200, 200, 200)) 
                        ), 
                        new NonLinearTextPaster( 
                            4, // minimal length of text 
                            4, // maximal length of text 
                            new Color(11, 11, 11) 
                        ) 
                    ) 
                ) 
            ), 
            180, // minGuarantedStorageDelayInSeconds 
            180000 // maxCaptchaStoreSize 
        )


页面中添加:
< input id="captchaResponse" maxlength="30" name="captchaResponse" type="text" />
< jcaptcha:jpeg name="imageCaptcha" width="100px" height="25px" >< /jcaptcha:jpeg >

Controller里面要做验证
if (! jcaptchaService.validateResponse("imageCaptcha", session.id, params.captchaResponse )){
    flash.message = message(code:'pinpin.login.invalid.jcaptcha')
    return
}

你可能感兴趣的:(给Grails添加验证码插件之jcaptcha-1.0)