如题,看见一个网站他们使用的验证码是加法运算的。头两次没注意到是让运算,直接输入了里面的内容,都说是错误。仔细一看。。。有意思。。。然后想着JF里面应该也是很容易做到这个吧。。。来试试:
新建一个Controller,里面建俩方法Action:
//...
public void captcha() {
render(new CaptchaRender() {
int bound = 10;//100
String myRandomString;
protected String getRandomString() {
int a = random.nextInt(bound);
int b = random.nextInt(bound);
String ret = null;
switch (random.nextInt(3)) {
case 0:
//临时存储起来,用于渲染给客户端
myRandomString = a + "+" + b + "=?";
//返回值被存在系统里,用于验证
ret = String.valueOf(a + b);
break;
case 1:
myRandomString = a + "-" + b + "=?";
ret = String.valueOf(a - b);
break;
default:
myRandomString = a + "*" + b + "=?";
ret = String.valueOf(a * b);
break;
}
return ret;
}
protected void drawGraphic(String x, BufferedImage image){
//使用临时存储起来的值,渲染给客户端
super.drawGraphic(myRandomString, image);
}
});
}
public void captchaOk() {
boolean validate = CaptchaRender.validate(this, get("val"));
renderText(String.valueOf(validate));
}然后请求一下,看下效果:

出来了,再验证一下结果吧:
哎!有意思!。。。