@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;
/**
* Author: mek
* Date: 2022\5\14 0014
* Time: 16:17
* Description: 自定义文本生成器
*/
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.修改配置文件
代码如下(示例):
<prop key="kaptcha.textproducer.impl">com.zjyc.qzaft.entity.customs.AftTextCreator</prop>
3.验证码判断逻辑
代码如下(示例):
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;
/**
* @author codermy
* @createTime 2020/7/20
*/
@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);
}
/**
* 计算原始验证码里的运算结果
* @param captcha 原始生成的验证码字符串
* @return 计算结果字符串
*/
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方法,那么就可以生成我们自己想要的验证码文字。
最后感谢大家的观看,希望对您有帮助。