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");
}
}
你好,你是否出现如下错误。
| Error Compilation error: startup failed: E:\work\myclass\grails-app\conf\Config.groovy: 112: unable to resolve class Gene ricManageableCaptchaService @ line 112, column 20. imageCaptcha = new GenericManageableCaptchaService( ^
E:\work\myclass\grails-app\conf\Config.groovy: 113: unable to resolve class Gene ricCaptchaEngine @ line 113, column 13. new GenericCaptchaEngine( ^
E:\work\myclass\grails-app\conf\Config.groovy: 114: unable to resolve class Gimp yFactory @ line 114, column 21. new GimpyFactory( ^
E:\work\myclass\grails-app\conf\Config.groovy: 116: unable to resolve class Rand omWordGenerator @ line 116, column 29. new RandomWordGenerator( ^
E:\work\myclass\grails-app\conf\Config.groovy: 119: unable to resolve class Comp osedWordToImage @ line 119, column 29. new ComposedWordToImage( ^
E:\work\myclass\grails-app\conf\Config.groovy: 121: unable to resolve class Rand omFontGenerator @ line 121, column 37. new RandomFontGenerator( ^
E:\work\myclass\grails-app\conf\Config.groovy: 124: unable to resolve class Font
@ line 124, column 45. [new Font( ^
E:\work\myclass\grails-app\conf\Config.groovy: 124: unable to resolve class Font
@ line 124, column 46. [new Font(" ^
E:\work\myclass\grails-app\conf\Config.groovy: 127: unable to resolve class Grad ientBackgroundGenerator @ line 127, column 37. new GradientBackgroundGenerator( ^
E:\work\myclass\grails-app\conf\Config.groovy: 130: unable to resolve class Sing leColorGenerator @ line 130, column 45. new Single ^
E:\work\myclass\grails-app\conf\Config.groovy: 130: unable to resolve class Colo r @ line 130, column 70. new SingleColorGenerator(new Color( ^
E:\work\myclass\grails-app\conf\Config.groovy: 131: unable to resolve class Sing leColorGenerator @ line 131, column 45. new Single ^
E:\work\myclass\grails-app\conf\Config.groovy: 131: unable to resolve class Colo r @ line 131, column 70. new SingleColorGenerator(new Color( ^
E:\work\myclass\grails-app\conf\Config.groovy: 134: unable to resolve class NonL inearTextPaster @ line 134, column 37. new NonLinearTextPaster( ^
E:\work\myclass\grails-app\conf\Config.groovy: 137: unable to resolve class Colo r @ line 137, column 45. new Color( ^
15 errors