栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot验证码kaptcha之加减法运算

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot验证码kaptcha之加减法运算

@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.修改配置文件

代码如下(示例):

com.zjyc.qzaft.entity.customs.AftTextCreator
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;


@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方法,那么就可以生成我们自己想要的验证码文字。

最后感谢大家的观看,希望对您有帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号