@TOC
前言实现加减法判断的图形验证码,效果如下图
一、实现原理
自定义一个kaptcha.textproducer.impl的实现类
二、实现步骤 1.自定义一个AftTextCreator类代码如下(示例):
package com.zjyc.qzaft.entity.customs;
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
import com.google.common.base.Objects;
public class AftTextCreator extends DefaultTextCreator {
@Override
public String getText() {
int leftNum = (int) (Math.random() * 100);
int rightNum = (int) (Math.random() * 100);
String[] opts = new String[]{"+", "-"};
String opt = opts[Math.random() > 0.5 ? 1 : 0];
if (leftNum < rightNum && Objects.equal(opt, "-")) {
int temp = leftNum;
leftNum = rightNum;
rightNum = temp;
}
return leftNum + opt + rightNum + "=";
}
}
2.修改配置文件
代码如下(示例):
3.验证码判断逻辑com.zjyc.qzaft.entity.customs.AftTextCreator
代码如下(示例):
package com.zjyc.qzaft.web.security.filter;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.zjyc.qzaft.entity.customs.RequestMessage;
import com.zjyc.qzaft.entity.util.BizException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Component
public class VerifyCodeFilter extends OncePerRequestFilter {
private String defaultFilterProcessUrl = "/login";
private String method = "POST";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//验证码的信息存放在seesion种,具体看EasyCaptcha官方解释
String genCaptcha = (String) request.getSession().getAttribute("captcha");
if (!StrUtil.equalsIgnoreCase(calculateNumber(genCaptcha), requestCaptcha)) {
session.removeAttribute("captcha");
response.getWriter().write(JSON.toJSONString(new RequestMessage().message("验证码错误!")));
return;
}
chain.doFilter(request, response);
}
private String calculateNumber(String captcha) {
int subIndex = captcha.indexOf("-");
int plusIndex = captcha.indexOf("+");
String ret;
if (subIndex != -1) {
int leftNum = Integer.parseInt(captcha.substring(0, subIndex));
int rightNum = Integer.parseInt(captcha.substring(subIndex + 1, captcha.length() - 1));
ret = String.valueOf(leftNum - rightNum);
} else if (plusIndex != -1) {
int leftNum = Integer.parseInt(captcha.substring(0, plusIndex));
int rightNum = Integer.parseInt(captcha.substring(plusIndex + 1, captcha.length() - 1));
ret = String.valueOf(leftNum + rightNum);
} else {
throw new BizException("验证码格式不对");
}
return ret;
}
}
总结
这个思路,是我看到下面这段代码想出来的
captchaProducer.createText();
进入实现
public String createText() {
return this.getConfig().getTextProducerImpl().getText();
}
继续进入
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.google.code.kaptcha.text.impl;
import com.google.code.kaptcha.text.TextProducer;
import com.google.code.kaptcha.util.Configurable;
import java.util.Random;
public class DefaultTextCreator extends Configurable implements TextProducer {
public DefaultTextCreator() {
}
public String getText() {
int length = this.getConfig().getTextProducerCharLength();
char[] chars = this.getConfig().getTextProducerCharString();
Random rand = new Random();
StringBuffer text = new StringBuffer();
for(int i = 0; i < length; ++i) {
text.append(chars[rand.nextInt(chars.length)]);
}
return text.toString();
}
}
可以看到,最后进入了DefaultTextCreator类的getText()方法,所以只要覆盖掉getText方法,那么就可以生成我们自己想要的验证码文字。
最后感谢大家的观看,希望对您有帮助。



